On Tue, 21 Dec 2021 12:50:36 GMT, Roland Westrelin <[email protected]> wrote:
>> @dean-long actually the issue reproduces with Java 17 where
>> `checkBoundsOffCount` was implemented in a more straight forward manner:
>>
>>
>> static void checkBoundsOffCount(int offset, int count, int length) {
>> if (offset < 0 || count < 0 || offset > length - count) {
>> throw new StringIndexOutOfBoundsException(
>> "offset " + offset + ", count " + count + ", length " + length);
>> }
>> }
>>
>>
>>
>> Here's a
>> [gist](https://gist.github.com/amirhadadi/9505c3f5d9ad68cad2fbfd1b9e01f0b8)
>> with a benchmark you can run. This benchmark compares safe and unsafe reads
>> from the byte array (In this gist I didn't modify the code to add the
>> offset >= 0 condition).
>>
>> Here are the results:
>>
>>
>> OpenJDK 17.0.1+12
>> OSX with 2.9 GHz Quad-Core Intel Core i7
>>
>>
>>
>> Benchmark Mode Cnt Score Error Units
>> StringBenchmark.safeDecoding avgt 20 120.312 ± 11.674 ns/op
>> StringBenchmark.unsafeDecoding avgt 20 72.628 ± 0.479 ns/op
>
> @amirhadadi unsafeDecode() is buggy I think. Offsets in the array when read
> with unsafe should be computed as `offset * unsafe.ARRAY_BYTE_INDEX_SCALE +
> unsafe.ARRAY_BYTE_BASE_OFFSET`.
@rwestrel thanks for the correction!
Here are the updated results:
Benchmark Mode Cnt Score Error Units
StringBenchmark.safeDecoding avgt 20 113.849 ± 1.609 ns/op
StringBenchmark.unsafeDecoding avgt 20 85.272 ± 1.462 ns/op
-------------
PR: https://git.openjdk.java.net/jdk/pull/6812