Class CachingBufferPool

  • All Implemented Interfaces:
    BufferPool

    public final class CachingBufferPool
    extends java.lang.Object
    implements 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 < 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

    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private CachingBufferPool()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) static int adjustSize​(int size)  
      byte[] allocateArray​(int size)
      Returns a byte[] of size or greater length.
      java.nio.ByteBuffer allocateDirect​(int size)
      Returns a direct ByteBuffer of size or greater capacity.
      static BufferPool getInstance()
      Returns instance of CachingBufferPool for using cached buffers.
      private static <E> E getOrCreate​(int size, java.util.concurrent.ConcurrentMap<java.lang.Integer,​java.util.concurrent.ConcurrentLinkedDeque<java.lang.ref.SoftReference<E>>> map, CachingBufferPool.IntFunction<E> creator)  
      private static <E> java.util.concurrent.ConcurrentLinkedDeque<java.lang.ref.SoftReference<E>> optimisticGetEntry​(java.lang.Integer key, java.util.concurrent.ConcurrentMap<java.lang.Integer,​java.util.concurrent.ConcurrentLinkedDeque<java.lang.ref.SoftReference<E>>> map)  
      void releaseArray​(byte[] buffer)
      Returns instance to pool for potential future reuse.
      void releaseDirect​(java.nio.ByteBuffer buffer)
      Returns instance to pool for potential future reuse.
      private static <E> void returnValue​(E value, java.lang.Integer size, java.util.concurrent.ConcurrentMap<java.lang.Integer,​java.util.concurrent.ConcurrentLinkedDeque<java.lang.ref.SoftReference<E>>> map)  
      private static int roundToPowers​(int number, int bits)  
      java.lang.String toString()
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • bytes

        private final java.util.concurrent.ConcurrentMap<java.lang.Integer,​java.util.concurrent.ConcurrentLinkedDeque<java.lang.ref.SoftReference<byte[]>>> bytes
      • buffers

        private final java.util.concurrent.ConcurrentMap<java.lang.Integer,​java.util.concurrent.ConcurrentLinkedDeque<java.lang.ref.SoftReference<java.nio.ByteBuffer>>> buffers
    • Constructor Detail

      • CachingBufferPool

        private CachingBufferPool()
    • Method Detail

      • allocateArray

        public byte[] allocateArray​(int size)
        Returns a byte[] of size or greater length.
        Specified by:
        allocateArray in interface BufferPool
        Parameters:
        size - The minimum size array required. Must be >= 0.
        Returns:
        A byte[] with length of at least size.
        See Also:
        BufferPool.releaseArray(byte[])
      • 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 interface BufferPool
        Parameters:
        buffer - Instance to return to pool. Must not be null. Must not be returned more than 1 time. Must not be used by caller after return.
      • allocateDirect

        public java.nio.ByteBuffer allocateDirect​(int size)
        Returns a direct ByteBuffer of size or greater capacity.
        Specified by:
        allocateDirect in interface BufferPool
        Parameters:
        size - The minimum size buffer required. Must be >= 0.
        Returns:
        A ByteBuffer of size or greater capacity.
        See Also:
        BufferPool.releaseDirect(ByteBuffer), ByteBuffer.allocateDirect(int)
      • releaseDirect

        public void releaseDirect​(java.nio.ByteBuffer 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:
        releaseDirect in interface BufferPool
        Parameters:
        buffer - Instance to return to pool. Must not be null. Must not be returned more than 1 time. Must not be used by caller after return.
      • getOrCreate

        private static <E> E getOrCreate​(int size,
                                         java.util.concurrent.ConcurrentMap<java.lang.Integer,​java.util.concurrent.ConcurrentLinkedDeque<java.lang.ref.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> java.util.concurrent.ConcurrentLinkedDeque<java.lang.ref.SoftReference<E>> optimisticGetEntry​(java.lang.Integer key,
                                                                                                                         java.util.concurrent.ConcurrentMap<java.lang.Integer,​java.util.concurrent.ConcurrentLinkedDeque<java.lang.ref.SoftReference<E>>> map)
      • returnValue

        private static <E> void returnValue​(E value,
                                            java.lang.Integer size,
                                            java.util.concurrent.ConcurrentMap<java.lang.Integer,​java.util.concurrent.ConcurrentLinkedDeque<java.lang.ref.SoftReference<E>>> map)
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object