On Wed, 13 Mar 2024 16:02:29 GMT, Chen Liang <li...@openjdk.org> wrote:

>> Shaojin Wen has updated the pull request incrementally with one additional 
>> commit since the last revision:
>> 
>>   bug fix for CharArraySequence
>
> src/java.base/share/classes/java/math/BigDecimal.java line 561:
> 
>> 559:             index += offset;
>> 560:             if (index >= length)
>> 561:                 throw new IndexOutOfBoundsException();
> 
> This logic is wrong: if offset is 3 and length is 2, aab*bc*c would be valid, 
> but your code will IOOBE on `charAt(0)` because `index += offset` will be 3, 
> 3 > 2.
> 
> You should use `Objects.checkIndex(index, length)` instead.

Oh, what a stupid mistake :)

Using Objects.checkIndex will have a redundant check for index < 0, which does 
not need to be done in this scenario. Objects.checkIndex will cause a slight 
performance degradation.

The performance numbers under MacBookPro M1 Max are as follows:


Benchmark                                      Mode  Cnt   Score   Error  Units
BigDecimals.testConstructorWithSmallCharArray  avgt   15  19.167 ? 0.070  ns/op

Benchmark                                      Mode  Cnt   Score   Error  Units 
# Objects.checkIndex
BigDecimals.testConstructorWithSmallCharArray  avgt   15  20.591 ? 0.241  ns/op

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

PR Review Comment: https://git.openjdk.org/jdk/pull/18177#discussion_r1524049182

Reply via email to