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 (#… · netty/netty@c29f4a5

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
+33-3Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,37 @@ public boolean tryAllocate(int size, int sizeBucket, int maxCapacity, AdaptiveBy
573573
allocationLock.unlockWrite(writeLock);
574574
}
575575
}
576-
return false;
576+
return allocateWithoutLock(size, sizeBucket, maxCapacity, buf);
577+
}
578+
579+
private boolean allocateWithoutLock(int size, int sizeBucket, int maxCapacity, AdaptiveByteBuf buf) {
580+
Chunk curr = NEXT_IN_LINE.getAndSet(this, null);
581+
if (curr == MAGAZINE_FREED) {
582+
// Allocation raced with a stripe-resize that freed this magazine.
583+
restoreMagazineFreed();
584+
return false;
585+
}
586+
if (curr == null) {
587+
curr = parent.centralQueue.poll();
588+
if (curr == null) {
589+
return false;
590+
}
591+
curr.attachToMagazine(this);
592+
}
593+
if (curr.remainingCapacity() >= size) {
594+
curr.readInitInto(buf, size, maxCapacity);
595+
}
596+
try {
597+
if (curr.remainingCapacity() >= RETIRE_CAPACITY) {
598+
transferToNextInLineOrRelease(curr);
599+
curr = null;
600+
}
601+
} finally {
602+
if (curr != null) {
603+
curr.release();
604+
}
605+
}
606+
return true;
577607
}
578608

579609
private boolean allocate(int size, int sizeBucket, int maxCapacity, AdaptiveByteBuf buf) {
@@ -617,8 +647,8 @@ private boolean allocate(int size, int sizeBucket, int maxCapacity, AdaptiveByte
617647
//
618648
// In any case we will store the Chunk as the current so it will be used again for the next allocation and
619649
// so be "reserved" by this Magazine for exclusive usage.
620-
if (nextInLine != null) {
621-
curr = NEXT_IN_LINE.getAndSet(this, null);
650+
curr = NEXT_IN_LINE.getAndSet(this, null);
651+
if (curr != null) {
622652
if (curr == MAGAZINE_FREED) {
623653
// Allocation raced with a stripe-resize that freed this magazine.
624654
restoreMagazineFreed();
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.