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

fabioromano1 duke at openjdk.org
Wed Oct 9 14:35:00 UTC 2024


On Wed, 9 Oct 2024 14:10:17 GMT, Raffaello Giulietti <rgiulietti at openjdk.org> wrote:

>> @rgiulietti FYI, I have found that `BigInteger` has already a cache for the repeated squares that uses in radix string conversion, and it is provided by the method `BigInteger.getRadixConversionCache(int radix, int exponent)`. Although, I don't know if it would be a good idea to use it, because it computes all the squares up to `radix^(2^exponent)`, and this may not be necessary but could cost a lot of running time and memory if the number to strip is very large...
>
> Seems like `BigInteger`s division either takes care of removing common powers of 2 in the dividend and divisor (Knuth division), or removing them has no practical benefit (Burnikel-Ziegler).
> 
> Thus, I'm wondering if using powers of 10 rather than powers of 5 in the stripping logic could simplify the code while retaining comparable performance.
> 
> The powers of 10 are already indirectly kept in the class by method `bigTenToThe()` (although they affect resident memory footprint). However, since that method is there to stay and used in other places as well, it might be worthwhile to profit from it.

@rgiulietti 
- About powers of two, there is a difference here: all powers of two are removed, not only the common, to use numbers as small as possible.
- About `bigTenToThe()`: its shortcoming is that the powers are calculated multiplying by 10 each time, rather than using repeated squares trick, until the cache is completely filled.

These are the main reasons why I decided not to use `bigTenToThe()`.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/21323#discussion_r1793636240


More information about the core-libs-dev mailing list