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 — Make AdaptiveByteBuf.memoryAddress() faster (#17767) · netty/netty@0679ea3

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • buffer/src
    • main/java/io/netty/buffer
      • AdaptivePoolingAllocator.java
    • test/java/io/netty/buffer
      • AdaptiveByteBufAllocatorTest.java
  • microbench/src/main/java/io/netty/microbench/buffer
    • AdaptiveByteBufMemoryAddressBenchmark.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
+14-2Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1588,6 +1588,8 @@ public void remove(Chunk chunk) {
15881588

15891589
static class Chunk implements ChunkInfo {
15901590
protected final AbstractByteBuf delegate;
1591+
// the delegate's memory address, or 0 when it has none: it doesn't change while the chunk is in use
1592+
final long delegateMemoryAddress;
15911593
protected Magazine magazine;
15921594
private final AdaptivePoolingAllocator allocator;
15931595
// Always populate the refCnt field, so HotSpot doesn't emit `null` checks.
@@ -1600,6 +1602,7 @@ static class Chunk implements ChunkInfo {
16001602
Chunk() {
16011603
// Constructor only used by the MAGAZINE_FREED sentinel.
16021604
delegate = null;
1605+
delegateMemoryAddress = 0L;
16031606
magazine = null;
16041607
allocator = null;
16051608
capacity = 0;
@@ -1608,6 +1611,7 @@ static class Chunk implements ChunkInfo {
16081611

16091612
Chunk(AbstractByteBuf delegate, Magazine magazine, boolean pooled) {
16101613
this.delegate = delegate;
1614+
delegateMemoryAddress = delegate.hasMemoryAddress() ? delegate.memoryAddress() : 0L;
16111615
this.pooled = pooled;
16121616
capacity = delegate.capacity();
16131617
attachToMagazine(magazine);
@@ -2149,6 +2153,9 @@ static final class AdaptiveByteBuf extends AbstractReferenceCountedByteBuf {
21492153
// this both act as adjustment and the start index for a free list segment allocation
21502154
private int startIndex;
21512155
private AbstractByteBuf rootParent;
2156+
// this buffer's memory address, from the root buffer's address that its chunk computed, or 0 when the root
2157+
// buffer has none, see _memoryAddress()
2158+
private long memoryAddress;
21522159
Chunk chunk;
21532160
private int length;
21542161
private int maxFastCapacity;
@@ -2171,6 +2178,8 @@ void init(AbstractByteBuf unwrapped, Chunk wrapped, int readerIndex, int writerI
21712178
setIndex0(readerIndex, writerIndex);
21722179
hasArray = unwrapped.hasArray();
21732180
hasMemoryAddress = unwrapped.hasMemoryAddress();
2181+
// the chunk's delegate is the root buffer
2182+
memoryAddress = hasMemoryAddress ? wrapped.delegateMemoryAddress + startIndex : 0L;
21742183
rootParent = unwrapped;
21752184
tmpNioBuf = null;
21762185

@@ -2287,8 +2296,10 @@ public long memoryAddress() {
22872296

22882297
@Override
22892298
long _memoryAddress() {
2290-
AbstractByteBuf root = rootParent;
2291-
return root != null && hasMemoryAddress ? root._memoryAddress() + startIndex : 0L;
2299+
// Use the address kept at init() instead of asking the root buffer, which would check its accessibility
2300+
// on every call. The root stays accessible while this buffer holds a segment of its chunk. The address is
2301+
// 0 when the root buffer has none, and after deallocation.
2302+
return memoryAddress;
22922303
}
22932304

22942305
@Override
@@ -2668,6 +2679,7 @@ protected void deallocate() {
26682679
tmpNioBuf = null;
26692680
chunk = null;
26702681
rootParent = null;
2682+
memoryAddress = 0L;
26712683
handle.unguardedRecycle(this);
26722684
}
26732685
}
Collapse file

‎buffer/src/test/java/io/netty/buffer/AdaptiveByteBufAllocatorTest.java‎

Copy file name to clipboardExpand all lines: buffer/src/test/java/io/netty/buffer/AdaptiveByteBufAllocatorTest.java
+41Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import io.netty.util.NettyRuntime;
1919
import io.netty.util.concurrent.FastThreadLocalThread;
20+
import io.netty.util.internal.PlatformDependent;
2021
import io.netty.util.test.DisabledForSlowLeakDetection;
2122
import org.junit.jupiter.api.RepeatedTest;
2223
import org.junit.jupiter.api.RepetitionInfo;
@@ -44,6 +45,7 @@
4445
import static org.junit.jupiter.api.Assertions.assertThrows;
4546
import static org.junit.jupiter.api.Assertions.assertTrue;
4647
import static org.junit.jupiter.api.Assertions.fail;
48+
import static org.junit.jupiter.api.Assumptions.assumeTrue;
4749
import io.netty.buffer.AbstractByteBufTest.TestGatheringByteChannel;
4850

4951
public class AdaptiveByteBufAllocatorTest extends AbstractByteBufAllocatorTest<AdaptiveByteBufAllocator> {
@@ -82,6 +84,45 @@ public void testUnsafeHeapBufferAndUnsafeDirectBuffer() {
8284
heapBuffer.release();
8385
}
8486

87+
@Test
88+
public void testDirectBufferMemoryAddressFollowsItsSegment() {
89+
AdaptiveByteBufAllocator allocator = newAllocator(true);
90+
// A direct buffer's address is the chunk's address plus the buffer's offset in the chunk. The second buffer
91+
// is likely at an offset above zero.
92+
ByteBuf first = allocator.directBuffer(16, 1 << 20);
93+
ByteBuf second = allocator.directBuffer(16, 1 << 20);
94+
AbstractByteBuf unwrappedSecond = unwrapToAbstractByteBuf(second);
95+
try {
96+
assumeTrue(first.hasMemoryAddress());
97+
assumeTrue(PlatformDependent.hasDirectByteBufferAddress(first.nioBuffer(0, first.capacity())));
98+
assertMemoryAddressMatchesTheNioBuffer(first);
99+
assertMemoryAddressMatchesTheNioBuffer(second);
100+
// growing beyond its segment moves the buffer to another segment, with another address
101+
second.ensureWritable(128 * 1024);
102+
assertMemoryAddressMatchesTheNioBuffer(second);
103+
} finally {
104+
first.release();
105+
second.release();
106+
}
107+
// a released buffer has no address
108+
assertEquals(0L, unwrappedSecond._memoryAddress());
109+
}
110+
111+
// The NIO buffer's address is computed from the chunk's buffer separately from memoryAddress(), and comparing the
112+
// addresses avoids reading memory through a wrong one.
113+
private static void assertMemoryAddressMatchesTheNioBuffer(ByteBuf buf) {
114+
assertEquals(PlatformDependent.directBufferAddress(buf.nioBuffer(0, buf.capacity())), buf.memoryAddress());
115+
}
116+
117+
// with leak detection, the allocator wraps the AdaptiveByteBuf
118+
private static AbstractByteBuf unwrapToAbstractByteBuf(ByteBuf buf) {
119+
ByteBuf unwrapped = buf;
120+
while (!(unwrapped instanceof AbstractByteBuf)) {
121+
unwrapped = unwrapped.unwrap();
122+
}
123+
return (AbstractByteBuf) unwrapped;
124+
}
125+
85126
@Override
86127
@Test
87128
public void testUsedDirectMemory() {
Collapse file

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

Copy file name to clipboard
+70Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright 2026 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.ByteBuf;
20+
import io.netty.microbench.util.AbstractMicrobenchmark;
21+
import org.openjdk.jmh.annotations.Benchmark;
22+
import org.openjdk.jmh.annotations.Param;
23+
import org.openjdk.jmh.annotations.Scope;
24+
import org.openjdk.jmh.annotations.Setup;
25+
import org.openjdk.jmh.annotations.State;
26+
import org.openjdk.jmh.annotations.TearDown;
27+
28+
/**
29+
* Gets the memory addresses of direct {@link AdaptiveByteBufAllocator} buffers, as a gathering write to a socket does
30+
* for each buffer that it writes. It measures the throughput of the calls with warm caches at a monomorphic call site.
31+
*/
32+
@State(Scope.Benchmark)
33+
public class AdaptiveByteBufMemoryAddressBenchmark extends AbstractMicrobenchmark {
34+
35+
@Param({ "16" })
36+
public int buffers;
37+
38+
@Param({ "8192" })
39+
public int size;
40+
41+
private ByteBuf[] bufs;
42+
43+
@Setup
44+
public void setup() {
45+
AdaptiveByteBufAllocator allocator = new AdaptiveByteBufAllocator();
46+
bufs = new ByteBuf[buffers];
47+
for (int i = 0; i < buffers; i++) {
48+
bufs[i] = allocator.directBuffer(size);
49+
if (!bufs[i].hasMemoryAddress()) {
50+
throw new IllegalStateException("The buffers have no memory address");
51+
}
52+
}
53+
}
54+
55+
@TearDown
56+
public void tearDown() {
57+
for (ByteBuf buf : bufs) {
58+
buf.release();
59+
}
60+
}
61+
62+
@Benchmark
63+
public long memoryAddress() {
64+
long sum = 0;
65+
for (ByteBuf buf : bufs) {
66+
sum += buf.memoryAddress() + buf.readerIndex();
67+
}
68+
return sum;
69+
}
70+
}

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.