Class ScratchPool

java.lang.Object
dev.relism.flash.transport.ScratchPool

public final class ScratchPool extends Object
A bounded cache of ConnectionScratch instances, reused across connections instead of being allocated and garbage-collected per connection.

This is a cache, not a leak-free arena: a burst of 100 000 concurrent connections still allocates 100 000 ConnectionScratch instances (one per connection, since each connection needs its own for as long as it is open), but only bound of them survive being released back to the pool afterward — the rest are simply dropped for the garbage collector, exactly as they would have been without this class. What the pool buys is avoiding repeated allocation for the common case of many short-lived or sequential connections sharing a bounded set of scratch objects.

Thread-safety

acquire() and release(dev.relism.flash.transport.ConnectionScratch) are safe to call concurrently from any number of threads — the underlying queue and size guard are lock-free.
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Default bound: generous enough that a real workload rarely misses, small enough that it is not itself a meaningful memory commitment (a few hundred KB at most).
  • Constructor Summary

    Constructors
    Constructor
    Description
     
    ScratchPool(int bound)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns a reset, ready-to-use scratch — either reused from the pool or freshly allocated.
    void
    Returns scratch to the pool for reuse, unless the pool is already at its bound — in which case it is simply dropped, for the garbage collector, so an unusually large burst of connections cannot grow this cache without limit.

    Methods inherited from class java.lang.Object

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

    • DEFAULT_BOUND

      public static final int DEFAULT_BOUND
      Default bound: generous enough that a real workload rarely misses, small enough that it is not itself a meaningful memory commitment (a few hundred KB at most).
  • Constructor Details

    • ScratchPool

      public ScratchPool()
    • ScratchPool

      public ScratchPool(int bound)
  • Method Details

    • acquire

      public ConnectionScratch acquire()
      Returns a reset, ready-to-use scratch — either reused from the pool or freshly allocated.
    • release

      public void release(ConnectionScratch scratch)
      Returns scratch to the pool for reuse, unless the pool is already at its bound — in which case it is simply dropped, for the garbage collector, so an unusually large burst of connections cannot grow this cache without limit.