Author: markt Date: Sat Sep 22 20:03:25 2012 New Revision: 1388890 URL: http://svn.apache.org/viewvc?rev=1388890&view=rev Log: Reduce GC when NIO connector is under load. Results in a small performance improvement. KeyReferences and finalizer references were accounting for 30%+ of the heap during my load tests before this patch.
Modified: tomcat/trunk/java/org/apache/tomcat/util/net/NioBlockingSelector.java Modified: tomcat/trunk/java/org/apache/tomcat/util/net/NioBlockingSelector.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/tomcat/util/net/NioBlockingSelector.java?rev=1388890&r1=1388889&r2=1388890&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/tomcat/util/net/NioBlockingSelector.java (original) +++ tomcat/trunk/java/org/apache/tomcat/util/net/NioBlockingSelector.java Sat Sep 22 20:03:25 2012 @@ -26,6 +26,7 @@ import java.nio.channels.SelectionKey; import java.nio.channels.Selector; import java.nio.channels.SocketChannel; import java.util.Iterator; +import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; @@ -42,6 +43,9 @@ public class NioBlockingSelector { private static int threadCounter = 0; + private Queue<KeyReference> keyReferenceQueue = + new ConcurrentLinkedQueue<>(); + protected Selector sharedSelector; protected BlockPoller poller; @@ -82,7 +86,10 @@ public class NioBlockingSelector { throws IOException { SelectionKey key = socket.getIOChannel().keyFor(socket.getPoller().getSelector()); if ( key == null ) throw new IOException("Key no longer registered"); - KeyReference reference = new KeyReference(); + KeyReference reference = keyReferenceQueue.poll(); + if (reference == null) { + reference = new KeyReference(); + } KeyAttachment att = (KeyAttachment) key.attachment(); int written = 0; boolean timedout = false; @@ -131,6 +138,7 @@ public class NioBlockingSelector { poller.cancelKey(reference.key); } reference.key = null; + keyReferenceQueue.add(reference); } return written; } @@ -150,7 +158,10 @@ public class NioBlockingSelector { public int read(ByteBuffer buf, NioChannel socket, long readTimeout) throws IOException { SelectionKey key = socket.getIOChannel().keyFor(socket.getPoller().getSelector()); if ( key == null ) throw new IOException("Key no longer registered"); - KeyReference reference = new KeyReference(); + KeyReference reference = keyReferenceQueue.poll(); + if (reference == null) { + reference = new KeyReference(); + } KeyAttachment att = (KeyAttachment) key.attachment(); int read = 0; boolean timedout = false; @@ -195,6 +206,7 @@ public class NioBlockingSelector { poller.cancelKey(reference.key); } reference.key = null; + keyReferenceQueue.add(reference); } return read; } --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org