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 — Refactor remainingCapacity calculation and deallocation logic in Adap… · netty/netty@e5751d6

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
+21-35Lines changed: 21 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,30 +1346,23 @@ private int nextAvailableSegmentOffset() {
13461346
return startIndex;
13471347
}
13481348

1349-
private int remainingCapacityOnFreeList() {
1350-
final int segmentSize = this.segmentSize;
1351-
int remainingCapacity = externalFreeList.size() * segmentSize;
1352-
IntStack localFreeList = this.localFreeList;
1353-
if (localFreeList != null) {
1354-
assert Thread.currentThread() == ownerThread;
1355-
remainingCapacity += localFreeList.size() * segmentSize;
1356-
}
1357-
return remainingCapacity;
1358-
}
1359-
13601349
@Override
13611350
public int remainingCapacity() {
1362-
int remainingCapacity = super.remainingCapacity();
1363-
if (remainingCapacity > segmentSize) {
1364-
return remainingCapacity;
1351+
int remaining = super.remainingCapacity();
1352+
return remaining > segmentSize ? remaining : updateRemainingCapacity(remaining);
1353+
}
1354+
1355+
private int updateRemainingCapacity(int snapshotted) {
1356+
int freeSegments = externalFreeList.size();
1357+
IntStack localFreeList = this.localFreeList;
1358+
if (localFreeList != null) {
1359+
freeSegments += localFreeList.size();
13651360
}
1366-
int updatedRemainingCapacity = remainingCapacityOnFreeList();
1367-
if (updatedRemainingCapacity == remainingCapacity) {
1368-
return remainingCapacity;
1361+
int updated = freeSegments * segmentSize;
1362+
if (updated != snapshotted) {
1363+
allocatedBytes = capacity() - updated;
13691364
}
1370-
// update allocatedBytes based on what's available in the free list
1371-
allocatedBytes = capacity() - updatedRemainingCapacity;
1372-
return updatedRemainingCapacity;
1365+
return updated;
13731366
}
13741367

13751368
private void releaseSegmentOffsetIntoFreeList(int startIndex) {
@@ -1405,12 +1398,7 @@ void releaseSegment(int startIndex, int size) {
14051398
private void handleStateOnExternalReleaseSegment(int localFreeListSize) {
14061399
// localFreeListSize is the number of segments that were in the local free list when
14071400
// markToDeallocate() was called. Check if all segments have been returned.
1408-
int totalFreeSegments = localFreeListSize + externalFreeList.size();
1409-
if (totalFreeSegments == segments) {
1410-
if (STATE.compareAndSet(this, localFreeListSize, DEALLOCATED)) {
1411-
deallocate();
1412-
}
1413-
}
1401+
deallocateIfNeeded(localFreeListSize);
14141402
}
14151403

14161404
private void updateStateOnLocalReleaseSegment(int previousLocalSize, IntStack localFreeList) {
@@ -1420,10 +1408,14 @@ private void updateStateOnLocalReleaseSegment(int previousLocalSize, IntStack lo
14201408
// State was concurrently changed by another external releaseSegment that deallocated.
14211409
return;
14221410
}
1411+
deallocateIfNeeded(newLocalSize);
1412+
}
1413+
1414+
private void deallocateIfNeeded(int localSize) {
14231415
// Check if all segments have been returned.
1424-
int totalFreeSegments = newLocalSize + externalFreeList.size();
1416+
int totalFreeSegments = localSize + externalFreeList.size();
14251417
if (totalFreeSegments == segments) {
1426-
if (STATE.compareAndSet(this, newLocalSize, DEALLOCATED)) {
1418+
if (STATE.compareAndSet(this, localSize, DEALLOCATED)) {
14271419
deallocate();
14281420
}
14291421
}
@@ -1434,13 +1426,7 @@ void markToDeallocate() {
14341426
IntStack localFreeList = this.localFreeList;
14351427
int localSize = localFreeList != null ? localFreeList.size() : 0;
14361428
STATE.set(this, localSize);
1437-
// Check if all segments have already been returned.
1438-
int totalFreeSegments = localSize + externalFreeList.size();
1439-
if (totalFreeSegments == segments) {
1440-
if (STATE.compareAndSet(this, localSize, DEALLOCATED)) {
1441-
deallocate();
1442-
}
1443-
}
1429+
deallocateIfNeeded(localSize);
14441430
}
14451431
}
14461432

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.