public class CachedBufferAllocator extends Object implements 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:
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.
Constructor and 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.
|
Modifier and Type | Method and Description |
---|---|
IoBuffer |
allocate(int requestedCapacity,
boolean direct)
Returns the buffer which is capable of the specified size.
|
ByteBuffer |
allocateNioBuffer(int capacity,
boolean direct)
Returns the NIO buffer which is capable of the specified size.
|
void |
dispose()
Dispose of this allocator.
|
int |
getMaxCachedBufferSize() |
int |
getMaxPoolSize() |
IoBuffer |
wrap(ByteBuffer nioBuffer)
Wraps the specified NIO
ByteBuffer into MINA buffer. |
public CachedBufferAllocator()
public CachedBufferAllocator(int maxPoolSize, int maxCachedBufferSize)
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.public int getMaxPoolSize()
public int getMaxCachedBufferSize()
public IoBuffer allocate(int requestedCapacity, boolean direct)
allocate
in interface IoBufferAllocator
requestedCapacity
- the capacity of the bufferdirect
- true to get a direct buffer,
false to get a heap buffer.IoBuffer
public ByteBuffer allocateNioBuffer(int capacity, boolean direct)
allocateNioBuffer
in interface IoBufferAllocator
capacity
- the capacity of the bufferdirect
- true to get a direct buffer,
false to get a heap buffer.ByteBuffer
public IoBuffer wrap(ByteBuffer nioBuffer)
ByteBuffer
into MINA buffer.wrap
in interface IoBufferAllocator
nioBuffer
- The ByteBuffer
to wrapIoBuffer
wrapping the ByteBuffer
public void dispose()
dispose
in interface IoBufferAllocator
Copyright © 2004–2021 Apache MINA Project. All rights reserved.