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 — Allocate bytebuf without magazine lock when threads get collisions, t… · netty/netty@af1c110

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • buffer/src/main/java/io/netty/buffer
    • AdaptivePoolingAllocator.java
  • microbench/src/main/java/io/netty/microbench/buffer
    • AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.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
+41-9Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,40 @@ public boolean tryAllocate(int size, int sizeBucket, int maxCapacity, AdaptiveBy
569569
allocationLock.unlockWrite(writeLock);
570570
}
571571
}
572-
return false;
572+
return allocateWithoutLock(size, sizeBucket, maxCapacity, buf);
573+
}
574+
575+
private boolean allocateWithoutLock(int size, int sizeBucket, int maxCapacity, AdaptiveByteBuf buf) {
576+
Chunk curr = NEXT_IN_LINE.getAndSet(this, null);
577+
if (curr == MAGAZINE_FREED) {
578+
// Allocation raced with a stripe-resize that freed this magazine.
579+
restoreMagazineFreed();
580+
return false;
581+
}
582+
if (curr == null) {
583+
curr = parent.centralQueue.poll();
584+
if (curr == null) {
585+
return false;
586+
}
587+
curr.attachToMagazine(this);
588+
}
589+
if (curr.remainingCapacity() >= size) {
590+
curr.readInitInto(buf, size, maxCapacity);
591+
}
592+
try {
593+
if (curr.remainingCapacity() < RETIRE_CAPACITY) {
594+
curr.release();
595+
} else {
596+
transferChunk(curr);
597+
}
598+
curr = null;
599+
} finally {
600+
if (curr != null) {
601+
// Something went wrong, let's ensure we not leak the newChunk.
602+
curr.release();
603+
}
604+
}
605+
return true;
573606
}
574607

575608
private boolean allocate(int size, int sizeBucket, int maxCapacity, AdaptiveByteBuf buf) {
@@ -603,14 +636,13 @@ private boolean allocate(int size, int sizeBucket, int maxCapacity, AdaptiveByte
603636
//
604637
// In any case we will store the Chunk as the current so it will be used again for the next allocation and
605638
// so be "reserved" by this Magazine for exclusive usage.
606-
if (nextInLine != null) {
607-
curr = NEXT_IN_LINE.getAndSet(this, null);
608-
if (curr == MAGAZINE_FREED) {
609-
// Allocation raced with a stripe-resize that freed this magazine.
610-
restoreMagazineFreed();
611-
return false;
612-
}
613-
} else {
639+
curr = NEXT_IN_LINE.getAndSet(this, null);
640+
if (curr == MAGAZINE_FREED) {
641+
// Allocation raced with a stripe-resize that freed this magazine.
642+
restoreMagazineFreed();
643+
return false;
644+
}
645+
if (curr == null) {
614646
curr = parent.centralQueue.poll();
615647
if (curr == null) {
616648
curr = newChunkAllocation(size);
Collapse file

‎microbench/src/main/java/io/netty/microbench/buffer/AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark.java‎

Copy file name to clipboard
+61Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2024 The Netty Project
3+
*
4+
* The Netty Project licenses this file to you under the Apache License,
5+
* version 2.0 (the "License"); you may not use this file except in compliance
6+
* with the License. You may obtain a copy of the License at:
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations
14+
* under the License.
15+
*/
16+
package io.netty.microbench.buffer;
17+
18+
import io.netty.buffer.AdaptiveByteBufAllocator;
19+
import io.netty.buffer.ByteBufAllocator;
20+
import io.netty.microbench.util.AbstractMicrobenchmark;
21+
import org.openjdk.jmh.annotations.Benchmark;
22+
import org.openjdk.jmh.annotations.Measurement;
23+
import org.openjdk.jmh.annotations.Param;
24+
import org.openjdk.jmh.annotations.Scope;
25+
import org.openjdk.jmh.annotations.State;
26+
import org.openjdk.jmh.annotations.Threads;
27+
import org.openjdk.jmh.annotations.Warmup;
28+
import org.openjdk.jmh.infra.Blackhole;
29+
30+
@State(Scope.Benchmark)
31+
@Warmup(iterations = 10, time = 1)
32+
@Measurement(iterations = 10, time = 1)
33+
public class AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark extends AbstractMicrobenchmark {
34+
35+
private static final ByteBufAllocator adaptiveAllocator =
36+
new AdaptiveByteBufAllocator(true, false);
37+
38+
@Param({
39+
"00064",
40+
"00256",
41+
"01024",
42+
"04096",
43+
})
44+
private int size;
45+
46+
public AdaptiveByteBufAllocatorConcurrentNoCacheBenchmark() {
47+
super(false, true);
48+
}
49+
50+
@Benchmark
51+
@Threads(32)
52+
public void allocateReleaseHeapAdaptive(Blackhole blackhole) {
53+
blackhole.consume(adaptiveAllocator.heapBuffer(size).release());
54+
}
55+
56+
@Benchmark
57+
@Threads(32)
58+
public void allocateReleaseDirectAdaptive(Blackhole blackhole) {
59+
blackhole.consume(adaptiveAllocator.directBuffer(size).release());
60+
}
61+
}

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.