Class SlicePool

java.lang.Object
dev.relism.flash.bytes.SlicePool

public final class SlicePool extends Object
A small, fixed-size ring of PooledSlice instances — one per ConnectionScratch- Http1HeaderMap.view, QueryParams.view, PathParams.view).

Why a ring, not a single reused slice

A single reused slice (the shape Http1HeaderMap.forEach already uses for its two nameSlice/valueSlice fields) is correct only when the caller is guaranteed to finish with one slice before the next is produced — true for a single forEach callback invocation, false for view(): a handler might reasonably call headers.view("A") and headers.view("B") and want to compare both. A ring of size slices lets up to size calls' results stay simultaneously valid.

Lifetime contract

A slice returned by acquire(byte[], int, int) is valid until either the request ends, or acquire(byte[], int, int) is called size more times on the same pool (at which point the ring has wrapped around and repositioned that same slot for a new caller) — whichever comes first. This must be restated precisely on every method that hands out a slice from a pool (see Http1HeaderMap.view's Javadoc for the canonical wording); it is a real, testable hazard, not a hypothetical one — see SlicePoolTest#wraparoundAliasesThePreviouslyReturnedSlice for a demonstration.
  • Constructor Summary

    Constructors
    Constructor
    Description
    SlicePool(int size)
    A ring of size reusable slices.
  • Method Summary

    Modifier and Type
    Method
    Description
    acquire(byte[] array, int offset, int length)
    Returns the next slice in the ring, repositioned over array[offset, offset + length).
    int
    How many slices this pool cycles through before a caller's slice is reused.

    Methods inherited from class java.lang.Object

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

    • SlicePool

      public SlicePool(int size)
      A ring of size reusable slices. size must be at least 1.
  • Method Details

    • size

      public int size()
      How many slices this pool cycles through before a caller's slice is reused.
    • acquire

      public PooledSlice acquire(byte[] array, int offset, int length)
      Returns the next slice in the ring, repositioned over array[offset, offset + length). Zero allocation — the returned instance already existed.