Author: markt
Date: Tue Sep 25 21:36:55 2012
New Revision: 1390162

URL: http://svn.apache.org/viewvc?rev=1390162&view=rev
Log:
Move the cache for the attributes listed below from the Processor to the 
SocketWrapper as they are fixed for the lifetime of the socket and the 
Processor gets recycled on every request
- remote address
- remote host name
- remote port
- local address
- local host name
- local port

This reduces object allocation during my load test by ~30% (the allocations 
were triggered by the AccessLogValve logging the remote IP)

Modified:
    tomcat/trunk/java/org/apache/coyote/http11/AbstractHttp11Processor.java
    tomcat/trunk/java/org/apache/coyote/http11/Http11AprProcessor.java
    tomcat/trunk/java/org/apache/coyote/http11/Http11NioProcessor.java
    tomcat/trunk/java/org/apache/coyote/http11/Http11Processor.java
    tomcat/trunk/java/org/apache/tomcat/util/net/SocketWrapper.java

Modified: 
tomcat/trunk/java/org/apache/coyote/http11/AbstractHttp11Processor.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/coyote/http11/AbstractHttp11Processor.java?rev=1390162&r1=1390161&r2=1390162&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/coyote/http11/AbstractHttp11Processor.java 
(original)
+++ tomcat/trunk/java/org/apache/coyote/http11/AbstractHttp11Processor.java Tue 
Sep 25 21:36:55 2012
@@ -159,42 +159,6 @@ public abstract class AbstractHttp11Proc
     protected int keepAliveTimeout = -1;
 
     /**
-     * Remote Address associated with the current connection.
-     */
-    protected String remoteAddr = null;
-
-
-    /**
-     * Remote Host associated with the current connection.
-     */
-    protected String remoteHost = null;
-
-
-    /**
-     * Local Host associated with the current connection.
-     */
-    protected String localName = null;
-
-
-    /**
-     * Local port to which the socket is connected
-     */
-    protected int localPort = -1;
-
-
-    /**
-     * Remote port to which the socket is connected
-     */
-    protected int remotePort = -1;
-
-
-    /**
-     * The local Host address.
-     */
-    protected String localAddr = null;
-
-
-    /**
      * Maximum timeout on uploads. 5 minutes as in Apache HTTPD server.
      */
     protected int connectionUploadTimeout = 300000;
@@ -1662,12 +1626,6 @@ public abstract class AbstractHttp11Proc
             asyncStateMachine.recycle();
         }
         upgradeInbound = null;
-        remoteAddr = null;
-        remoteHost = null;
-        localAddr = null;
-        localName = null;
-        remotePort = -1;
-        localPort = -1;
         comet = false;
         recycleInternal();
     }

Modified: tomcat/trunk/java/org/apache/coyote/http11/Http11AprProcessor.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/coyote/http11/Http11AprProcessor.java?rev=1390162&r1=1390161&r2=1390162&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/coyote/http11/Http11AprProcessor.java 
(original)
+++ tomcat/trunk/java/org/apache/coyote/http11/Http11AprProcessor.java Tue Sep 
25 21:36:55 2012
@@ -278,87 +278,110 @@ public class Http11AprProcessor extends 
 
         if (actionCode == ActionCode.REQ_HOST_ADDR_ATTRIBUTE) {
 
-            // Get remote host address
-            if (remoteAddr == null && (socketRef != 0)) {
-                try {
-                    long sa = Address.get(Socket.APR_REMOTE, socketRef);
-                    remoteAddr = Address.getip(sa);
-                } catch (Exception e) {
-                    log.warn(sm.getString("http11processor.socket.info"), e);
+            if (socketRef == 0) {
+                request.remoteAddr().recycle();
+            } else {
+                if (socket.getRemoteAddr() == null) {
+                    try {
+                        long sa = Address.get(Socket.APR_REMOTE, socketRef);
+                        socket.setRemoteAddr(Address.getip(sa));
+                    } catch (Exception e) {
+                        log.warn(sm.getString("http11processor.socket.info"), 
e);
+                    }
                 }
+                request.remoteAddr().setString(socket.getRemoteAddr());
             }
-            request.remoteAddr().setString(remoteAddr);
 
         } else if (actionCode == ActionCode.REQ_LOCAL_NAME_ATTRIBUTE) {
 
-            // Get local host name
-            if (localName == null && (socketRef != 0)) {
-                try {
-                    long sa = Address.get(Socket.APR_LOCAL, socketRef);
-                    localName = Address.getnameinfo(sa, 0);
-                } catch (Exception e) {
-                    log.warn(sm.getString("http11processor.socket.info"), e);
+            if (socketRef == 0) {
+                request.localName().recycle();
+            } else {
+                if (socket.getLocalName() == null) {
+                    try {
+                        long sa = Address.get(Socket.APR_LOCAL, socketRef);
+                        socket.setLocalName(Address.getnameinfo(sa, 0));
+                    } catch (Exception e) {
+                        log.warn(sm.getString("http11processor.socket.info"), 
e);
+                    }
                 }
+                request.localName().setString(socket.getLocalName());
             }
-            request.localName().setString(localName);
 
         } else if (actionCode == ActionCode.REQ_HOST_ATTRIBUTE) {
 
-            // Get remote host name
-            if (remoteHost == null && (socketRef != 0)) {
-                try {
-                    long sa = Address.get(Socket.APR_REMOTE, socketRef);
-                    remoteHost = Address.getnameinfo(sa, 0);
-                    if (remoteHost == null) {
-                        remoteHost = Address.getip(sa);
+            if (socketRef == 0) {
+                request.remoteHost().recycle();
+            } else {
+                if (socket.getRemoteHost() == null) {
+                    try {
+                        long sa = Address.get(Socket.APR_REMOTE, socketRef);
+                        socket.setRemoteHost(Address.getnameinfo(sa, 0));
+                        if (socket.getRemoteHost() == null) {
+                            if (socket.getRemoteAddr() == null) {
+                                socket.setRemoteAddr(Address.getip(sa));
+                            }
+                            if (socket.getRemoteAddr() != null) {
+                                socket.setRemoteHost(socket.getRemoteAddr());
+                            }
+                        }
+                    } catch (Exception e) {
+                        log.warn(sm.getString("http11processor.socket.info"), 
e);
                     }
-                } catch (Exception e) {
-                    log.warn(sm.getString("http11processor.socket.info"), e);
+                } else {
+                    request.remoteHost().setString(socket.getRemoteHost());
                 }
             }
-            request.remoteHost().setString(remoteHost);
 
         } else if (actionCode == ActionCode.REQ_LOCAL_ADDR_ATTRIBUTE) {
 
-            // Get local host address
-            if (localAddr == null && (socketRef != 0)) {
-                try {
-                    long sa = Address.get(Socket.APR_LOCAL, socketRef);
-                    localAddr = Address.getip(sa);
-                } catch (Exception e) {
-                    log.warn(sm.getString("http11processor.socket.info"), e);
+            if (socketRef == 0) {
+                request.localAddr().recycle();
+            } else {
+                if (socket.getLocalAddr() == null) {
+                    try {
+                        long sa = Address.get(Socket.APR_LOCAL, socketRef);
+                        socket.setLocalAddr(Address.getip(sa));
+                    } catch (Exception e) {
+                        log.warn(sm.getString("http11processor.socket.info"), 
e);
+                    }
                 }
+                request.localAddr().setString(socket.getLocalAddr());
             }
 
-            request.localAddr().setString(localAddr);
-
         } else if (actionCode == ActionCode.REQ_REMOTEPORT_ATTRIBUTE) {
 
-            // Get remote port
-            if (remotePort == -1 && (socketRef != 0)) {
-                try {
-                    long sa = Address.get(Socket.APR_REMOTE, socketRef);
-                    Sockaddr addr = Address.getInfo(sa);
-                    remotePort = addr.port;
-                } catch (Exception e) {
-                    log.warn(sm.getString("http11processor.socket.info"), e);
+            if (socketRef == 0) {
+                request.setRemotePort(0);
+            } else {
+                if (socket.getRemotePort() == -1) {
+                    try {
+                        long sa = Address.get(Socket.APR_REMOTE, socketRef);
+                        Sockaddr addr = Address.getInfo(sa);
+                        socket.setRemotePort(addr.port);
+                    } catch (Exception e) {
+                        log.warn(sm.getString("http11processor.socket.info"), 
e);
+                    }
                 }
+                request.setRemotePort(socket.getRemotePort());
             }
-            request.setRemotePort(remotePort);
 
         } else if (actionCode == ActionCode.REQ_LOCALPORT_ATTRIBUTE) {
 
-            // Get local port
-            if (localPort == -1 && (socketRef != 0)) {
-                try {
-                    long sa = Address.get(Socket.APR_LOCAL, socketRef);
-                    Sockaddr addr = Address.getInfo(sa);
-                    localPort = addr.port;
-                } catch (Exception e) {
-                    log.warn(sm.getString("http11processor.socket.info"), e);
+            if (socketRef == 0) {
+                request.setLocalPort(0);
+            } else {
+                if (socket.getLocalPort() == -1) {
+                    try {
+                        long sa = Address.get(Socket.APR_LOCAL, socketRef);
+                        Sockaddr addr = Address.getInfo(sa);
+                        socket.setLocalPort(addr.port);
+                    } catch (Exception e) {
+                        log.warn(sm.getString("http11processor.socket.info"), 
e);
+                    }
                 }
+                request.setLocalPort(socket.getLocalPort());
             }
-            request.setLocalPort(localPort);
 
         } else if (actionCode == ActionCode.REQ_SSL_ATTRIBUTE ) {
 

Modified: tomcat/trunk/java/org/apache/coyote/http11/Http11NioProcessor.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/coyote/http11/Http11NioProcessor.java?rev=1390162&r1=1390161&r2=1390162&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/coyote/http11/Http11NioProcessor.java 
(original)
+++ tomcat/trunk/java/org/apache/coyote/http11/Http11NioProcessor.java Tue Sep 
25 21:36:55 2012
@@ -397,65 +397,88 @@ public class Http11NioProcessor extends 
 
         if (actionCode == ActionCode.REQ_HOST_ADDR_ATTRIBUTE) {
 
-            // Get remote host address
-            if ((remoteAddr == null) && (socket != null)) {
-                InetAddress inetAddr = 
socket.getSocket().getIOChannel().socket().getInetAddress();
-                if (inetAddr != null) {
-                    remoteAddr = inetAddr.getHostAddress();
+            if (socket == null) {
+                request.remoteAddr().recycle();
+            } else {
+                if (socket.getRemoteAddr() == null) {
+                    InetAddress inetAddr = 
socket.getSocket().getIOChannel().socket().getInetAddress();
+                    if (inetAddr != null) {
+                        socket.setRemoteAddr(inetAddr.getHostAddress());
+                    }
                 }
+                request.remoteAddr().setString(socket.getRemoteAddr());
             }
-            request.remoteAddr().setString(remoteAddr);
 
         } else if (actionCode == ActionCode.REQ_LOCAL_NAME_ATTRIBUTE) {
 
-            // Get local host name
-            if ((localName == null) && (socket != null)) {
-                InetAddress inetAddr = 
socket.getSocket().getIOChannel().socket().getLocalAddress();
-                if (inetAddr != null) {
-                    localName = inetAddr.getHostName();
+            if (socket == null) {
+                request.localName().recycle();
+            } else {
+                if (socket.getLocalName() == null) {
+                    InetAddress inetAddr = 
socket.getSocket().getIOChannel().socket().getLocalAddress();
+                    if (inetAddr != null) {
+                        socket.setLocalName(inetAddr.getHostName());
+                    }
                 }
+                request.localName().setString(socket.getLocalName());
             }
-            request.localName().setString(localName);
 
         } else if (actionCode == ActionCode.REQ_HOST_ATTRIBUTE) {
 
-            // Get remote host name
-            if ((remoteHost == null) && (socket != null)) {
-                InetAddress inetAddr = 
socket.getSocket().getIOChannel().socket().getInetAddress();
-                if (inetAddr != null) {
-                    remoteHost = inetAddr.getHostName();
-                }
-                if(remoteHost == null) {
-                    if(remoteAddr != null) {
-                        remoteHost = remoteAddr;
-                    } else { // all we can do is punt
-                        request.remoteHost().recycle();
+            if (socket == null) {
+                request.remoteHost().recycle();
+            } else {
+                if (socket.getRemoteHost() == null) {
+                    InetAddress inetAddr = 
socket.getSocket().getIOChannel().socket().getInetAddress();
+                    if (inetAddr != null) {
+                        socket.setRemoteHost(inetAddr.getHostName());
+                    }
+                    if (socket.getRemoteHost() == null) {
+                        if (socket.getRemoteAddr() == null &&
+                                inetAddr != null) {
+                            socket.setRemoteAddr(inetAddr.getHostAddress());
+                        }
+                        if (socket.getRemoteAddr() != null) {
+                            socket.setRemoteHost(socket.getRemoteAddr());
+                        }
                     }
                 }
+                request.remoteHost().setString(socket.getRemoteHost());
             }
-            request.remoteHost().setString(remoteHost);
 
         } else if (actionCode == ActionCode.REQ_LOCAL_ADDR_ATTRIBUTE) {
 
-            if (localAddr == null) {
-                localAddr = 
socket.getSocket().getIOChannel().socket().getLocalAddress().getHostAddress();
+            if (socket == null) {
+                request.localAddr().recycle();
+            } else {
+                if (socket.getLocalAddr() == null) {
+                    socket.setLocalAddr(
+                            
socket.getSocket().getIOChannel().socket().getLocalAddress().getHostAddress());
+                }
+                request.localAddr().setString(socket.getLocalAddr());
             }
 
-            request.localAddr().setString(localAddr);
-
         } else if (actionCode == ActionCode.REQ_REMOTEPORT_ATTRIBUTE) {
 
-            if ((remotePort == -1 ) && (socket !=null)) {
-                remotePort = 
socket.getSocket().getIOChannel().socket().getPort();
+            if (socket == null) {
+                request.setRemotePort(0);
+            } else {
+                if (socket.getRemotePort() == -1) {
+                    
socket.setRemotePort(socket.getSocket().getIOChannel().socket().getPort());
+                }
+                request.setRemotePort(socket.getRemotePort());
             }
-            request.setRemotePort(remotePort);
 
         } else if (actionCode == ActionCode.REQ_LOCALPORT_ATTRIBUTE) {
 
-            if ((localPort == -1 ) && (socket !=null)) {
-                localPort = 
socket.getSocket().getIOChannel().socket().getLocalPort();
+            if (socket == null) {
+                request.setLocalPort(0);
+            } else {
+                if (socket.getLocalPort() == -1) {
+                    
socket.setLocalPort(socket.getSocket().getIOChannel().socket().getLocalPort());
+                }
+                request.setLocalPort(socket.getLocalPort());
             }
-            request.setLocalPort(localPort);
 
         } else if (actionCode == ActionCode.REQ_SSL_ATTRIBUTE ) {
 

Modified: tomcat/trunk/java/org/apache/coyote/http11/Http11Processor.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/coyote/http11/Http11Processor.java?rev=1390162&r1=1390161&r2=1390162&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/coyote/http11/Http11Processor.java (original)
+++ tomcat/trunk/java/org/apache/coyote/http11/Http11Processor.java Tue Sep 25 
21:36:55 2012
@@ -273,61 +273,88 @@ public class Http11Processor extends Abs
 
         } else if (actionCode == ActionCode.REQ_HOST_ADDR_ATTRIBUTE) {
 
-            if ((remoteAddr == null) && (socket != null)) {
-                InetAddress inetAddr = socket.getSocket().getInetAddress();
-                if (inetAddr != null) {
-                    remoteAddr = inetAddr.getHostAddress();
+            if (socket == null) {
+                request.remoteAddr().recycle();
+            } else {
+                if (socket.getRemoteAddr() == null) {
+                    InetAddress inetAddr = socket.getSocket().getInetAddress();
+                    if (inetAddr != null) {
+                        socket.setRemoteAddr(inetAddr.getHostAddress());
+                    }
                 }
+                request.remoteAddr().setString(socket.getRemoteAddr());
             }
-            request.remoteAddr().setString(remoteAddr);
 
         } else if (actionCode == ActionCode.REQ_LOCAL_NAME_ATTRIBUTE) {
 
-            if ((localName == null) && (socket != null)) {
-                InetAddress inetAddr = socket.getSocket().getLocalAddress();
-                if (inetAddr != null) {
-                    localName = inetAddr.getHostName();
+            if (socket == null) {
+                request.localName().recycle();
+            } else {
+                if (socket.getLocalName() == null) {
+                    InetAddress inetAddr = 
socket.getSocket().getLocalAddress();
+                    if (inetAddr != null) {
+                        socket.setLocalName(inetAddr.getHostName());
+                    }
                 }
+                request.localName().setString(socket.getLocalName());
             }
-            request.localName().setString(localName);
 
         } else if (actionCode == ActionCode.REQ_HOST_ATTRIBUTE) {
 
-            if ((remoteHost == null) && (socket != null)) {
-                InetAddress inetAddr = socket.getSocket().getInetAddress();
-                if (inetAddr != null) {
-                    remoteHost = inetAddr.getHostName();
-                }
-                if(remoteHost == null) {
-                    if(remoteAddr != null) {
-                        remoteHost = remoteAddr;
-                    } else { // all we can do is punt
-                        request.remoteHost().recycle();
+            if (socket == null) {
+                request.remoteHost().recycle();
+            } else {
+                if (socket.getRemoteHost() == null) {
+                    InetAddress inetAddr = socket.getSocket().getInetAddress();
+                    if (inetAddr != null) {
+                        socket.setRemoteHost(inetAddr.getHostName());
+                    }
+                    if (socket.getRemoteHost() == null) {
+                        if (socket.getRemoteAddr() == null &&
+                                inetAddr != null) {
+                            socket.setRemoteAddr(inetAddr.getHostAddress());
+                        }
+                        if (socket.getRemoteAddr() != null) {
+                            socket.setRemoteHost(socket.getRemoteAddr());
+                        }
                     }
                 }
+                request.remoteHost().setString(socket.getRemoteHost());
             }
-            request.remoteHost().setString(remoteHost);
 
         } else if (actionCode == ActionCode.REQ_LOCAL_ADDR_ATTRIBUTE) {
 
-            if (localAddr == null)
-               localAddr = 
socket.getSocket().getLocalAddress().getHostAddress();
-
-            request.localAddr().setString(localAddr);
+            if (socket == null) {
+                request.localAddr().recycle();
+            } else {
+                if (socket.getLocalAddr() == null) {
+                    socket.setLocalAddr(
+                            
socket.getSocket().getLocalAddress().getHostAddress());
+                }
+                request.localAddr().setString(socket.getLocalAddr());
+            }
 
         } else if (actionCode == ActionCode.REQ_REMOTEPORT_ATTRIBUTE) {
 
-            if ((remotePort == -1 ) && (socket !=null)) {
-                remotePort = socket.getSocket().getPort();
+            if (socket == null) {
+                request.setRemotePort(0);
+            } else {
+                if (socket.getRemotePort() == -1) {
+                    socket.setRemotePort(socket.getSocket().getPort());
+                }
+                request.setRemotePort(socket.getRemotePort());
             }
-            request.setRemotePort(remotePort);
 
         } else if (actionCode == ActionCode.REQ_LOCALPORT_ATTRIBUTE) {
 
-            if ((localPort == -1 ) && (socket !=null)) {
-                localPort = socket.getSocket().getLocalPort();
+            if (socket == null) {
+                request.setLocalPort(0);
+            } else {
+                if (socket.getLocalPort() == -1) {
+                    socket.setLocalPort(socket.getSocket().getLocalPort());
+                }
+                request.setLocalPort(socket.getLocalPort());
             }
-            request.setLocalPort(localPort);
 
         } else if (actionCode == ActionCode.REQ_SSL_CERTIFICATE) {
             if( sslSupport != null) {

Modified: tomcat/trunk/java/org/apache/tomcat/util/net/SocketWrapper.java
URL: 
http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/net/SocketWrapper.java?rev=1390162&r1=1390161&r2=1390162&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/tomcat/util/net/SocketWrapper.java (original)
+++ tomcat/trunk/java/org/apache/tomcat/util/net/SocketWrapper.java Tue Sep 25 
21:36:55 2012
@@ -27,6 +27,16 @@ public class SocketWrapper<E> {
     protected volatile int keepAliveLeft = 100;
     protected boolean async = false;
     protected boolean keptAlive = false;
+    /*
+     * Following cached for speed / reduced GC
+     */
+    private int localPort = -1;
+    private String localName = null;
+    private String localAddr = null;
+    private int remotePort = -1;
+    private String remoteHost = null;
+    private String remoteAddr = null;
+
 
     public SocketWrapper(E socket) {
         this.socket = socket;
@@ -49,4 +59,16 @@ public class SocketWrapper<E> {
     public int decrementKeepAlive() { return (--keepAliveLeft);}
     public boolean isKeptAlive() {return keptAlive;}
     public void setKeptAlive(boolean keptAlive) {this.keptAlive = keptAlive;}
+    public int getLocalPort() { return localPort; }
+    public void setLocalPort(int localPort) {this.localPort = localPort; }
+    public String getLocalName() { return localName; }
+    public void setLocalName(String localName) {this.localName = localName; }
+    public String getLocalAddr() { return localAddr; }
+    public void setLocalAddr(String localAddr) {this.localAddr = localAddr; }
+    public int getRemotePort() { return remotePort; }
+    public void setRemotePort(int remotePort) {this.remotePort = remotePort; }
+    public String getRemoteHost() { return remoteHost; }
+    public void setRemoteHost(String remoteHost) {this.remoteHost = 
remoteHost; }
+    public String getRemoteAddr() { return remoteAddr; }
+    public void setRemoteAddr(String remoteAddr) {this.remoteAddr = 
remoteAddr; }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to