Resolved issue: NETTY-156 Improve NIO worker performance by removing unnecessary SelectionKey access

* Applied patch by Wade Poziombka
* Reduced the call to SelectionKey.readyOps() even more
This commit is contained in:
Trustin Lee 2009-05-21 10:28:05 +00:00
parent 4d81a66e26
commit 77742f5cf9

View File

@ -250,10 +250,14 @@ class NioWorker implements Runnable {
SelectionKey k = i.next(); SelectionKey k = i.next();
i.remove(); i.remove();
try { try {
if (k.isReadable()) { int readyOps = k.readyOps();
read(k); if ((readyOps & SelectionKey.OP_READ) != 0) {
if (!read(k)) {
// Connection already closed - no need to handle write.
continue;
} }
if (k.isWritable()) { }
if ((readyOps & SelectionKey.OP_WRITE) != 0) {
write(k); write(k);
} }
} catch (CancelledKeyException e) { } catch (CancelledKeyException e) {
@ -262,7 +266,7 @@ class NioWorker implements Runnable {
} }
} }
private static void read(SelectionKey k) { private static boolean read(SelectionKey k) {
ScatteringByteChannel ch = (ScatteringByteChannel) k.channel(); ScatteringByteChannel ch = (ScatteringByteChannel) k.channel();
NioSocketChannel channel = (NioSocketChannel) k.attachment(); NioSocketChannel channel = (NioSocketChannel) k.attachment();
@ -301,7 +305,10 @@ class NioWorker implements Runnable {
if (ret < 0 || failure) { if (ret < 0 || failure) {
close(k); close(k);
return false;
} }
return true;
} }
private static void write(SelectionKey k) { private static void write(SelectionKey k) {