GeezerVison is showing github.com — Improve multi-byte access performance when UNALIGNED availability is … · netty/netty@0ee9723
File treeExpand file treeCollapse file tree Open diff view settings buffer/src/main/java/io/netty/buffer common/src/main/java/io/netty/util/internal microbench/src/main/java/io/netty
Expand file treeCollapse file tree Open diff view settings Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -74,6 +74,7 @@ protected byte[] initialValue() throws Exception { | 74 | 74 |
| 75 | 75 | static final int WRITE_CHUNK_SIZE = 8192; | 76 | 76 | static final ByteBufAllocator DEFAULT_ALLOCATOR; | | 77 | + private static final boolean SWAR_UNALIGNED = PlatformDependent.canUnalignedAccess(); | 77 | 78 |
| 78 | 79 | static { | 79 | 80 | String allocType = SystemPropertyUtil.get( | @@ -576,10 +577,10 @@ static int firstIndexOf(AbstractByteBuf buffer, int fromIndex, int toIndex, byte | 576 | 577 | } | 577 | 578 | final int length = toIndex - fromIndex; | 578 | 579 | buffer.checkIndex(fromIndex, length); | 579 | | - if (!PlatformDependent.isUnaligned()) { | | 580 | + if (!SWAR_UNALIGNED) { | 580 | 581 | return linearFirstIndexOf(buffer, fromIndex, toIndex, value); | 581 | 582 | } | 582 | | - assert PlatformDependent.isUnaligned(); | | 583 | + assert SWAR_UNALIGNED; | 583 | 584 | int offset = fromIndex; | 584 | 585 | final int byteCount = length & 7; | 585 | 586 | if (byteCount > 0) { | @@ -729,7 +730,7 @@ static int lastIndexOf(final AbstractByteBuf buffer, int fromIndex, final int to | 729 | 730 | } | 730 | 731 | final int length = fromIndex - toIndex; | 731 | 732 | buffer.checkIndex(toIndex, length); | 732 | | - if (!PlatformDependent.isUnaligned()) { | | 733 | + if (!SWAR_UNALIGNED) { | 733 | 734 | return linearLastIndexOf(buffer, fromIndex, toIndex, value); | 734 | 735 | } | 735 | 736 | final int longCount = length >>> 3; | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
| 31 | 31 | */ | 32 | 32 | public class UnpooledUnsafeDirectByteBuf extends UnpooledDirectByteBuf { | 33 | 33 |
| | 34 | + private static final boolean USE_VAR_HANDLE = PlatformDependent.useVarHandleForMultiByteAccess(); | | 35 | + | 34 | 36 | long memoryAddress; | 35 | 37 |
| 36 | 38 | /** | @@ -126,11 +128,17 @@ public short getShort(int index) { | 126 | 128 |
| 127 | 129 | @Override | 128 | 130 | protected short _getShort(int index) { | | 131 | + if (USE_VAR_HANDLE) { | | 132 | + return VarHandleByteBufferAccess.getShortBE(buffer, index); | | 133 | + } | 129 | 134 | return UnsafeByteBufUtil.getShort(addr(index)); | 130 | 135 | } | 131 | 136 |
| 132 | 137 | @Override | 133 | 138 | protected short _getShortLE(int index) { | | 139 | + if (USE_VAR_HANDLE) { | | 140 | + return VarHandleByteBufferAccess.getShortLE(buffer, index); | | 141 | + } | 134 | 142 | return UnsafeByteBufUtil.getShortLE(addr(index)); | 135 | 143 | } | 136 | 144 |
| @@ -158,11 +166,17 @@ public int getInt(int index) { | 158 | 166 |
| 159 | 167 | @Override | 160 | 168 | protected int _getInt(int index) { | | 169 | + if (USE_VAR_HANDLE) { | | 170 | + return VarHandleByteBufferAccess.getIntBE(buffer, index); | | 171 | + } | 161 | 172 | return UnsafeByteBufUtil.getInt(addr(index)); | 162 | 173 | } | 163 | 174 |
| 164 | 175 | @Override | 165 | 176 | protected int _getIntLE(int index) { | | 177 | + if (USE_VAR_HANDLE) { | | 178 | + return VarHandleByteBufferAccess.getIntLE(buffer, index); | | 179 | + } | 166 | 180 | return UnsafeByteBufUtil.getIntLE(addr(index)); | 167 | 181 | } | 168 | 182 |
| @@ -174,11 +188,17 @@ public long getLong(int index) { | 174 | 188 |
| 175 | 189 | @Override | 176 | 190 | protected long _getLong(int index) { | | 191 | + if (USE_VAR_HANDLE) { | | 192 | + return VarHandleByteBufferAccess.getLongBE(buffer, index); | | 193 | + } | 177 | 194 | return UnsafeByteBufUtil.getLong(addr(index)); | 178 | 195 | } | 179 | 196 |
| 180 | 197 | @Override | 181 | 198 | protected long _getLongLE(int index) { | | 199 | + if (USE_VAR_HANDLE) { | | 200 | + return VarHandleByteBufferAccess.getLongLE(buffer, index); | | 201 | + } | 182 | 202 | return UnsafeByteBufUtil.getLongLE(addr(index)); | 183 | 203 | } | 184 | 204 |
| @@ -219,11 +239,19 @@ public ByteBuf setShort(int index, int value) { | 219 | 239 |
| 220 | 240 | @Override | 221 | 241 | protected void _setShort(int index, int value) { | | 242 | + if (USE_VAR_HANDLE) { | | 243 | + VarHandleByteBufferAccess.setShortBE(buffer, index, value); | | 244 | + return; | | 245 | + } | 222 | 246 | UnsafeByteBufUtil.setShort(addr(index), value); | 223 | 247 | } | 224 | 248 |
| 225 | 249 | @Override | 226 | 250 | protected void _setShortLE(int index, int value) { | | 251 | + if (USE_VAR_HANDLE) { | | 252 | + VarHandleByteBufferAccess.setShortLE(buffer, index, value); | | 253 | + return; | | 254 | + } | 227 | 255 | UnsafeByteBufUtil.setShortLE(addr(index), value); | 228 | 256 | } | 229 | 257 |
| @@ -253,11 +281,19 @@ public ByteBuf setInt(int index, int value) { | 253 | 281 |
| 254 | 282 | @Override | 255 | 283 | protected void _setInt(int index, int value) { | | 284 | + if (USE_VAR_HANDLE) { | | 285 | + VarHandleByteBufferAccess.setIntBE(buffer, index, value); | | 286 | + return; | | 287 | + } | 256 | 288 | UnsafeByteBufUtil.setInt(addr(index), value); | 257 | 289 | } | 258 | 290 |
| 259 | 291 | @Override | 260 | 292 | protected void _setIntLE(int index, int value) { | | 293 | + if (USE_VAR_HANDLE) { | | 294 | + VarHandleByteBufferAccess.setIntLE(buffer, index, value); | | 295 | + return; | | 296 | + } | 261 | 297 | UnsafeByteBufUtil.setIntLE(addr(index), value); | 262 | 298 | } | 263 | 299 |
| @@ -270,11 +306,19 @@ public ByteBuf setLong(int index, long value) { | 270 | 306 |
| 271 | 307 | @Override | 272 | 308 | protected void _setLong(int index, long value) { | | 309 | + if (USE_VAR_HANDLE) { | | 310 | + VarHandleByteBufferAccess.setLongBE(buffer, index, value); | | 311 | + return; | | 312 | + } | 273 | 313 | UnsafeByteBufUtil.setLong(addr(index), value); | 274 | 314 | } | 275 | 315 |
| 276 | 316 | @Override | 277 | 317 | protected void _setLongLE(int index, long value) { | | 318 | + if (USE_VAR_HANDLE) { | | 319 | + VarHandleByteBufferAccess.setLongLE(buffer, index, value); | | 320 | + return; | | 321 | + } | 278 | 322 | UnsafeByteBufUtil.setLongLE(addr(index), value); | 279 | 323 | } | 280 | 324 |
| |
Collapse file 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 number | Diff line number | Diff line change |
|---|
| 33 | 33 | */ | 34 | 34 | final class UnsafeByteBufUtil { | 35 | 35 | private static final boolean UNALIGNED = PlatformDependent.isUnaligned(); | | 36 | + private static final boolean USE_VAR_HANDLE = PlatformDependent.useVarHandleForMultiByteAccess(); | 36 | 37 | private static final byte ZERO = 0; | 37 | 38 | private static final int MAX_HAND_ROLLED_SET_ZERO_BYTES = 64; | 38 | 39 |
| @@ -237,6 +238,9 @@ static short getShort(byte[] array, int index) { | 237 | 238 | short v = PlatformDependent.getShort(array, index); | 238 | 239 | return BIG_ENDIAN_NATIVE_ORDER ? v : Short.reverseBytes(v); | 239 | 240 | } | | 241 | + if (USE_VAR_HANDLE) { | | 242 | + return VarHandleByteBufferAccess.getShortBE(array, index); | | 243 | + } | 240 | 244 | return (short) (PlatformDependent.getByte(array, index) << 8 | | 241 | 245 | PlatformDependent.getByte(array, index + 1) & 0xff); | 242 | 246 | } | @@ -246,6 +250,9 @@ static short getShortLE(byte[] array, int index) { | 246 | 250 | short v = PlatformDependent.getShort(array, index); | 247 | 251 | return BIG_ENDIAN_NATIVE_ORDER ? Short.reverseBytes(v) : v; | 248 | 252 | } | | 253 | + if (USE_VAR_HANDLE) { | | 254 | + return VarHandleByteBufferAccess.getShortLE(array, index); | | 255 | + } | 249 | 256 | return (short) (PlatformDependent.getByte(array, index) & 0xff | | 250 | 257 | PlatformDependent.getByte(array, index + 1) << 8); | 251 | 258 | } | @@ -278,6 +285,9 @@ static int getInt(byte[] array, int index) { | 278 | 285 | int v = PlatformDependent.getInt(array, index); | 279 | 286 | return BIG_ENDIAN_NATIVE_ORDER ? v : Integer.reverseBytes(v); | 280 | 287 | } | | 288 | + if (USE_VAR_HANDLE) { | | 289 | + return VarHandleByteBufferAccess.getIntBE(array, index); | | 290 | + } | 281 | 291 | return PlatformDependent.getByte(array, index) << 24 | | 282 | 292 | (PlatformDependent.getByte(array, index + 1) & 0xff) << 16 | | 283 | 293 | (PlatformDependent.getByte(array, index + 2) & 0xff) << 8 | | @@ -289,6 +299,9 @@ static int getIntLE(byte[] array, int index) { | 289 | 299 | int v = PlatformDependent.getInt(array, index); | 290 | 300 | return BIG_ENDIAN_NATIVE_ORDER ? Integer.reverseBytes(v) : v; | 291 | 301 | } | | 302 | + if (USE_VAR_HANDLE) { | | 303 | + return VarHandleByteBufferAccess.getIntLE(array, index); | | 304 | + } | 292 | 305 | return PlatformDependent.getByte(array, index) & 0xff | | 293 | 306 | (PlatformDependent.getByte(array, index + 1) & 0xff) << 8 | | 294 | 307 | (PlatformDependent.getByte(array, index + 2) & 0xff) << 16 | | @@ -300,6 +313,9 @@ static long getLong(byte[] array, int index) { | 300 | 313 | long v = PlatformDependent.getLong(array, index); | 301 | 314 | return BIG_ENDIAN_NATIVE_ORDER ? v : Long.reverseBytes(v); | 302 | 315 | } | | 316 | + if (USE_VAR_HANDLE) { | | 317 | + return VarHandleByteBufferAccess.getLongBE(array, index); | | 318 | + } | 303 | 319 | return ((long) PlatformDependent.getByte(array, index)) << 56 | | 304 | 320 | (PlatformDependent.getByte(array, index + 1) & 0xffL) << 48 | | 305 | 321 | (PlatformDependent.getByte(array, index + 2) & 0xffL) << 40 | | @@ -315,6 +331,9 @@ static long getLongLE(byte[] array, int index) { | 315 | 331 | long v = PlatformDependent.getLong(array, index); | 316 | 332 | return BIG_ENDIAN_NATIVE_ORDER ? Long.reverseBytes(v) : v; | 317 | 333 | } | | 334 | + if (USE_VAR_HANDLE) { | | 335 | + return VarHandleByteBufferAccess.getLongLE(array, index); | | 336 | + } | 318 | 337 | return PlatformDependent.getByte(array, index) & 0xffL | | 319 | 338 | (PlatformDependent.getByte(array, index + 1) & 0xffL) << 8 | | 320 | 339 | (PlatformDependent.getByte(array, index + 2) & 0xffL) << 16 | | @@ -333,6 +352,8 @@ static void setShort(byte[] array, int index, int value) { | 333 | 352 | if (UNALIGNED) { | 334 | 353 | PlatformDependent.putShort(array, index, | 335 | 354 | BIG_ENDIAN_NATIVE_ORDER ? (short) value : Short.reverseBytes((short) value)); | | 355 | + } else if (USE_VAR_HANDLE) { | | 356 | + VarHandleByteBufferAccess.setShortBE(array, index, value); | 336 | 357 | } else { | 337 | 358 | PlatformDependent.putByte(array, index, (byte) (value >>> 8)); | 338 | 359 | PlatformDependent.putByte(array, index + 1, (byte) value); | @@ -343,6 +364,8 @@ static void setShortLE(byte[] array, int index, int value) { | 343 | 364 | if (UNALIGNED) { | 344 | 365 | PlatformDependent.putShort(array, index, | 345 | 366 | BIG_ENDIAN_NATIVE_ORDER ? Short.reverseBytes((short) value) : (short) value); | | 367 | + } else if (USE_VAR_HANDLE) { | | 368 | + VarHandleByteBufferAccess.setShortLE(array, index, value); | 346 | 369 | } else { | 347 | 370 | PlatformDependent.putByte(array, index, (byte) value); | 348 | 371 | PlatformDependent.putByte(array, index + 1, (byte) (value >>> 8)); | @@ -376,6 +399,8 @@ static void setMediumLE(byte[] array, int index, int value) { | 376 | 399 | static void setInt(byte[] array, int index, int value) { | 377 | 400 | if (UNALIGNED) { | 378 | 401 | PlatformDependent.putInt(array, index, BIG_ENDIAN_NATIVE_ORDER ? value : Integer.reverseBytes(value)); | | 402 | + } else if (USE_VAR_HANDLE) { | | 403 | + VarHandleByteBufferAccess.setIntBE(array, index, value); | 379 | 404 | } else { | 380 | 405 | PlatformDependent.putByte(array, index, (byte) (value >>> 24)); | 381 | 406 | PlatformDependent.putByte(array, index + 1, (byte) (value >>> 16)); | @@ -387,6 +412,8 @@ static void setInt(byte[] array, int index, int value) { | 387 | 412 | static void setIntLE(byte[] array, int index, int value) { | 388 | 413 | if (UNALIGNED) { | 389 | 414 | PlatformDependent.putInt(array, index, BIG_ENDIAN_NATIVE_ORDER ? Integer.reverseBytes(value) : value); | | 415 | + } else if (USE_VAR_HANDLE) { | | 416 | + VarHandleByteBufferAccess.setIntLE(array, index, value); | 390 | 417 | } else { | 391 | 418 | PlatformDependent.putByte(array, index, (byte) value); | 392 | 419 | PlatformDependent.putByte(array, index + 1, (byte) (value >>> 8)); | @@ -398,6 +425,8 @@ static void setIntLE(byte[] array, int index, int value) { | 398 | 425 | static void setLong(byte[] array, int index, long value) { | 399 | 426 | if (UNALIGNED) { | 400 | 427 | PlatformDependent.putLong(array, index, BIG_ENDIAN_NATIVE_ORDER ? value : Long.reverseBytes(value)); | | 428 | + } else if (USE_VAR_HANDLE) { | | 429 | + VarHandleByteBufferAccess.setLongBE(array, index, value); | 401 | 430 | } else { | 402 | 431 | PlatformDependent.putByte(array, index, (byte) (value >>> 56)); | 403 | 432 | PlatformDependent.putByte(array, index + 1, (byte) (value >>> 48)); | @@ -413,6 +442,8 @@ static void setLong(byte[] array, int index, long value) { | 413 | 442 | static void setLongLE(byte[] array, int index, long value) { | 414 | 443 | if (UNALIGNED) { | 415 | 444 | PlatformDependent.putLong(array, index, BIG_ENDIAN_NATIVE_ORDER ? Long.reverseBytes(value) : value); | | 445 | + } else if (USE_VAR_HANDLE) { | | 446 | + VarHandleByteBufferAccess.setLongLE(array, index, value); | 416 | 447 | } else { | 417 | 448 | PlatformDependent.putByte(array, index, (byte) value); | 418 | 449 | PlatformDependent.putByte(array, index + 1, (byte) (value >>> 8)); | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -264,7 +264,7 @@ public void freeDirectBuffer(ByteBuffer buffer) { | 264 | 264 | } | 265 | 265 |
| 266 | 266 | private static boolean initializeVarHandle() { | 267 | | - if (UNSAFE_UNAVAILABILITY_CAUSE == null || javaVersion() < 9 || | | 267 | + if (javaVersion() < 9 || | 268 | 268 | PlatformDependent0.isNativeImage()) { | 269 | 269 | return false; | 270 | 270 | } | @@ -629,6 +629,26 @@ public static boolean hasVarHandle() { | 629 | 629 | return VAR_HANDLE; | 630 | 630 | } | 631 | 631 |
| | 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 | + | 632 | 652 | public static VarHandle findVarHandleOfIntField(MethodHandles.Lookup lookup, Class<?> type, String fieldName) { | 633 | 653 | if (VAR_HANDLE) { | 634 | 654 | return VarHandleFactory.privateFindVarHandle(lookup, type, fieldName, int.class); | |
Collapse file 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 number | Diff line number | Diff line change |
|---|
@@ -336,11 +336,19 @@ public Object run() { | 336 | 336 | LONG_ARRAY_BASE_OFFSET = UNSAFE.arrayBaseOffset(long[].class); | 337 | 337 | LONG_ARRAY_INDEX_SCALE = UNSAFE.arrayIndexScale(long[].class); | 338 | 338 | final boolean unaligned; | | 339 | + String unalignedProperty = SystemPropertyUtil.get("io.netty.unalignedAccess", "").trim(); | | 340 | + | 339 | 341 | // using a known type to avoid loading new classes | 340 | 342 | final AtomicLong maybeMaxMemory = new AtomicLong(-1); | 341 | 343 | Object maybeUnaligned = AccessController.doPrivileged(new PrivilegedAction<Object>() { | 342 | 344 | @Override | 343 | 345 | public Object run() { | | 346 | + if ("true".equalsIgnoreCase(unalignedProperty)) { | | 347 | + return Boolean.TRUE; | | 348 | + } | | 349 | + if ("false".equalsIgnoreCase(unalignedProperty)) { | | 350 | + return Boolean.FALSE; | | 351 | + } | 344 | 352 | try { | 345 | 353 | Class<?> bitsClass = | 346 | 354 | Class.forName("java.nio.Bits", false, getSystemClassLoader()); | |
Collapse file Copy file name to clipboard +66Lines changed: 66 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 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 | +} |
|