Class SizedSoftCache<T>

  • Type Parameters:
    T -
    Direct Known Subclasses:
    ByteBuffersSoftCache, BytesSoftCache

    public abstract class SizedSoftCache<T>
    extends java.lang.Object
    Common code for a simple class that caches object that have an associated size
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected SizedSoftCache​(int traceIndex, int minimum)  
      protected SizedSoftCache​(int traceIndex, java.lang.String identity, int minimum)  
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      T allocate​(int desiredSize)
      Obtain a new or recycled object of the given type with capacity no less than the specified desired size.
      protected abstract T create​(int size)
      When the cache runs empty, further allocations must create new objects.
      protected abstract int dynamicEach​(int size)
      Implementations must provide a function that estimates the GC impact of an instance of a given size.
      void free​(T recycle)
      Return an object of the given type for caching.
      int normalizedSize​(int requestSize)
      To maximize re-use, requested sizes are rounded up to the next larger power of two, or the next larger value that is 3 * a power of two.
      protected abstract boolean qualified​(T recycle)
      An item returned from a user might have been changed in a way that makes it unsuitable for reuse.
      protected abstract int sizeOf​(T recycle)
      When an object is freed for recycling, its size must be examined to determine the appropriate nested cache to use, and whether it exactly matches that cache.
      • Methods inherited from class java.lang.Object

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

      • FullName

        public static final java.lang.String FullName
      • sLogger

        public static final org.slf4j.Logger sLogger
      • minimum

        protected final int minimum
      • sizedCaches

        protected final java.util.Map<java.lang.Integer,​SoftCache<T>> sizedCaches
      • traceIndex

        protected final int traceIndex
      • identity

        protected final java.lang.String identity
    • Constructor Detail

      • SizedSoftCache

        protected SizedSoftCache​(int traceIndex,
                                 int minimum)
      • SizedSoftCache

        protected SizedSoftCache​(int traceIndex,
                                 java.lang.String identity,
                                 int minimum)
    • Method Detail

      • dynamicEach

        protected abstract int dynamicEach​(int size)
        Implementations must provide a function that estimates the GC impact of an instance of a given size.
        Parameters:
        size -
        Returns:
      • normalizedSize

        public int normalizedSize​(int requestSize)
        To maximize re-use, requested sizes are rounded up to the next larger power of two, or the next larger value that is 3 * a power of two. But never less than 2.

        Like so: 2, 3, 4, 6, 8, 12, 16, 24, 32, 48, 64, 96, 128, 192, 256...

        Parameters:
        requestSize -
        Returns:
      • qualified

        protected abstract boolean qualified​(T recycle)
        An item returned from a user might have been changed in a way that makes it unsuitable for reuse. This method gives the implementation a chance to verify that and/or make corrections.
        Parameters:
        recycle - The subject item that is to be placed back in this cache's fast access list.
        Returns:
        True if ready for re-use, false if to be discarded.
      • create

        protected abstract T create​(int size)
        When the cache runs empty, further allocations must create new objects. Since the queue is based on soft references, the garbage collector can throw some away at will.
        Returns:
      • allocate

        public T allocate​(int desiredSize)
        Obtain a new or recycled object of the given type with capacity no less than the specified desired size. Collect timing stats by normalized size on allocation pace to maintain approximately five seconds of buffered objects.
        Returns:
      • sizeOf

        protected abstract int sizeOf​(T recycle)
        When an object is freed for recycling, its size must be examined to determine the appropriate nested cache to use, and whether it exactly matches that cache.
        Parameters:
        recycle -
        Returns:
        The actual size of the object to recycle.
      • free

        public void free​(T recycle)
        Return an object of the given type for caching.
        Parameters:
        recycle -