From 966004332b0d1074631e52fe9cc2a2c89f59450f Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Wed, 27 Jan 2010 06:55:10 +0000 Subject: [PATCH] Small optimizations on DirectBufferPool --- .../channel/socket/nio/DirectBufferPool.java | 27 ++++++++----------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/jboss/netty/channel/socket/nio/DirectBufferPool.java b/src/main/java/org/jboss/netty/channel/socket/nio/DirectBufferPool.java index 9a92911140..db395bb58c 100644 --- a/src/main/java/org/jboss/netty/channel/socket/nio/DirectBufferPool.java +++ b/src/main/java/org/jboss/netty/channel/socket/nio/DirectBufferPool.java @@ -27,15 +27,13 @@ import org.jboss.netty.buffer.ChannelBuffer; */ final class DirectBufferPool { - private final SoftReference[] pool; - - DirectBufferPool() { - this(4); - } + private static final int POOL_SIZE = 4; @SuppressWarnings("unchecked") - DirectBufferPool(int poolSize) { - pool = new SoftReference[poolSize]; + private final SoftReference[] pool = new SoftReference[POOL_SIZE]; + + DirectBufferPool() { + super(); } final ByteBuffer acquire(ChannelBuffer src) { @@ -46,7 +44,7 @@ final class DirectBufferPool { } private final ByteBuffer acquire(int size) { - for (int i = 0; i < pool.length; i ++) { + for (int i = 0; i < POOL_SIZE; i ++) { SoftReference ref = pool[i]; if (ref == null) { continue; @@ -75,11 +73,7 @@ final class DirectBufferPool { } final void release(ByteBuffer buffer) { - if (buffer == null) { - return; - } - - for (int i = 0; i < pool.length; i ++) { + for (int i = 0; i < POOL_SIZE; i ++) { SoftReference ref = pool[i]; if (ref == null || ref.get() == null) { pool[i] = new SoftReference(buffer); @@ -88,7 +82,8 @@ final class DirectBufferPool { } // pool is full - replace one - for (int i = 0; i< pool.length; i ++) { + final int capacity = buffer.capacity(); + for (int i = 0; i< POOL_SIZE; i ++) { SoftReference ref = pool[i]; ByteBuffer pooled = ref.get(); if (pooled == null) { @@ -96,14 +91,14 @@ final class DirectBufferPool { continue; } - if (pooled.capacity() < buffer.capacity()) { + if (pooled.capacity() < capacity) { pool[i] = new SoftReference(buffer); return; } } } - static final int normalizeCapacity(int capacity) { + private static final int normalizeCapacity(int capacity) { // Normalize to multiple of 4096. // Strictly speaking, 4096 should be normalized to 4096, // but it becomes 8192 to keep the calculation simplistic.