RFR: 8341470: BigDecimal.stripTrailingZeros() optimization [v18]

Raffaello Giulietti rgiulietti at openjdk.org
Fri Oct 4 16:37:39 UTC 2024


On Fri, 4 Oct 2024 16:09:28 GMT, Shaojin Wen <swen at openjdk.org> wrote:

>> fabioromano1 has updated the pull request incrementally with two additional commits since the last revision:
>> 
>>  - Merge branch 'patchStripTrailingZeros' of https://github.com/fabioromano1/jdk into patchStripTrailingZeros
>>  - Added benchmark tests
>
> Expand the length of FIVE_TO_2_TO to 32 and use `& 0x1F` when accessing the array to eliminate bounds checks.
> 
> private static final BigInteger[] FIVE_TO_2_TO = new BigInteger[32];  // 30 -> 32
> 
> 
>     private static BigInteger fiveToTwoToThe(int n) {
>         if (n >= FIVE_TO_2_TO_LEN) {
>             BigInteger pow = FIVE_TO_2_TO[(FIVE_TO_2_TO_LEN - 1) & 0x1F]; // & 0x1F eliminate bounds checks.
>             for (int i = FIVE_TO_2_TO_LEN; i <= n; i++)
>                 FIVE_TO_2_TO[i & 0x1F] = pow = pow.multiply(pow); // & 0x1F eliminate bounds checks.
> 
>             FIVE_TO_2_TO_LEN = n + 1;
>         }
> 
>         return FIVE_TO_2_TO[n & 0x1F]; // & 0x1F eliminate bounds checks.
>     }

@wenshao I don't think it makes any practical difference in performance, and the code becomes a bit more convoluted. The need for comments to enlighten the reader is suspicious.

These tricks are great in time critical code, but IMO this is not the case here.

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

PR Comment: https://git.openjdk.org/jdk/pull/21323#issuecomment-2394076573


More information about the core-libs-dev mailing list