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 — Improve multi-byte access performance when UNALIGNED availability is … · netty/netty@0ee9723

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • buffer/src/main/java/io/netty/buffer
    • ByteBufUtil.java
    • UnpooledUnsafeDirectByteBuf.java
    • UnsafeByteBufUtil.java
  • common/src/main/java/io/netty/util/internal
    • PlatformDependent.java
    • PlatformDependent0.java
  • microbench/src/main/java/io/netty
    • buffer
      • VarHandleAccessBenchmark.java
    • microbench/search
      • UnalignedIndexOfBenchmark.java
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎buffer/src/main/java/io/netty/buffer/ByteBufUtil.java‎

Copy file name to clipboardExpand all lines: buffer/src/main/java/io/netty/buffer/ByteBufUtil.java
+4-3Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ protected byte[] initialValue() throws Exception {
7474

7575
static final int WRITE_CHUNK_SIZE = 8192;
7676
static final ByteBufAllocator DEFAULT_ALLOCATOR;
77+
private static final boolean SWAR_UNALIGNED = PlatformDependent.canUnalignedAccess();
7778

7879
static {
7980
String allocType = SystemPropertyUtil.get(
@@ -576,10 +577,10 @@ static int firstIndexOf(AbstractByteBuf buffer, int fromIndex, int toIndex, byte
576577
}
577578
final int length = toIndex - fromIndex;
578579
buffer.checkIndex(fromIndex, length);
579-
if (!PlatformDependent.isUnaligned()) {
580+
if (!SWAR_UNALIGNED) {
580581
return linearFirstIndexOf(buffer, fromIndex, toIndex, value);
581582
}
582-
assert PlatformDependent.isUnaligned();
583+
assert SWAR_UNALIGNED;
583584
int offset = fromIndex;
584585
final int byteCount = length & 7;
585586
if (byteCount > 0) {
@@ -729,7 +730,7 @@ static int lastIndexOf(final AbstractByteBuf buffer, int fromIndex, final int to
729730
}
730731
final int length = fromIndex - toIndex;
731732
buffer.checkIndex(toIndex, length);
732-
if (!PlatformDependent.isUnaligned()) {
733+
if (!SWAR_UNALIGNED) {
733734
return linearLastIndexOf(buffer, fromIndex, toIndex, value);
734735
}
735736
final int longCount = length >>> 3;
Collapse file

‎buffer/src/main/java/io/netty/buffer/UnpooledUnsafeDirectByteBuf.java‎

Copy file name to clipboardExpand all lines: buffer/src/main/java/io/netty/buffer/UnpooledUnsafeDirectByteBuf.java
+44Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
*/
3232
public class UnpooledUnsafeDirectByteBuf extends UnpooledDirectByteBuf {
3333

34+
private static final boolean USE_VAR_HANDLE = PlatformDependent.useVarHandleForMultiByteAccess();
35+
3436
long memoryAddress;
3537

3638
/**
@@ -126,11 +128,17 @@ public short getShort(int index) {
126128

127129
@Override
128130
protected short _getShort(int index) {
131+
if (USE_VAR_HANDLE) {
132+
return VarHandleByteBufferAccess.getShortBE(buffer, index);
133+
}
129134
return UnsafeByteBufUtil.getShort(addr(index));
130135
}
131136

132137
@Override
133138
protected short _getShortLE(int index) {
139+
if (USE_VAR_HANDLE) {
140+
return VarHandleByteBufferAccess.getShortLE(buffer, index);
141+
}
134142
return UnsafeByteBufUtil.getShortLE(addr(index));
135143
}
136144

@@ -158,11 +166,17 @@ public int getInt(int index) {
158166

159167
@Override
160168
protected int _getInt(int index) {
169+
if (USE_VAR_HANDLE) {
170+
return VarHandleByteBufferAccess.getIntBE(buffer, index);
171+
}
161172
return UnsafeByteBufUtil.getInt(addr(index));
162173
}
163174

164175
@Override
165176
protected int _getIntLE(int index) {
177+
if (USE_VAR_HANDLE) {
178+
return VarHandleByteBufferAccess.getIntLE(buffer, index);
179+
}
166180
return UnsafeByteBufUtil.getIntLE(addr(index));
167181
}
168182

@@ -174,11 +188,17 @@ public long getLong(int index) {
174188

175189
@Override
176190
protected long _getLong(int index) {
191+
if (USE_VAR_HANDLE) {
192+
return VarHandleByteBufferAccess.getLongBE(buffer, index);
193+
}
177194
return UnsafeByteBufUtil.getLong(addr(index));
178195
}
179196

180197
@Override
181198
protected long _getLongLE(int index) {
199+
if (USE_VAR_HANDLE) {
200+
return VarHandleByteBufferAccess.getLongLE(buffer, index);
201+
}
182202
return UnsafeByteBufUtil.getLongLE(addr(index));
183203
}
184204

@@ -219,11 +239,19 @@ public ByteBuf setShort(int index, int value) {
219239

220240
@Override
221241
protected void _setShort(int index, int value) {
242+
if (USE_VAR_HANDLE) {
243+
VarHandleByteBufferAccess.setShortBE(buffer, index, value);
244+
return;
245+
}
222246
UnsafeByteBufUtil.setShort(addr(index), value);
223247
}
224248

225249
@Override
226250
protected void _setShortLE(int index, int value) {
251+
if (USE_VAR_HANDLE) {
252+
VarHandleByteBufferAccess.setShortLE(buffer, index, value);
253+
return;
254+
}
227255
UnsafeByteBufUtil.setShortLE(addr(index), value);
228256
}
229257

@@ -253,11 +281,19 @@ public ByteBuf setInt(int index, int value) {
253281

254282
@Override
255283
protected void _setInt(int index, int value) {
284+
if (USE_VAR_HANDLE) {
285+
VarHandleByteBufferAccess.setIntBE(buffer, index, value);
286+
return;
287+
}
256288
UnsafeByteBufUtil.setInt(addr(index), value);
257289
}
258290

259291
@Override
260292
protected void _setIntLE(int index, int value) {
293+
if (USE_VAR_HANDLE) {
294+
VarHandleByteBufferAccess.setIntLE(buffer, index, value);
295+
return;
296+
}
261297
UnsafeByteBufUtil.setIntLE(addr(index), value);
262298
}
263299

@@ -270,11 +306,19 @@ public ByteBuf setLong(int index, long value) {
270306

271307
@Override
272308
protected void _setLong(int index, long value) {
309+
if (USE_VAR_HANDLE) {
310+
VarHandleByteBufferAccess.setLongBE(buffer, index, value);
311+
return;
312+
}
273313
UnsafeByteBufUtil.setLong(addr(index), value);
274314
}
275315

276316
@Override
277317
protected void _setLongLE(int index, long value) {
318+
if (USE_VAR_HANDLE) {
319+
VarHandleByteBufferAccess.setLongLE(buffer, index, value);
320+
return;
321+
}
278322
UnsafeByteBufUtil.setLongLE(addr(index), value);
279323
}
280324

Collapse file

‎buffer/src/main/java/io/netty/buffer/UnsafeByteBufUtil.java‎

Copy file name to clipboardExpand all lines: buffer/src/main/java/io/netty/buffer/UnsafeByteBufUtil.java
+31Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
*/
3434
final class UnsafeByteBufUtil {
3535
private static final boolean UNALIGNED = PlatformDependent.isUnaligned();
36+
private static final boolean USE_VAR_HANDLE = PlatformDependent.useVarHandleForMultiByteAccess();
3637
private static final byte ZERO = 0;
3738
private static final int MAX_HAND_ROLLED_SET_ZERO_BYTES = 64;
3839

@@ -237,6 +238,9 @@ static short getShort(byte[] array, int index) {
237238
short v = PlatformDependent.getShort(array, index);
238239
return BIG_ENDIAN_NATIVE_ORDER ? v : Short.reverseBytes(v);
239240
}
241+
if (USE_VAR_HANDLE) {
242+
return VarHandleByteBufferAccess.getShortBE(array, index);
243+
}
240244
return (short) (PlatformDependent.getByte(array, index) << 8 |
241245
PlatformDependent.getByte(array, index + 1) & 0xff);
242246
}
@@ -246,6 +250,9 @@ static short getShortLE(byte[] array, int index) {
246250
short v = PlatformDependent.getShort(array, index);
247251
return BIG_ENDIAN_NATIVE_ORDER ? Short.reverseBytes(v) : v;
248252
}
253+
if (USE_VAR_HANDLE) {
254+
return VarHandleByteBufferAccess.getShortLE(array, index);
255+
}
249256
return (short) (PlatformDependent.getByte(array, index) & 0xff |
250257
PlatformDependent.getByte(array, index + 1) << 8);
251258
}
@@ -278,6 +285,9 @@ static int getInt(byte[] array, int index) {
278285
int v = PlatformDependent.getInt(array, index);
279286
return BIG_ENDIAN_NATIVE_ORDER ? v : Integer.reverseBytes(v);
280287
}
288+
if (USE_VAR_HANDLE) {
289+
return VarHandleByteBufferAccess.getIntBE(array, index);
290+
}
281291
return PlatformDependent.getByte(array, index) << 24 |
282292
(PlatformDependent.getByte(array, index + 1) & 0xff) << 16 |
283293
(PlatformDependent.getByte(array, index + 2) & 0xff) << 8 |
@@ -289,6 +299,9 @@ static int getIntLE(byte[] array, int index) {
289299
int v = PlatformDependent.getInt(array, index);
290300
return BIG_ENDIAN_NATIVE_ORDER ? Integer.reverseBytes(v) : v;
291301
}
302+
if (USE_VAR_HANDLE) {
303+
return VarHandleByteBufferAccess.getIntLE(array, index);
304+
}
292305
return PlatformDependent.getByte(array, index) & 0xff |
293306
(PlatformDependent.getByte(array, index + 1) & 0xff) << 8 |
294307
(PlatformDependent.getByte(array, index + 2) & 0xff) << 16 |
@@ -300,6 +313,9 @@ static long getLong(byte[] array, int index) {
300313
long v = PlatformDependent.getLong(array, index);
301314
return BIG_ENDIAN_NATIVE_ORDER ? v : Long.reverseBytes(v);
302315
}
316+
if (USE_VAR_HANDLE) {
317+
return VarHandleByteBufferAccess.getLongBE(array, index);
318+
}
303319
return ((long) PlatformDependent.getByte(array, index)) << 56 |
304320
(PlatformDependent.getByte(array, index + 1) & 0xffL) << 48 |
305321
(PlatformDependent.getByte(array, index + 2) & 0xffL) << 40 |
@@ -315,6 +331,9 @@ static long getLongLE(byte[] array, int index) {
315331
long v = PlatformDependent.getLong(array, index);
316332
return BIG_ENDIAN_NATIVE_ORDER ? Long.reverseBytes(v) : v;
317333
}
334+
if (USE_VAR_HANDLE) {
335+
return VarHandleByteBufferAccess.getLongLE(array, index);
336+
}
318337
return PlatformDependent.getByte(array, index) & 0xffL |
319338
(PlatformDependent.getByte(array, index + 1) & 0xffL) << 8 |
320339
(PlatformDependent.getByte(array, index + 2) & 0xffL) << 16 |
@@ -333,6 +352,8 @@ static void setShort(byte[] array, int index, int value) {
333352
if (UNALIGNED) {
334353
PlatformDependent.putShort(array, index,
335354
BIG_ENDIAN_NATIVE_ORDER ? (short) value : Short.reverseBytes((short) value));
355+
} else if (USE_VAR_HANDLE) {
356+
VarHandleByteBufferAccess.setShortBE(array, index, value);
336357
} else {
337358
PlatformDependent.putByte(array, index, (byte) (value >>> 8));
338359
PlatformDependent.putByte(array, index + 1, (byte) value);
@@ -343,6 +364,8 @@ static void setShortLE(byte[] array, int index, int value) {
343364
if (UNALIGNED) {
344365
PlatformDependent.putShort(array, index,
345366
BIG_ENDIAN_NATIVE_ORDER ? Short.reverseBytes((short) value) : (short) value);
367+
} else if (USE_VAR_HANDLE) {
368+
VarHandleByteBufferAccess.setShortLE(array, index, value);
346369
} else {
347370
PlatformDependent.putByte(array, index, (byte) value);
348371
PlatformDependent.putByte(array, index + 1, (byte) (value >>> 8));
@@ -376,6 +399,8 @@ static void setMediumLE(byte[] array, int index, int value) {
376399
static void setInt(byte[] array, int index, int value) {
377400
if (UNALIGNED) {
378401
PlatformDependent.putInt(array, index, BIG_ENDIAN_NATIVE_ORDER ? value : Integer.reverseBytes(value));
402+
} else if (USE_VAR_HANDLE) {
403+
VarHandleByteBufferAccess.setIntBE(array, index, value);
379404
} else {
380405
PlatformDependent.putByte(array, index, (byte) (value >>> 24));
381406
PlatformDependent.putByte(array, index + 1, (byte) (value >>> 16));
@@ -387,6 +412,8 @@ static void setInt(byte[] array, int index, int value) {
387412
static void setIntLE(byte[] array, int index, int value) {
388413
if (UNALIGNED) {
389414
PlatformDependent.putInt(array, index, BIG_ENDIAN_NATIVE_ORDER ? Integer.reverseBytes(value) : value);
415+
} else if (USE_VAR_HANDLE) {
416+
VarHandleByteBufferAccess.setIntLE(array, index, value);
390417
} else {
391418
PlatformDependent.putByte(array, index, (byte) value);
392419
PlatformDependent.putByte(array, index + 1, (byte) (value >>> 8));
@@ -398,6 +425,8 @@ static void setIntLE(byte[] array, int index, int value) {
398425
static void setLong(byte[] array, int index, long value) {
399426
if (UNALIGNED) {
400427
PlatformDependent.putLong(array, index, BIG_ENDIAN_NATIVE_ORDER ? value : Long.reverseBytes(value));
428+
} else if (USE_VAR_HANDLE) {
429+
VarHandleByteBufferAccess.setLongBE(array, index, value);
401430
} else {
402431
PlatformDependent.putByte(array, index, (byte) (value >>> 56));
403432
PlatformDependent.putByte(array, index + 1, (byte) (value >>> 48));
@@ -413,6 +442,8 @@ static void setLong(byte[] array, int index, long value) {
413442
static void setLongLE(byte[] array, int index, long value) {
414443
if (UNALIGNED) {
415444
PlatformDependent.putLong(array, index, BIG_ENDIAN_NATIVE_ORDER ? Long.reverseBytes(value) : value);
445+
} else if (USE_VAR_HANDLE) {
446+
VarHandleByteBufferAccess.setLongLE(array, index, value);
416447
} else {
417448
PlatformDependent.putByte(array, index, (byte) value);
418449
PlatformDependent.putByte(array, index + 1, (byte) (value >>> 8));
Collapse file

‎common/src/main/java/io/netty/util/internal/PlatformDependent.java‎

Copy file name to clipboardExpand all lines: common/src/main/java/io/netty/util/internal/PlatformDependent.java
+21-1Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ public void freeDirectBuffer(ByteBuffer buffer) {
264264
}
265265

266266
private static boolean initializeVarHandle() {
267-
if (UNSAFE_UNAVAILABILITY_CAUSE == null || javaVersion() < 9 ||
267+
if (javaVersion() < 9 ||
268268
PlatformDependent0.isNativeImage()) {
269269
return false;
270270
}
@@ -629,6 +629,26 @@ public static boolean hasVarHandle() {
629629
return VAR_HANDLE;
630630
}
631631

632+
/**
633+
* {@code true} if {@code VarHandle} should be used for multi-byte access.
634+
*
635+
* The multi-byte access strategy is determined as follows:
636+
* 1) If the platform supports unaligned access natively, use {@code Unsafe} as the fastest option.
637+
* 2) Otherwise, if {@code VarHandle} is available, use it as a fallback.
638+
* 3) Otherwise, fall back to manual byte-by-byte access.
639+
*/
640+
public static boolean useVarHandleForMultiByteAccess() {
641+
return !isUnaligned() && VAR_HANDLE;
642+
}
643+
644+
/**
645+
* {@code true} if multi-byte access at arbitrary offsets is possible, either natively through {@code Unsafe}
646+
* or via {@code VarHandle} where the JVM handles alignment and byte ordering internally.
647+
*/
648+
public static boolean canUnalignedAccess() {
649+
return isUnaligned() || VAR_HANDLE;
650+
}
651+
632652
public static VarHandle findVarHandleOfIntField(MethodHandles.Lookup lookup, Class<?> type, String fieldName) {
633653
if (VAR_HANDLE) {
634654
return VarHandleFactory.privateFindVarHandle(lookup, type, fieldName, int.class);
Collapse file

‎common/src/main/java/io/netty/util/internal/PlatformDependent0.java‎

Copy file name to clipboardExpand all lines: common/src/main/java/io/netty/util/internal/PlatformDependent0.java
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,11 +336,19 @@ public Object run() {
336336
LONG_ARRAY_BASE_OFFSET = UNSAFE.arrayBaseOffset(long[].class);
337337
LONG_ARRAY_INDEX_SCALE = UNSAFE.arrayIndexScale(long[].class);
338338
final boolean unaligned;
339+
String unalignedProperty = SystemPropertyUtil.get("io.netty.unalignedAccess", "").trim();
340+
339341
// using a known type to avoid loading new classes
340342
final AtomicLong maybeMaxMemory = new AtomicLong(-1);
341343
Object maybeUnaligned = AccessController.doPrivileged(new PrivilegedAction<Object>() {
342344
@Override
343345
public Object run() {
346+
if ("true".equalsIgnoreCase(unalignedProperty)) {
347+
return Boolean.TRUE;
348+
}
349+
if ("false".equalsIgnoreCase(unalignedProperty)) {
350+
return Boolean.FALSE;
351+
}
344352
try {
345353
Class<?> bitsClass =
346354
Class.forName("java.nio.Bits", false, getSystemClassLoader());
Collapse file

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

Copy file name to clipboard
+66Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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.buffer;
17+
18+
import io.netty.microbench.util.AbstractMicrobenchmark;
19+
import org.openjdk.jmh.annotations.Benchmark;
20+
import org.openjdk.jmh.annotations.BenchmarkMode;
21+
import org.openjdk.jmh.annotations.Fork;
22+
import org.openjdk.jmh.annotations.Measurement;
23+
import org.openjdk.jmh.annotations.Mode;
24+
import org.openjdk.jmh.annotations.OutputTimeUnit;
25+
import org.openjdk.jmh.annotations.Setup;
26+
import org.openjdk.jmh.annotations.TearDown;
27+
import org.openjdk.jmh.annotations.Warmup;
28+
29+
import java.util.concurrent.TimeUnit;
30+
31+
@Warmup(iterations = 5)
32+
@Measurement(iterations = 5)
33+
@BenchmarkMode(Mode.AverageTime)
34+
@OutputTimeUnit(TimeUnit.NANOSECONDS)
35+
public class VarHandleAccessBenchmark extends AbstractMicrobenchmark {
36+
37+
private ByteBuf unsafeDirect;
38+
private ByteBuf unsafeHeap;
39+
40+
@Setup
41+
public void setup() {
42+
unsafeDirect = new UnpooledUnsafeDirectByteBuf(ByteBufAllocator.DEFAULT, 8, 8);
43+
unsafeHeap = new UnpooledUnsafeHeapByteBuf(ByteBufAllocator.DEFAULT, 8, 8);
44+
unsafeDirect.setLong(0, 1L);
45+
unsafeHeap.setLong(0, 1L);
46+
}
47+
48+
@TearDown
49+
public void tearDown() {
50+
unsafeDirect.release();
51+
unsafeHeap.release();
52+
}
53+
54+
// UNALIGNED = false: should use VarHandle for multi-byte access
55+
@Benchmark
56+
@Fork(value = 1, jvmArgsAppend = "-Dio.netty.unalignedAccess=false")
57+
public long getLongDirectFalse() {
58+
return unsafeDirect.getLong(0);
59+
}
60+
61+
@Benchmark
62+
@Fork(value = 1, jvmArgsAppend = "-Dio.netty.unalignedAccess=false")
63+
public long getLongHeapFalse() {
64+
return unsafeHeap.getLong(0);
65+
}
66+
}

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.