RFR: 8349503: Consolidate multi-byte access into ByteArray
Raffaello Giulietti
rgiulietti at openjdk.org
Thu Feb 6 14:09:13 UTC 2025
On Wed, 5 Feb 2025 23:41:19 GMT, Chen Liang <liach at openjdk.org> wrote:
> `MethodHandles.byteArrayViewVarHandle` exposes checked multi-byte access to byte arrays via VarHandle. This larger access speeds up many operations, yet it cannot be used in early bootstrap, and as a result, people tend to use `Unsafe` which can threaten memory safety of the Java Platform.
>
> To promote the safe use of multi-byte access, I propose to move the checked implementations from VarHandle to ByteArray to allow earlier use and reduce maintenance costs. In addition, ByteArrayLittleEndian is consolidated, and now the access methods are distinguished by BO (byte order) / BE (big endian) / LE (little endian) suffixes to indicate their access features.
test/jdk/jdk/internal/util/ByteArray/Types.java line 27:
> 25: * @test
> 26: * @bug 8349503
> 27: * @library /test/lib
Suggestion:
* @library /test/lib
* @key randomness
test/jdk/jdk/internal/util/ByteArray/Types.java line 64:
> 62: new ReadCase<>("u2", ByteArray::getUnsignedShortBO, ByteArray::getUnsignedShortBE, ByteArray::getUnsignedShortLE, 2, u2 -> ((u2 >> Byte.SIZE) & 0xFF) | ((u2 << Byte.SIZE) & 0xFF00), Comparator.naturalOrder()),
> 63: new ReadCase<>("int", ByteArray::getIntBO, ByteArray::getIntBE, ByteArray::getIntLE, Integer.BYTES, Integer::reverseBytes, Comparator.naturalOrder()),
> 64: new ReadCase<>("float", ByteArray::getFloatBO, ByteArray::getFloatBE, ByteArray::getFloatLE, Float.BYTES, null, Comparator.comparing(Float::floatToRawIntBits)),
Would it be possible to have a local `reverseBytes` for `float` and `double` as well?
test/jdk/jdk/internal/util/ByteArray/Types.java line 124:
> 122: new WriteCase<>("int", ByteArray::setIntBO, ByteArray::setIntBE, ByteArray::setIntLE, Integer.BYTES, List.of(42)),
> 123: new WriteCase<>("float", ByteArray::setFloatBO, ByteArray::setFloatBE, ByteArray::setFloatLE, Float.BYTES, List.of(Float.NaN, Float.intBitsToFloat(0x7FF23847))),
> 124: new WriteCase<>("float raw", ByteArray::setFloatRawBO, ByteArray::setFloatRawBE, ByteArray::setFloatRawLE, Float.BYTES, List.of(1.0F)),
Raw seems to be exercised only on unproblematic cases, not on NaNs.
Similarly for "double raw".
test/jdk/jdk/internal/util/ByteArray/Types.java line 152:
> 150: assertThrows(IndexOutOfBoundsException.class, () -> leWriter.set(arr, arrayLen - size + 1, value));
> 151:
> 152: int index = 0;
This is always 0.
test/jdk/jdk/internal/util/ByteArray/Types.java line 173:
> 171: var arrBe1 = arr.clone();
> 172: beWriter.set(arrBe1, index, v1);
> 173: assertArrayEquals(arrBe, arrBe1);
What about the little-endian case?
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/23478#discussion_r1944742266
PR Review Comment: https://git.openjdk.org/jdk/pull/23478#discussion_r1944786100
PR Review Comment: https://git.openjdk.org/jdk/pull/23478#discussion_r1944783068
PR Review Comment: https://git.openjdk.org/jdk/pull/23478#discussion_r1944769063
PR Review Comment: https://git.openjdk.org/jdk/pull/23478#discussion_r1944772926
More information about the core-libs-dev
mailing list