Package dev.relism.flash.transport
Class ScratchPool
java.lang.Object
dev.relism.flash.transport.ScratchPool
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
FieldsModifier and TypeFieldDescriptionstatic final intDefault 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 -
Method Summary
Modifier and TypeMethodDescriptionacquire()Returns a reset, ready-to-use scratch — either reused from the pool or freshly allocated.voidrelease(ConnectionScratch scratch) Returnsscratchto 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.
-
Field Details
-
DEFAULT_BOUND
public static final int DEFAULT_BOUNDDefault 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
Returns a reset, ready-to-use scratch — either reused from the pool or freshly allocated. -
release
Returnsscratchto 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.
-