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

Shaojin Wen swen at openjdk.org
Fri Oct 4 16:11:38 UTC 2024


On Fri, 4 Oct 2024 14:12:16 GMT, fabioromano1 <duke at openjdk.org> wrote:

>> An optimized algorithm for `BigDecimal.stripTrailingZeros()` that uses repeated squares trick.
>
> 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];

    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];
            for (int i = FIVE_TO_2_TO_LEN; i <= n; i++)
                FIVE_TO_2_TO[i & 0x1F] = pow = pow.multiply(pow);

            FIVE_TO_2_TO_LEN = n + 1;
        }

        return FIVE_TO_2_TO[n & 0x1F];
    }

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

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


More information about the core-libs-dev mailing list