Package org.xerial.snappy.pool
Class CachingBufferPool
java.lang.Object
org.xerial.snappy.pool.CachingBufferPool
- All Implemented Interfaces:
BufferPool
A
BufferPool
implementation which caches values at fixed sizes.
Pooled instances are held as SoftReference
to allow GC if necessary.
The current fixed sizes are calculated as follows:
- Values invalid input: '<' 4KB return 4KB
- 4KB - 32KB to 2KB
- 32KB - 512KB to 16KB
- 512KB - 2MB to 128KB
- 2MB - 16MB to 512KB
- 16MB - 128MB to 4MB
- 128MB - 512MB to 16MB
- 512MB - 1.5 GB to 128MB
- Values > 1.5GB return
Integer.MAX_VALUE
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final CachingBufferPool.IntFunction
<byte[]> private final ConcurrentMap
<Integer, ConcurrentLinkedDeque<SoftReference<ByteBuffer>>> private final ConcurrentMap
<Integer, ConcurrentLinkedDeque<SoftReference<byte[]>>> private static final CachingBufferPool.IntFunction
<ByteBuffer> private static final CachingBufferPool
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) static int
adjustSize
(int size) byte[]
allocateArray
(int size) Returns abyte[]
of size or greater length.allocateDirect
(int size) Returns adirect ByteBuffer
of size or greatercapacity
.static BufferPool
Returns instance ofCachingBufferPool
for using cached buffers.private static <E> E
getOrCreate
(int size, ConcurrentMap<Integer, ConcurrentLinkedDeque<SoftReference<E>>> map, CachingBufferPool.IntFunction<E> creator) private static <E> ConcurrentLinkedDeque
<SoftReference<E>> optimisticGetEntry
(Integer key, ConcurrentMap<Integer, ConcurrentLinkedDeque<SoftReference<E>>> map) void
releaseArray
(byte[] buffer) Returns instance to pool for potential future reuse.void
releaseDirect
(ByteBuffer buffer) Returns instance to pool for potential future reuse.private static <E> void
returnValue
(E value, Integer size, ConcurrentMap<Integer, ConcurrentLinkedDeque<SoftReference<E>>> map) private static int
roundToPowers
(int number, int bits) toString()
-
Field Details
-
ARRAY_FUNCTION
-
DBB_FUNCTION
-
INSTANCE
-
bytes
-
buffers
-
-
Constructor Details
-
CachingBufferPool
private CachingBufferPool()
-
-
Method Details
-
getInstance
Returns instance ofCachingBufferPool
for using cached buffers.- Returns:
- instance of
CachingBufferPool
for using cached buffers.
-
allocateArray
public byte[] allocateArray(int size) Returns abyte[]
of size or greater length.- Specified by:
allocateArray
in interfaceBufferPool
- Parameters:
size
- The minimum size array required. Must be>= 0
.- Returns:
- A
byte[]
with length of at least size. - See Also:
-
releaseArray
public void releaseArray(byte[] buffer) Returns instance to pool for potential future reuse.Must not be returned more than 1 time. Must not be used by caller after return.
- Specified by:
releaseArray
in interfaceBufferPool
- Parameters:
buffer
- Instance to return to pool. Must not benull
. Must not be returned more than 1 time. Must not be used by caller after return.
-
allocateDirect
Returns adirect ByteBuffer
of size or greatercapacity
.- Specified by:
allocateDirect
in interfaceBufferPool
- Parameters:
size
- The minimum size buffer required. Must be>= 0
.- Returns:
- A
ByteBuffer
of size or greatercapacity
. - See Also:
-
releaseDirect
Returns instance to pool for potential future reuse.Must not be returned more than 1 time. Must not be used by caller after return.
- Specified by:
releaseDirect
in interfaceBufferPool
- Parameters:
buffer
- Instance to return to pool. Must not benull
. Must not be returned more than 1 time. Must not be used by caller after return.
-
getOrCreate
private static <E> E getOrCreate(int size, ConcurrentMap<Integer, ConcurrentLinkedDeque<SoftReference<E>>> map, CachingBufferPool.IntFunction<E> creator) -
adjustSize
static int adjustSize(int size) -
roundToPowers
private static int roundToPowers(int number, int bits) -
optimisticGetEntry
private static <E> ConcurrentLinkedDeque<SoftReference<E>> optimisticGetEntry(Integer key, ConcurrentMap<Integer, ConcurrentLinkedDeque<SoftReference<E>>> map) -
returnValue
private static <E> void returnValue(E value, Integer size, ConcurrentMap<Integer, ConcurrentLinkedDeque<SoftReference<E>>> map) -
toString
-