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 — Adaptive: Fix concurrency issue in adaptive allocator (#16767) (#16778) · netty/netty@7fc2dcb

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • buffer/src/main/java/io/netty/buffer
    • AdaptivePoolingAllocator.java
  • common/src/main/java/io/netty/util/concurrent
    • MpscAtomicIntegerArrayQueue.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
+23-8Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -704,13 +704,13 @@ private SizeClassChunkController(MagazineGroup group, int segmentSize, int chunk
704704
chunkRegistry = group.allocator.chunkRegistry;
705705
}
706706

707-
private MpscIntQueue createEmptyFreeList() {
707+
private MpscAtomicIntegerArrayQueue createEmptyFreeList() {
708708
return new MpscAtomicIntegerArrayQueue(chunkSize / segmentSize, SizeClassedChunk.FREE_LIST_EMPTY);
709709
}
710710

711-
private MpscIntQueue createFreeList() {
711+
private MpscAtomicIntegerArrayQueue createFreeList() {
712712
final int segmentsCount = chunkSize / segmentSize;
713-
final MpscIntQueue freeList = new MpscAtomicIntegerArrayQueue(
713+
final MpscAtomicIntegerArrayQueue freeList = new MpscAtomicIntegerArrayQueue(
714714
segmentsCount, SizeClassedChunk.FREE_LIST_EMPTY);
715715
int segmentOffset = 0;
716716
for (int i = 0; i < segmentsCount; i++) {
@@ -1455,7 +1455,7 @@ private static final class BuddyChunk extends Chunk implements IntConsumer {
14551455
private static final int PACK_OFFSET_MASK = 0xFFFF;
14561456
private static final int PACK_SIZE_SHIFT = Integer.SIZE - Integer.numberOfLeadingZeros(PACK_OFFSET_MASK);
14571457

1458-
private final MpscIntQueue freeList;
1458+
private final MpscAtomicIntegerArrayQueue freeList;
14591459
// The bits of each buddy: [1: is claimed][1: has claimed children][30: MIN_BUDDY_SIZE shift to get size]
14601460
private final byte[] buddies;
14611461
private final int freeListCapacity;
@@ -1512,12 +1512,20 @@ public boolean readInitInto(AdaptiveByteBuf buf, int size, int startingCapacity,
15121512
@Override
15131513
public void accept(int packed) {
15141514
// Called by allocating thread when draining freeList.
1515-
int size = MIN_BUDDY_SIZE << (packed >> PACK_SIZE_SHIFT);
1516-
int offset = (packed & PACK_OFFSET_MASK) * MIN_BUDDY_SIZE;
1515+
int size = unpackSize(packed);
1516+
int offset = unpackOffset(packed);
15171517
unreserveMatchingBuddy(1, size, offset, 0);
15181518
allocatedBytes -= size;
15191519
}
15201520

1521+
private static int unpackSize(int packed) {
1522+
return MIN_BUDDY_SIZE << (packed >> PACK_SIZE_SHIFT);
1523+
}
1524+
1525+
private static int unpackOffset(int packed) {
1526+
return (packed & PACK_OFFSET_MASK) * MIN_BUDDY_SIZE;
1527+
}
1528+
15211529
@Override
15221530
void releaseSegment(int startingIndex, int size) {
15231531
int packedOffset = startingIndex / MIN_BUDDY_SIZE;
@@ -1529,10 +1537,17 @@ void releaseSegment(int startingIndex, int size) {
15291537

15301538
@Override
15311539
public int remainingCapacity() {
1540+
int capacityInFreeList = 0;
15321541
if (!freeList.isEmpty()) {
1533-
freeList.drain(freeListCapacity, this);
1542+
capacityInFreeList = freeList.weakPeekReduce(freeListCapacity, 0,
1543+
new MpscAtomicIntegerArrayQueue.IntBinaryOperator() {
1544+
@Override
1545+
public int applyAsInt(int sum, int entry) {
1546+
return sum + unpackSize(entry);
1547+
}
1548+
});
15341549
}
1535-
return super.remainingCapacity();
1550+
return super.remainingCapacity() + capacityInFreeList;
15361551
}
15371552

15381553
@Override
Collapse file

‎common/src/main/java/io/netty/util/concurrent/MpscAtomicIntegerArrayQueue.java‎

Copy file name to clipboardExpand all lines: common/src/main/java/io/netty/util/concurrent/MpscAtomicIntegerArrayQueue.java
+35Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,37 @@ public int fill(int limit, IntSupplier supplier) {
199199
return actualLimit;
200200
}
201201

202+
/**
203+
* Peek at all available elements and compute a reduction.
204+
* The elements are not removed, and the iteration is weakly consistent.
205+
* @param limit The maximum number of elements to process.
206+
* @param initial The initial value to the reduction operation.
207+
* @param op The reduction operation, taking a prior result and an element, and producing a new result.
208+
* @return The last result of the reduction operation.
209+
*/
210+
public int weakPeekReduce(int limit, int initial, IntBinaryOperator op) {
211+
ObjectUtil.checkNotNull(op, "op");
212+
ObjectUtil.checkPositiveOrZero(limit, "limit");
213+
if (limit == 0) {
214+
return 0;
215+
}
216+
int result = initial;
217+
218+
final int mask = this.mask;
219+
final long cIndex = consumerIndex; // Note: could be weakened to plain-load.
220+
for (int i = 0; i < limit; i++) {
221+
final long index = cIndex + i;
222+
final int offset = (int) (index & mask);
223+
final int value = get(offset);
224+
if (emptyValue == value) {
225+
return result;
226+
}
227+
// Do not remove the element or advance the consumer index.
228+
result = op.applyAsInt(result, value);
229+
}
230+
return result;
231+
}
232+
202233
@Override
203234
public boolean isEmpty() {
204235
// Load consumer index before producer index, so our check is conservative.
@@ -223,4 +254,8 @@ public int size() {
223254
}
224255
return size < 0 ? 0 : size > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) size;
225256
}
257+
258+
public interface IntBinaryOperator {
259+
int applyAsInt(int a, int b);
260+
}
226261
}

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.