Package io.netty.buffer
Class AdaptivePoolingAllocator.SizeClassedChunk
- java.lang.Object
-
- io.netty.buffer.AdaptivePoolingAllocator.Chunk
-
- io.netty.buffer.AdaptivePoolingAllocator.SizeClassedChunk
-
- All Implemented Interfaces:
ReferenceCounted
- Enclosing class:
- AdaptivePoolingAllocator
private static final class AdaptivePoolingAllocator.SizeClassedChunk extends AdaptivePoolingAllocator.Chunk
Removes per-allocation retain()/release() atomic ops from the hot path by replacing ref counting with a segment-count state machine. Atomics are only needed on the cold deallocation path (markToDeallocate()), which is rare for long-lived chunks that cycle segments many times. The tradeoff is aMpscIntQueue.size()call (volatile reads, no RMW) per remaining segment return after mark — acceptable since it avoids atomic RMWs entirely.State transitions:
AVAILABLE(-1): chunk is in use, no deallocation tracking needed- 0..N: local free list size at the time
markToDeallocate()was called; used to track when all segments have been returned DEALLOCATED(Integer.MIN_VALUE): all segments returned, chunk deallocated
Ordering: external
releaseSegment(int, int)pushes to the MPSC queue (which has an implicit StoreLoad barrier via itsoffer()), then readsstate— this guarantees visibility of any precedingmarkToDeallocate()write.
-
-
Field Summary
Fields Modifier and Type Field Description private static intAVAILABLEprivate static intDEALLOCATEDprivate MpscIntQueueexternalFreeListprivate static intFREE_LIST_EMPTYprivate AdaptivePoolingAllocator.IntStacklocalFreeListprivate java.lang.ThreadownerThreadprivate intsegmentsprivate intsegmentSizeprivate intstateprivate static java.util.concurrent.atomic.AtomicIntegerFieldUpdater<AdaptivePoolingAllocator.SizeClassedChunk>STATE-
Fields inherited from class io.netty.buffer.AdaptivePoolingAllocator.Chunk
allocatedBytes, delegate, magazine
-
-
Constructor Summary
Constructors Constructor Description SizeClassedChunk(AbstractByteBuf delegate, AdaptivePoolingAllocator.Magazine magazine, AdaptivePoolingAllocator.SizeClassChunkController controller)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description private voiddeallocateIfNeeded(int localSize)booleanhasRemainingCapacity()(package private) voidmarkToDeallocate()private intnextAvailableSegmentOffset()booleanreadInitInto(AdaptivePoolingAllocator.AdaptiveByteBuf buf, int size, int startingCapacity, int maxCapacity)(package private) voidreleaseSegment(int startIndex, int size)Called when a ByteBuf is done using its allocation in this chunk.private voidreleaseSegmentOffsetIntoFreeList(int startIndex)intremainingCapacity()private intupdateRemainingCapacity(int snapshotted)private voidupdateStateOnLocalReleaseSegment(int previousLocalSize, AdaptivePoolingAllocator.IntStack localFreeList)-
Methods inherited from class io.netty.buffer.AdaptivePoolingAllocator.Chunk
attachToMagazine, capacity, currentMagazine, deallocate, detachFromMagazine, hasUnprocessedFreelistEntries, processFreelistEntries, refCnt, release, release, releaseFromMagazine, retain, retain, touch, touch
-
-
-
-
Field Detail
-
FREE_LIST_EMPTY
private static final int FREE_LIST_EMPTY
- See Also:
- Constant Field Values
-
AVAILABLE
private static final int AVAILABLE
- See Also:
- Constant Field Values
-
DEALLOCATED
private static final int DEALLOCATED
- See Also:
- Constant Field Values
-
STATE
private static final java.util.concurrent.atomic.AtomicIntegerFieldUpdater<AdaptivePoolingAllocator.SizeClassedChunk> STATE
-
state
private volatile int state
-
segments
private final int segments
-
segmentSize
private final int segmentSize
-
externalFreeList
private final MpscIntQueue externalFreeList
-
localFreeList
private final AdaptivePoolingAllocator.IntStack localFreeList
-
ownerThread
private java.lang.Thread ownerThread
-
-
Constructor Detail
-
SizeClassedChunk
SizeClassedChunk(AbstractByteBuf delegate, AdaptivePoolingAllocator.Magazine magazine, AdaptivePoolingAllocator.SizeClassChunkController controller)
-
-
Method Detail
-
readInitInto
public boolean readInitInto(AdaptivePoolingAllocator.AdaptiveByteBuf buf, int size, int startingCapacity, int maxCapacity)
- Overrides:
readInitIntoin classAdaptivePoolingAllocator.Chunk
-
nextAvailableSegmentOffset
private int nextAvailableSegmentOffset()
-
hasRemainingCapacity
public boolean hasRemainingCapacity()
-
remainingCapacity
public int remainingCapacity()
- Overrides:
remainingCapacityin classAdaptivePoolingAllocator.Chunk
-
updateRemainingCapacity
private int updateRemainingCapacity(int snapshotted)
-
releaseSegmentOffsetIntoFreeList
private void releaseSegmentOffsetIntoFreeList(int startIndex)
-
releaseSegment
void releaseSegment(int startIndex, int size)Description copied from class:AdaptivePoolingAllocator.ChunkCalled when a ByteBuf is done using its allocation in this chunk.- Overrides:
releaseSegmentin classAdaptivePoolingAllocator.Chunk
-
updateStateOnLocalReleaseSegment
private void updateStateOnLocalReleaseSegment(int previousLocalSize, AdaptivePoolingAllocator.IntStack localFreeList)
-
deallocateIfNeeded
private void deallocateIfNeeded(int localSize)
-
markToDeallocate
void markToDeallocate()
- Overrides:
markToDeallocatein classAdaptivePoolingAllocator.Chunk
-
-