RFR: 8356709: Avoid redundant String formatting in BigDecimal.valueOf(double)
Johannes Graham
duke at openjdk.org
Mon May 12 14:43:11 UTC 2025
On Sun, 11 May 2025 16:22:11 GMT, Johannes Graham <duke at openjdk.org> wrote:
> Optimize `BigDecimal.valueOf(double)` by using `FormattedFPDecimal` instead of converting to decimal string and then parsing it. This results in an approximate 6x improvement for me.
On Aarch64 M4:
Benchmark Mode Cnt Score Error Units
+ BigDecimals.testValueOfWithDouble avgt 15 9.913 ± 0.080 ns/op
- BigDecimals.testValueOfWithDoubleString avgt 15 65.846 ± 0.801 ns/op
src/java.base/share/classes/java/math/BigDecimal.java line 1398:
> 1396: public static BigDecimal valueOf(double val) {
> 1397: if (!Double.isFinite(val)) {
> 1398: throw new NumberFormatException("Infinite or NaN");
Note, this changes the exception message. The new message is consistent with `new BigDecimal(double)`
-------------
PR Comment: https://git.openjdk.org/jdk/pull/25173#issuecomment-2869969120
PR Review Comment: https://git.openjdk.org/jdk/pull/25173#discussion_r2083568201
More information about the core-libs-dev
mailing list