DirectBufferPool should be static because it uses thread local now

This commit is contained in:
Trustin Lee 2010-01-28 13:16:46 +00:00
parent 4bf4d5f814
commit 84cf41890c
3 changed files with 20 additions and 23 deletions

View File

@ -29,7 +29,7 @@ final class DirectBufferPool {
private static final int POOL_SIZE = 4;
private final ThreadLocal<SoftReference<ByteBuffer>[]> pool =
private static final ThreadLocal<SoftReference<ByteBuffer>[]> pool =
new ThreadLocal<SoftReference<ByteBuffer>[]>() {
@Override
@SuppressWarnings("unchecked")
@ -40,19 +40,15 @@ final class DirectBufferPool {
};
DirectBufferPool() {
super();
}
final ByteBuffer acquire(ChannelBuffer src) {
static final ByteBuffer acquire(ChannelBuffer src) {
ByteBuffer dst = acquire(src.readableBytes());
src.getBytes(src.readerIndex(), dst);
dst.rewind();
return dst;
}
final ByteBuffer acquire(int size) {
final SoftReference<ByteBuffer>[] pool = this.pool.get();
static final ByteBuffer acquire(int size) {
final SoftReference<ByteBuffer>[] pool = DirectBufferPool.pool.get();
for (int i = 0; i < POOL_SIZE; i ++) {
SoftReference<ByteBuffer> ref = pool[i];
if (ref == null) {
@ -81,8 +77,8 @@ final class DirectBufferPool {
return buf;
}
final void release(ByteBuffer buffer) {
final SoftReference<ByteBuffer>[] pool = this.pool.get();
static final void release(ByteBuffer buffer) {
final SoftReference<ByteBuffer>[] pool = DirectBufferPool.pool.get();
for (int i = 0; i < POOL_SIZE; i ++) {
SoftReference<ByteBuffer> ref = pool[i];
if (ref == null || ref.get() == null) {
@ -114,4 +110,8 @@ final class DirectBufferPool {
// but it becomes 8192 to keep the calculation simplistic.
return (capacity & 0xfffff000) + 0x1000;
}
private DirectBufferPool() {
super();
}
}

View File

@ -127,8 +127,6 @@ class NioDatagramWorker implements Runnable {
private volatile int cancelledKeys; // should use AtomicInteger but we just need approximation
private final DirectBufferPool directBufferPool = new DirectBufferPool();
/**
* Sole constructor.
*
@ -476,7 +474,7 @@ class NioDatagramWorker implements Runnable {
channel.currentWriteBuffer = buf = origBuf.toByteBuffer();
channel.currentWriteBufferIsPooled = false;
} else {
channel.currentWriteBuffer = buf = directBufferPool.acquire(origBuf);
channel.currentWriteBuffer = buf = DirectBufferPool.acquire(origBuf);
channel.currentWriteBufferIsPooled = true;
}
} else {
@ -508,7 +506,7 @@ class NioDatagramWorker implements Runnable {
if (localWrittenBytes > 0) {
// Successful write - proceed to the next message.
if (channel.currentWriteBufferIsPooled) {
directBufferPool.release(buf);
DirectBufferPool.release(buf);
}
ChannelFuture future = evt.getFuture();
@ -526,7 +524,7 @@ class NioDatagramWorker implements Runnable {
// Doesn't need a user attention - ignore.
} catch (final Throwable t) {
if (channel.currentWriteBufferIsPooled) {
directBufferPool.release(buf);
DirectBufferPool.release(buf);
}
ChannelFuture future = evt.getFuture();
channel.currentWriteEvent = null;
@ -636,7 +634,7 @@ class NioDatagramWorker implements Runnable {
cause = new ClosedChannelException();
}
if (channel.currentWriteBufferIsPooled) {
directBufferPool.release(buf);
DirectBufferPool.release(buf);
}
ChannelFuture future = evt.getFuture();

View File

@ -78,7 +78,6 @@ class NioWorker implements Runnable {
private final Queue<Runnable> registerTaskQueue = new LinkedTransferQueue<Runnable>();
private final Queue<Runnable> writeTaskQueue = new LinkedTransferQueue<Runnable>();
private volatile int cancelledKeys; // should use AtomicInteger but we just need approximation
private final DirectBufferPool directBufferPool = new DirectBufferPool();
NioWorker(int bossId, int id, Executor executor) {
this.bossId = bossId;
@ -317,7 +316,7 @@ class NioWorker implements Runnable {
final ByteBuffer directBuffer;
final boolean fromPool = !buffer.isDirect();
if (fromPool) {
directBuffer = directBufferPool.acquire(buffer.writableBytes());
directBuffer = DirectBufferPool.acquire(buffer.writableBytes());
} else {
directBuffer = buffer.toByteBuffer();
}
@ -341,7 +340,7 @@ class NioWorker implements Runnable {
if (fromPool) {
directBuffer.flip();
buffer.writeBytes(directBuffer);
directBufferPool.release(directBuffer);
DirectBufferPool.release(directBuffer);
} else {
// no need to copy: directBuffer is just a view to buffer.
buffer.writerIndex(buffer.writerIndex() + readBytes);
@ -408,7 +407,7 @@ class NioWorker implements Runnable {
channel.currentWriteBuffer = buf = origBuf.toByteBuffer();
channel.currentWriteBufferIsPooled = false;
} else {
channel.currentWriteBuffer = buf = directBufferPool.acquire(origBuf);
channel.currentWriteBuffer = buf = DirectBufferPool.acquire(origBuf);
channel.currentWriteBufferIsPooled = true;
}
} else {
@ -427,7 +426,7 @@ class NioWorker implements Runnable {
if (!buf.hasRemaining()) {
// Successful write - proceed to the next message.
if (channel.currentWriteBufferIsPooled) {
directBufferPool.release(buf);
DirectBufferPool.release(buf);
}
ChannelFuture future = evt.getFuture();
@ -445,7 +444,7 @@ class NioWorker implements Runnable {
// Doesn't need a user attention - ignore.
} catch (Throwable t) {
if (channel.currentWriteBufferIsPooled) {
directBufferPool.release(buf);
DirectBufferPool.release(buf);
}
ChannelFuture future = evt.getFuture();
channel.currentWriteEvent = null;
@ -563,7 +562,7 @@ class NioWorker implements Runnable {
}
if (channel.currentWriteBufferIsPooled) {
directBufferPool.release(buf);
DirectBufferPool.release(buf);
}
ChannelFuture future = evt.getFuture();