Class CachedBufferAllocator
- All Implemented Interfaces:
IoBufferAllocator
IoBufferAllocator that caches the buffers which are likely to
be reused during auto-expansion of the buffers.
In SimpleBufferAllocator, the underlying ByteBuffer of
the IoBuffer is reallocated on its capacity change, which means
the newly allocated bigger ByteBuffer replaces the old small
ByteBuffer. Consequently, the old ByteBuffer is 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 ByteBuffer with
NUL surpass the cost of accessing the cache. In 2 dual-core
Opteron Italy 270 processors, CachedBufferAllocator outperformed
SimpleBufferAllocator in 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.
CachedBufferAllocator uses ThreadLocal to store the cached
buffer, allocates buffers whose capacity is power of 2 only and provides
performance advantage if IoBuffer.free() is called properly.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final intprivate static final intprivate final ThreadLocal<Map<Integer, Queue<CachedBufferAllocator.CachedBuffer>>> private final ThreadLocal<Map<Integer, Queue<CachedBufferAllocator.CachedBuffer>>> private final intprivate final int -
Constructor Summary
ConstructorsConstructorDescriptionCreates 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
Modifier and TypeMethodDescriptionallocate(int requestedCapacity, boolean direct) Returns the buffer which is capable of the specified size.allocateNioBuffer(int capacity, boolean direct) Returns the NIO buffer which is capable of the specified size.voiddispose()Dispose of this allocator.intint(package private) Map<Integer, Queue<CachedBufferAllocator.CachedBuffer>> wrap(ByteBuffer nioBuffer) Wraps the specified NIOByteBufferinto MINA buffer.
-
Field Details
-
DEFAULT_MAX_POOL_SIZE
private static final int DEFAULT_MAX_POOL_SIZE- See Also:
-
DEFAULT_MAX_CACHED_BUFFER_SIZE
private static final int DEFAULT_MAX_CACHED_BUFFER_SIZE- See Also:
-
maxPoolSize
private final int maxPoolSize -
maxCachedBufferSize
private final int maxCachedBufferSize -
heapBuffers
-
directBuffers
-
-
Constructor Details
-
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.0disables this limitation.maxCachedBufferSize- the maximum capacity of a cached buffer. A buffer whose capacity is bigger than this value is not pooled.0disables this limitation.
-
-
Method Details
-
getMaxPoolSize
public int getMaxPoolSize()- Returns:
- the maximum number of buffers with the same capacity per thread.
0means '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.
0means 'no limitation'.
-
newPoolMap
Map<Integer, Queue<CachedBufferAllocator.CachedBuffer>> newPoolMap() -
allocate
Returns the buffer which is capable of the specified size.- Specified by:
allocatein interfaceIoBufferAllocator- Parameters:
requestedCapacity- the capacity of the bufferdirect-trueto get a direct buffer,falseto get a heap buffer.- Returns:
- The allocated
IoBuffer
-
allocateNioBuffer
Returns the NIO buffer which is capable of the specified size.- Specified by:
allocateNioBufferin interfaceIoBufferAllocator- Parameters:
capacity- the capacity of the bufferdirect-trueto get a direct buffer,falseto get a heap buffer.- Returns:
- The allocated
ByteBuffer
-
wrap
Wraps the specified NIOByteBufferinto MINA buffer.- Specified by:
wrapin interfaceIoBufferAllocator- Parameters:
nioBuffer- TheByteBufferto wrap- Returns:
- The
IoBufferwrapping theByteBuffer
-
dispose
public void dispose()Dispose of this allocator.- Specified by:
disposein interfaceIoBufferAllocator
-