Class CachedBufferAllocator
- java.lang.Object
-
- com.google.code.yanf4j.buffer.CachedBufferAllocator
-
- All Implemented Interfaces:
IoBufferAllocator
public class CachedBufferAllocator extends java.lang.Object implements IoBufferAllocator
AnIoBufferAllocatorthat caches the buffers which are likely to be reused during auto-expansion of the buffers.In
SimpleBufferAllocator, the underlyingByteBufferof theIoBufferis reallocated on its capacity change, which means the newly allocated biggerByteBufferreplaces the old smallByteBuffer. Consequently, the oldByteBufferis marked for garbage collection.It's not a problem in most cases as long as the capacity change doesn't happen frequently. However, once it happens too often, it burdens the VM and the cost of filling the newly allocated
ByteBufferwithNULsurpass the cost of accessing the cache. In 2 dual-core Opteron Italy 270 processors,CachedBufferAllocatoroutperformedSimpleBufferAllocatorin the following situation:- when a 32 bytes buffer is expanded 4 or more times,
- when a 64 bytes buffer is expanded 4 or more times,
- when a 128 bytes buffer is expanded 2 or more times,
- and when a 256 bytes or bigger buffer is expanded 1 or more times.
CachedBufferAllocatorusesThreadLocalto store the cached buffer, allocates buffers whose capacity is power of 2 only and provides performance advantage ifIoBuffer.free()is called properly.- Version:
- $Rev: 671827 $, $Date: 2008-06-26 10:49:48 +0200 (Thu, 26 Jun 2008) $
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classCachedBufferAllocator.CachedBuffer
-
Field Summary
Fields Modifier and Type Field Description private static intDEFAULT_MAX_CACHED_BUFFER_SIZEprivate static intDEFAULT_MAX_POOL_SIZEprivate java.lang.ThreadLocal<java.util.Map<java.lang.Integer,java.util.Queue<CachedBufferAllocator.CachedBuffer>>>directBuffersprivate java.lang.ThreadLocal<java.util.Map<java.lang.Integer,java.util.Queue<CachedBufferAllocator.CachedBuffer>>>heapBuffersprivate intmaxCachedBufferSizeprivate intmaxPoolSize
-
Constructor Summary
Constructors Constructor Description CachedBufferAllocator()Creates a new instance with the default parameters (#DEFAULT_MAX_POOL_SIZE and #DEFAULT_MAX_CACHED_BUFFER_SIZE).CachedBufferAllocator(int maxPoolSize, int maxCachedBufferSize)Creates a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description IoBufferallocate(int requestedCapacity, boolean direct)Returns the buffer which is capable of the specified size.java.nio.ByteBufferallocateNioBuffer(int capacity, boolean direct)Returns the NIO buffer which is capable of the specified size.voiddispose()Dispose of this allocator.intgetMaxCachedBufferSize()Returns the maximum capacity of a cached buffer.intgetMaxPoolSize()Returns the maximum number of buffers with the same capacity per thread.private java.util.Map<java.lang.Integer,java.util.Queue<CachedBufferAllocator.CachedBuffer>>newPoolMap()IoBufferwrap(java.nio.ByteBuffer nioBuffer)Wraps the specified NIOByteBufferinto MINA buffer.
-
-
-
Field Detail
-
DEFAULT_MAX_POOL_SIZE
private static final int DEFAULT_MAX_POOL_SIZE
- See Also:
- Constant Field Values
-
DEFAULT_MAX_CACHED_BUFFER_SIZE
private static final int DEFAULT_MAX_CACHED_BUFFER_SIZE
- See Also:
- Constant Field Values
-
maxPoolSize
private final int maxPoolSize
-
maxCachedBufferSize
private final int maxCachedBufferSize
-
heapBuffers
private final java.lang.ThreadLocal<java.util.Map<java.lang.Integer,java.util.Queue<CachedBufferAllocator.CachedBuffer>>> heapBuffers
-
directBuffers
private final java.lang.ThreadLocal<java.util.Map<java.lang.Integer,java.util.Queue<CachedBufferAllocator.CachedBuffer>>> directBuffers
-
-
Constructor Detail
-
CachedBufferAllocator
public CachedBufferAllocator()
Creates a new instance with the default parameters (#DEFAULT_MAX_POOL_SIZE and #DEFAULT_MAX_CACHED_BUFFER_SIZE).
-
CachedBufferAllocator
public CachedBufferAllocator(int maxPoolSize, int maxCachedBufferSize)Creates a new instance.- Parameters:
maxPoolSize- the maximum number of buffers with the same capacity per thread. 0 disables this limitation.maxCachedBufferSize- the maximum capacity of a cached buffer. A buffer whose capacity is bigger than this value is not pooled. 0 disables this limitation.
-
-
Method Detail
-
getMaxPoolSize
public int getMaxPoolSize()
Returns the maximum number of buffers with the same capacity per thread. 0 means 'no limitation'.
-
getMaxCachedBufferSize
public int getMaxCachedBufferSize()
Returns the maximum capacity of a cached buffer. A buffer whose capacity is bigger than this value is not pooled. 0 means 'no limitation'.
-
newPoolMap
private java.util.Map<java.lang.Integer,java.util.Queue<CachedBufferAllocator.CachedBuffer>> newPoolMap()
-
allocate
public IoBuffer allocate(int requestedCapacity, boolean direct)
Description copied from interface:IoBufferAllocatorReturns the buffer which is capable of the specified size.- Specified by:
allocatein interfaceIoBufferAllocator- Parameters:
requestedCapacity- the capacity of the bufferdirect- true to get a direct buffer, false to get a heap buffer.
-
allocateNioBuffer
public java.nio.ByteBuffer allocateNioBuffer(int capacity, boolean direct)Description copied from interface:IoBufferAllocatorReturns the NIO buffer which is capable of the specified size.- Specified by:
allocateNioBufferin interfaceIoBufferAllocator- Parameters:
capacity- the capacity of the bufferdirect- true to get a direct buffer, false to get a heap buffer.
-
wrap
public IoBuffer wrap(java.nio.ByteBuffer nioBuffer)
Description copied from interface:IoBufferAllocatorWraps the specified NIOByteBufferinto MINA buffer.- Specified by:
wrapin interfaceIoBufferAllocator
-
dispose
public void dispose()
Description copied from interface:IoBufferAllocatorDispose of this allocator.- Specified by:
disposein interfaceIoBufferAllocator
-
-