从字节数组中的偏移量读取 32 位整数的最快方法

Fastest way to read a 32-bit integer from an offset in a byte array

之前在Java中关于从字节数组读取整数的讨论集中在你拥有的是四个字节的场景。我的情况略有不同:

执行此操作最快的方法是什么?显然我可以手写 read-shift 循环,但是有更快的习惯用法吗?或者,如果要手动完成,哪种变体生成的代码最快?

如果重要的话,我正在使用 OpenJDK 17。

如果您不能或不希望使用内部 类,您可以使用 VarHandle 来访问它:

private static final VarHandle READ_ARRAY = MethodHandles.byteArrayViewVarHandle(int[].class, ByteOrder.nativeOrder());

public static void main(String[] args) {
    byte[] arr = ...;
    
    int pos = ...; // pos is the index into the byte array, and may be unaligned.
    int result = (int) READ_ARRAY.get(arr, pos);


    System.out.println(result);
}

虽然这增加了一些间接性,但最终它会在支持时调用 Unsafe.unalignedAccess()
如果您使用最佳实践(VarHandlestatic final 字段中......),JIT 通常可以将所有内容内联到 Unsafe.unalignedAccess().