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 — Avoid allocating NIO buffer views in AdaptiveByteBuf.setBytes(int, By… · netty/netty@ee7ab44

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
      • AbstractAdaptiveByteBufTest.java
  • microbench/src/main/java/io/netty/microbench/buffer
    • AdaptiveByteBufSetBytesBenchmark.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
+10Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2481,6 +2481,15 @@ public ByteBuf setBytes(int index, byte[] src, int srcIndex, int length) {
24812481

24822482
@Override
24832483
public ByteBuf setBytes(int index, ByteBuf src, int srcIndex, int length) {
2484+
if (PlatformDependent.hasUnsafe() && hasMemoryAddress) {
2485+
// Copies from a source with a memory address or an array without an NIO buffer view of the source,
2486+
// which src.nioBuffer(...) below would allocate for each copy from a buffer of another kind, such
2487+
// as a slice or a buffer of another allocator. Without Unsafe, a direct buffer can still have a
2488+
// memory address (Java 25+), but the copy needs Unsafe; checking hasUnsafe() first lets the JIT
2489+
// drop this branch then.
2490+
UnsafeByteBufUtil.setBytes(this, _memoryAddress() + index, index, src, srcIndex, length);
2491+
return this;
2492+
}
24842493
checkIndex(index, length);
24852494
if (src instanceof AdaptiveByteBuf && PlatformDependent.javaVersion() >= 16) {
24862495
AdaptiveByteBuf srcBuf = (AdaptiveByteBuf) src;
@@ -2685,6 +2694,7 @@ protected void deallocate() {
26852694
chunk = null;
26862695
rootParent = null;
26872696
memoryAddress = 0L;
2697+
hasMemoryAddress = false;
26882698
handle.unguardedRecycle(this);
26892699
}
26902700
}
Collapse file

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

Copy file name to clipboardExpand all lines: buffer/src/test/java/io/netty/buffer/AbstractAdaptiveByteBufTest.java
+47Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
import org.junit.jupiter.api.Disabled;
1919
import org.junit.jupiter.api.Test;
2020

21+
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
import static org.junit.jupiter.api.Assertions.assertThrows;
23+
2124
public abstract class AbstractAdaptiveByteBufTest extends AbstractPooledByteBufTest {
2225
private final AdaptiveByteBufAllocator allocator = new AdaptiveByteBufAllocator();
2326

@@ -39,4 +42,48 @@ public void testMaxFastWritableBytes() {
3942
@Override
4043
public void testEnsureWritableDoesntGrowTooMuch() {
4144
}
45+
46+
@Test
47+
public void testSetBytesFromBuffersOfOtherKinds() {
48+
final byte[] bytes = new byte[64];
49+
for (int i = 0; i < bytes.length; i++) {
50+
bytes[i] = (byte) (i + 1);
51+
}
52+
ByteBuf pooledDirect = PooledByteBufAllocator.DEFAULT.directBuffer(72).writeZero(8).writeBytes(bytes);
53+
ByteBuf unpooledHeap = Unpooled.buffer(72).writeZero(8).writeBytes(bytes);
54+
ByteBuf adaptive = alloc(72, 72).writeZero(8).writeBytes(bytes);
55+
CompositeByteBuf composite = Unpooled.compositeBuffer().addComponents(true,
56+
Unpooled.directBuffer(32).writeBytes(bytes, 0, 32),
57+
Unpooled.buffer(32).writeBytes(bytes, 32, 32));
58+
ByteBuf wrappedHeap = Unpooled.wrappedBuffer(bytes);
59+
final ByteBuf[] sources = {
60+
pooledDirect.slice(8, 64),
61+
unpooledHeap.slice(8, 64),
62+
adaptive.slice(8, 64),
63+
composite,
64+
wrappedHeap.asReadOnly(),
65+
};
66+
final ByteBuf target = alloc(128, 128);
67+
try {
68+
for (ByteBuf src : sources) {
69+
target.clear().writeZero(128);
70+
target.setBytes(16, src, 4, 40);
71+
for (int i = 0; i < 128; i++) {
72+
byte expected = i >= 16 && i < 56 ? bytes[i - 16 + 4] : 0;
73+
assertEquals(expected, target.getByte(i), "index " + i + " from " + src);
74+
}
75+
assertEquals(0, src.readerIndex());
76+
assertEquals(64, src.writerIndex());
77+
}
78+
assertThrows(IndexOutOfBoundsException.class, () -> target.setBytes(0, sources[0], 40, 40));
79+
assertThrows(IndexOutOfBoundsException.class, () -> target.setBytes(100, sources[0], 0, 40));
80+
} finally {
81+
target.release();
82+
pooledDirect.release();
83+
unpooledHeap.release();
84+
adaptive.release();
85+
composite.release();
86+
wrappedHeap.release();
87+
}
88+
}
4289
}
Collapse file

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

Copy file name to clipboard
+114Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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.buffer.CompositeByteBuf;
21+
import io.netty.buffer.PooledByteBufAllocator;
22+
import io.netty.buffer.Unpooled;
23+
import io.netty.microbench.util.AbstractMicrobenchmark;
24+
import org.openjdk.jmh.annotations.Benchmark;
25+
import org.openjdk.jmh.annotations.Fork;
26+
import org.openjdk.jmh.annotations.Param;
27+
import org.openjdk.jmh.annotations.Setup;
28+
import org.openjdk.jmh.annotations.TearDown;
29+
30+
/**
31+
* Copies into a direct buffer of the {@link AdaptiveByteBufAllocator} from sources of different kinds, as
32+
* {@code target.writeBytes(source)} does. Run with {@code -prof gc} to see the allocations per copy.
33+
*
34+
* <p>Unsafe is enabled explicitly, since Netty disables it by default on Java 25+; to compare without it, pass
35+
* {@code -jvmArgsAppend -Dio.netty.noUnsafe=true}.
36+
*/
37+
@Fork(value = 2, jvmArgsAppend = "-Dio.netty.noUnsafe=false")
38+
public class AdaptiveByteBufSetBytesBenchmark extends AbstractMicrobenchmark {
39+
40+
public enum Source {
41+
ADAPTIVE,
42+
ADAPTIVE_SLICE,
43+
POOLED_DIRECT,
44+
POOLED_DIRECT_SLICE,
45+
UNPOOLED_HEAP,
46+
COMPOSITE
47+
}
48+
49+
@Param({
50+
"64",
51+
"1024",
52+
"16384",
53+
})
54+
private int size;
55+
56+
@Param
57+
private Source source;
58+
59+
private final AdaptiveByteBufAllocator adaptiveAllocator = new AdaptiveByteBufAllocator();
60+
private ByteBuf sourceRoot;
61+
private ByteBuf src;
62+
private ByteBuf target;
63+
64+
@Setup
65+
public void setup() {
66+
switch (source) {
67+
case ADAPTIVE:
68+
sourceRoot = adaptiveAllocator.directBuffer(size, size);
69+
src = sourceRoot;
70+
break;
71+
case ADAPTIVE_SLICE:
72+
sourceRoot = adaptiveAllocator.directBuffer(size + 8, size + 8);
73+
src = sourceRoot.slice(8, size);
74+
break;
75+
case POOLED_DIRECT:
76+
sourceRoot = PooledByteBufAllocator.DEFAULT.directBuffer(size, size);
77+
src = sourceRoot;
78+
break;
79+
case POOLED_DIRECT_SLICE:
80+
sourceRoot = PooledByteBufAllocator.DEFAULT.directBuffer(size + 8, size + 8);
81+
src = sourceRoot.slice(8, size);
82+
break;
83+
case UNPOOLED_HEAP:
84+
sourceRoot = Unpooled.buffer(size, size);
85+
src = sourceRoot;
86+
break;
87+
case COMPOSITE:
88+
CompositeByteBuf composite = Unpooled.compositeBuffer();
89+
composite.addComponents(true,
90+
PooledByteBufAllocator.DEFAULT.directBuffer(size / 2).writeZero(size / 2),
91+
PooledByteBufAllocator.DEFAULT.directBuffer(size - size / 2).writeZero(size - size / 2));
92+
sourceRoot = composite;
93+
src = composite;
94+
break;
95+
default:
96+
throw new IllegalArgumentException(String.valueOf(source));
97+
}
98+
for (int i = 0; i < size; i++) {
99+
src.setByte(i, 0xA5);
100+
}
101+
target = adaptiveAllocator.directBuffer(size, size);
102+
}
103+
104+
@Benchmark
105+
public ByteBuf setBytes() {
106+
return target.setBytes(0, src, 0, size);
107+
}
108+
109+
@TearDown
110+
public void tearDown() {
111+
target.release();
112+
sourceRoot.release();
113+
}
114+
}

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.