On Sat, 18 Dec 2021 21:48:33 GMT, amirhadadi <d...@openjdk.java.net> wrote:

>> This does look like a HotSpot JIT compiler issue to me.  My guess is that it 
>> is related to how checkBoundsOffCount() checks for offset < 0:
>> 
>> 396          if ((length | fromIndex | size) < 0 || size > length - 
>> fromIndex)
>> 
>> using | to combine three values.
>
> @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`.

-------------

PR: https://git.openjdk.java.net/jdk/pull/6812

Reply via email to