From 43a603cfde0cc45ca71fe64655b1fb3c2db1822a Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Tue, 23 Feb 2010 00:56:04 +0000 Subject: [PATCH] * Removed unnecessary conditional * Optimized the use of memory bandwidth in NioWorker.read --- .../buffer/ByteBufferBackedChannelBuffer.java | 2 +- .../netty/channel/socket/nio/NioWorker.java | 65 ++++++++----------- .../socket/nio/SocketReceiveBufferPool.java | 7 +- 3 files changed, 30 insertions(+), 44 deletions(-) diff --git a/src/main/java/org/jboss/netty/buffer/ByteBufferBackedChannelBuffer.java b/src/main/java/org/jboss/netty/buffer/ByteBufferBackedChannelBuffer.java index 0e59a5794a..4bae872fce 100644 --- a/src/main/java/org/jboss/netty/buffer/ByteBufferBackedChannelBuffer.java +++ b/src/main/java/org/jboss/netty/buffer/ByteBufferBackedChannelBuffer.java @@ -204,7 +204,7 @@ public class ByteBufferBackedChannelBuffer extends AbstractChannelBuffer { return; } - if (!buffer.isReadOnly() && buffer.hasArray()) { + if (buffer.hasArray()) { out.write( buffer.array(), index + buffer.arrayOffset(), diff --git a/src/main/java/org/jboss/netty/channel/socket/nio/NioWorker.java b/src/main/java/org/jboss/netty/channel/socket/nio/NioWorker.java index 4eb649a062..fbb2c612a5 100644 --- a/src/main/java/org/jboss/netty/channel/socket/nio/NioWorker.java +++ b/src/main/java/org/jboss/netty/channel/socket/nio/NioWorker.java @@ -306,62 +306,49 @@ class NioWorker implements Runnable { } private boolean read(SelectionKey k) { - SocketChannel ch = (SocketChannel) k.channel(); - NioSocketChannel channel = (NioSocketChannel) k.attachment(); + final SocketChannel ch = (SocketChannel) k.channel(); + final NioSocketChannel channel = (NioSocketChannel) k.attachment(); - ReceiveBufferSizePredictor predictor = + final ReceiveBufferSizePredictor predictor = channel.getConfig().getReceiveBufferSizePredictor(); - ChannelBufferFactory bufferFactory = - channel.getConfig().getBufferFactory(); - - ChannelBuffer buffer = - bufferFactory.getBuffer(predictor.nextReceiveBufferSize()); + final int predictedRecvBufSize = predictor.nextReceiveBufferSize(); int ret = 0; int readBytes = 0; boolean failure = true; - if (buffer.isDirect()) { - try { - while ((ret = buffer.writeBytes(ch, buffer.writableBytes())) > 0) { - readBytes += ret; - if (!buffer.writable()) { - break; - } + ByteBuffer bb = recvBufferPool.acquire(predictedRecvBufSize); + try { + while ((ret = ch.read(bb)) > 0) { + readBytes += ret; + if (!bb.hasRemaining()) { + break; } - failure = false; - } catch (ClosedChannelException e) { - // Can happen, and does not need a user attention. - } catch (Throwable t) { - fireExceptionCaught(channel, t); - } - } else { - ByteBuffer bb = recvBufferPool.acquire(buffer.writableBytes()); - try { - while ((ret = ch.read(bb)) > 0) { - readBytes += ret; - if (!bb.hasRemaining()) { - break; - } - } - failure = false; - } catch (ClosedChannelException e) { - // Can happen, and does not need a user attention. - } catch (Throwable t) { - fireExceptionCaught(channel, t); - } finally { - bb.flip(); - buffer.writeBytes(bb); - recvBufferPool.release(bb); } + failure = false; + } catch (ClosedChannelException e) { + // Can happen, and does not need a user attention. + } catch (Throwable t) { + fireExceptionCaught(channel, t); } if (readBytes > 0) { + bb.flip(); + + final ChannelBufferFactory bufferFactory = + channel.getConfig().getBufferFactory(); + final ChannelBuffer buffer = bufferFactory.getBuffer( + bb.order(bufferFactory.getDefaultOrder())); + + recvBufferPool.release(bb); + // Update the predictor. predictor.previousReceiveBufferSize(readBytes); // Fire the event. fireMessageReceived(channel, buffer); + } else { + recvBufferPool.release(bb); } if (ret < 0 || failure) { diff --git a/src/main/java/org/jboss/netty/channel/socket/nio/SocketReceiveBufferPool.java b/src/main/java/org/jboss/netty/channel/socket/nio/SocketReceiveBufferPool.java index f8db7d83fd..5bc7d98608 100644 --- a/src/main/java/org/jboss/netty/channel/socket/nio/SocketReceiveBufferPool.java +++ b/src/main/java/org/jboss/netty/channel/socket/nio/SocketReceiveBufferPool.java @@ -25,7 +25,7 @@ import java.nio.ByteBuffer; */ final class SocketReceiveBufferPool { - private static final int POOL_SIZE = 4; + private static final int POOL_SIZE = 8; @SuppressWarnings("unchecked") private final SoftReference[] pool = new SoftReference[POOL_SIZE]; @@ -54,13 +54,12 @@ final class SocketReceiveBufferPool { pool[i] = null; - buf.rewind(); - buf.limit(size); + buf.clear(); return buf; } ByteBuffer buf = ByteBuffer.allocateDirect(normalizeCapacity(size)); - buf.limit(size); + buf.clear(); return buf; }