GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Remove reference counting from size classed chunks · netty/netty@0eefff6

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • buffer/src/main/java/io/netty/buffer
    • AdaptivePoolingAllocator.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java‎

Copy file name to clipboardExpand all lines: buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java
+104-16Lines changed: 104 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import java.util.Queue;
5050
import java.util.concurrent.ConcurrentLinkedQueue;
5151
import java.util.concurrent.atomic.AtomicInteger;
52+
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
5253
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;
5354
import java.util.concurrent.atomic.LongAdder;
5455
import java.util.concurrent.locks.StampedLock;
@@ -511,7 +512,7 @@ private void freeChunkReuseQueue(Thread ownerThread) {
511512
// that reference some thread local chunk
512513
threadLocalChunk.ownerThread = null;
513514
}
514-
chunk.release();
515+
chunk.markToDeallocate();
515516
}
516517
}
517518
}
@@ -633,7 +634,7 @@ public boolean offerChunk(Chunk chunk) {
633634
break;
634635
}
635636
if (chunks.remove(key, toDeallocate)) {
636-
toDeallocate.release();
637+
toDeallocate.markToDeallocate();
637638
}
638639
size = chunks.size();
639640
}
@@ -1143,6 +1144,10 @@ void releaseSegment(int ignoredSegmentId, int size) {
11431144
release();
11441145
}
11451146

1147+
void markToDeallocate() {
1148+
release();
1149+
}
1150+
11461151
private void retain() {
11471152
RefCnt.retain(refCnt);
11481153
}
@@ -1249,8 +1254,30 @@ public int size() {
12491254
}
12501255
}
12511256

1257+
/**
1258+
* Replaces per-allocation retain()/release() ref counting with a segment-count state machine
1259+
* that only needs atomics on the cold deallocation path.
1260+
* <p>
1261+
* State transitions:
1262+
* <ul>
1263+
* <li>{@link #AVAILABLE} (-1): chunk is in use, no deallocation tracking needed</li>
1264+
* <li>0..N: local free list size at the time {@link #markToDeallocate()} was called;
1265+
* used to track when all segments have been returned</li>
1266+
* <li>{@link #DEALLOCATED} (Integer.MIN_VALUE): all segments returned, chunk deallocated</li>
1267+
* </ul>
1268+
* <p>
1269+
* Ordering: external {@link #releaseSegment} pushes to the MPSC queue (which has an implicit
1270+
* StoreLoad barrier via its {@code offer()}), then reads {@code state} — this guarantees
1271+
* visibility of any preceding {@link #markToDeallocate()} write.
1272+
*/
12521273
private static final class SizeClassedChunk extends Chunk {
12531274
private static final int FREE_LIST_EMPTY = -1;
1275+
private static final int AVAILABLE = -1;
1276+
private static final int DEALLOCATED = Integer.MIN_VALUE;
1277+
private static final AtomicIntegerFieldUpdater<SizeClassedChunk> STATE =
1278+
AtomicIntegerFieldUpdater.newUpdater(SizeClassedChunk.class, "state");
1279+
private volatile int state;
1280+
private final int segments;
12541281
private final int segmentSize;
12551282
private final MpscIntQueue externalFreeList;
12561283
private final IntStack localFreeList;
@@ -1260,6 +1287,8 @@ private static final class SizeClassedChunk extends Chunk {
12601287
SizeClassChunkController controller) {
12611288
super(delegate, magazine, true);
12621289
segmentSize = controller.segmentSize;
1290+
segments = controller.chunkSize / segmentSize;
1291+
STATE.lazySet(this, AVAILABLE);
12631292
ownerThread = magazine.group.ownerThread;
12641293
if (ownerThread == null) {
12651294
externalFreeList = controller.createFreeList();
@@ -1270,26 +1299,30 @@ private static final class SizeClassedChunk extends Chunk {
12701299
}
12711300
}
12721301

1302+
@Override
1303+
boolean releaseFromMagazine() {
1304+
Magazine mag = magazine;
1305+
detachFromMagazine();
1306+
if (!mag.offerToQueue(this)) {
1307+
markToDeallocate();
1308+
}
1309+
return false;
1310+
}
1311+
12731312
@Override
12741313
public boolean readInitInto(AdaptiveByteBuf buf, int size, int startingCapacity, int maxCapacity) {
1314+
assert state == AVAILABLE;
12751315
final int startIndex = nextAvailableSegmentOffset();
12761316
if (startIndex == FREE_LIST_EMPTY) {
12771317
return false;
12781318
}
12791319
allocatedBytes += segmentSize;
1280-
Chunk chunk = this;
1281-
chunk.retain();
12821320
try {
1283-
buf.init(delegate, chunk, 0, 0, startIndex, size, startingCapacity, maxCapacity);
1284-
chunk = null;
1285-
} finally {
1286-
if (chunk != null) {
1287-
// If chunk is not null we know that buf.init(...) failed and so we need to manually release
1288-
// the chunk again as we retained it before calling buf.init(...). Beside this we also need to
1289-
// restore the old allocatedBytes value.
1290-
allocatedBytes -= segmentSize;
1291-
chunk.releaseSegment(startIndex, startingCapacity);
1292-
}
1321+
buf.init(delegate, this, 0, 0, startIndex, size, startingCapacity, maxCapacity);
1322+
} catch (Throwable t) {
1323+
allocatedBytes -= segmentSize;
1324+
releaseSegmentOffsetIntoFreeList(startIndex);
1325+
throw t;
12931326
}
12941327
return true;
12951328
}
@@ -1348,8 +1381,63 @@ private void releaseSegmentOffsetIntoFreeList(int startIndex) {
13481381

13491382
@Override
13501383
void releaseSegment(int startIndex, int size) {
1351-
release();
1352-
releaseSegmentOffsetIntoFreeList(startIndex);
1384+
IntStack localFreeList = this.localFreeList;
1385+
if (localFreeList != null && Thread.currentThread() == ownerThread) {
1386+
localFreeList.push(startIndex);
1387+
int state = this.state;
1388+
if (state != AVAILABLE) {
1389+
updateStateOnLocalReleaseSegment(state, localFreeList);
1390+
}
1391+
} else {
1392+
boolean segmentReturned = externalFreeList.offer(startIndex);
1393+
assert segmentReturned;
1394+
// implicit StoreLoad barrier from MPSC offer()
1395+
int state = this.state;
1396+
if (state != AVAILABLE) {
1397+
handleStateOnExternalReleaseSegment(state);
1398+
}
1399+
}
1400+
}
1401+
1402+
private void handleStateOnExternalReleaseSegment(int localFreeListSize) {
1403+
// localFreeListSize is the number of segments that were in the local free list when
1404+
// markToDeallocate() was called. Check if all segments have been returned.
1405+
int totalFreeSegments = localFreeListSize + externalFreeList.size();
1406+
if (totalFreeSegments == segments) {
1407+
if (STATE.compareAndSet(this, localFreeListSize, DEALLOCATED)) {
1408+
deallocate();
1409+
}
1410+
}
1411+
}
1412+
1413+
private void updateStateOnLocalReleaseSegment(int previousLocalSize, IntStack localFreeList) {
1414+
// Update the state to reflect the new local free list size.
1415+
int newLocalSize = localFreeList.size();
1416+
if (!STATE.compareAndSet(this, previousLocalSize, newLocalSize)) {
1417+
// State was concurrently changed by another external releaseSegment that deallocated.
1418+
return;
1419+
}
1420+
// Check if all segments have been returned.
1421+
int totalFreeSegments = newLocalSize + externalFreeList.size();
1422+
if (totalFreeSegments == segments) {
1423+
if (STATE.compareAndSet(this, newLocalSize, DEALLOCATED)) {
1424+
deallocate();
1425+
}
1426+
}
1427+
}
1428+
1429+
@Override
1430+
void markToDeallocate() {
1431+
IntStack localFreeList = this.localFreeList;
1432+
int localSize = localFreeList != null ? localFreeList.size() : 0;
1433+
STATE.set(this, localSize);
1434+
// Check if all segments have already been returned.
1435+
int totalFreeSegments = localSize + externalFreeList.size();
1436+
if (totalFreeSegments == segments) {
1437+
if (STATE.compareAndSet(this, localSize, DEALLOCATED)) {
1438+
deallocate();
1439+
}
1440+
}
13531441
}
13541442
}
13551443

0 commit comments

Comments
0 (0)
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.