Skip to content

Conversation

@rgiulietti
Copy link
Contributor

@rgiulietti rgiulietti commented Aug 28, 2025

Yet another step in modernizing FloatingDecimals floating-point parsing.


Progress

  • Change must be properly reviewed (1 review required, with at least 1 Reviewer)
  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue

Issue

  • JDK-8366017: Extend the set of inputs handled by fast paths in FloatingDecimal (Enhancement - P4)

Reviewers

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/26990/head:pull/26990
$ git checkout pull/26990

Update a local copy of the PR:
$ git checkout pull/26990
$ git pull https://git.openjdk.org/jdk.git pull/26990/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 26990

View PR using the GUI difftool:
$ git pr show -t 26990

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/26990.diff

Using Webrev

Link to Webrev Comment

@bridgekeeper
Copy link

bridgekeeper bot commented Aug 28, 2025

👋 Welcome back rgiulietti! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

@openjdk
Copy link

openjdk bot commented Aug 28, 2025

@rgiulietti This change now passes all automated pre-integration checks.

ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details.

After integration, the commit message for the final commit will be:

8366017: Extend the set of inputs handled by fast paths in FloatingDecimal

Reviewed-by: darcy

You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed.

At the time when this comment was updated there had been 37 new commits pushed to the master branch:

As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details.

➡️ To integrate this PR with the above commit message to the master branch, type /integrate in a new comment.

@openjdk
Copy link

openjdk bot commented Aug 28, 2025

@rgiulietti The following label will be automatically applied to this pull request:

  • core-libs

When this pull request is ready to be reviewed, an "RFR" email will be sent to the corresponding mailing list. If you would like to change these labels, use the /label pull request command.

@openjdk openjdk bot added the core-libs core-libs-dev@openjdk.org label Aug 28, 2025
@rgiulietti
Copy link
Contributor Author

Parsing proper has been the topic of a previous PR. However, the mathematical conversion from the decimal representation itself still relied on existing code.

The plan is to rework the conversion algorithms and end up with a 3 tiers schema.

  • The 1st tier implements some simple, efficient fast paths. They apply to a large number of simple decimals, like 1.2345 or 2.3456e-9 (see the code for the details).
  • The 2nd tier will implement some more involved fast paths with higher precision arithmetic. The hope is to be able to convert most decimals produced by Double.toString(double) in just the 1st or 2nd tier.
  • The 3rd is the fall back tier, using full precision arithmetic for long decimals. This tier will undergo a rehaul, reducing its complexity, and relying on [Mutable]BigInteger rather than FDBigInteger.

Comment on lines 43 to 53
private static final int EXP_SHIFT = DoubleConsts.SIGNIFICAND_WIDTH - 1;
private static final long FRACT_HOB = 1L << EXP_SHIFT; // assumed High-Order bit
private static final long EXP_ONE = (long) DoubleConsts.EXP_BIAS << EXP_SHIFT; // exponent of 1.0
private static final int MAX_SMALL_BIN_EXP = 62;
private static final int MIN_SMALL_BIN_EXP = -63 / 3;
private static final int MAX_DECIMAL_DIGITS = 15; // max{n : 10^n <= 2^P}
private static final int FLOG_10_MAX_LONG = 18; // max{i : 10^i ≤ Long.MAX_VALUE}

private static final int SINGLE_EXP_SHIFT = FloatConsts.SIGNIFICAND_WIDTH - 1;
private static final int SINGLE_FRACT_HOB = 1 << SINGLE_EXP_SHIFT;
private static final int SINGLE_MAX_DECIMAL_DIGITS = 7;
Copy link
Contributor

@wenshao wenshao Sep 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private static final int EXP_SHIFT = DoubleConsts.SIGNIFICAND_WIDTH - 1;
private static final long FRACT_HOB = 1L << EXP_SHIFT; // assumed High-Order bit
private static final long EXP_ONE = (long) DoubleConsts.EXP_BIAS << EXP_SHIFT; // exponent of 1.0
private static final int MAX_SMALL_BIN_EXP = 62;
private static final int MIN_SMALL_BIN_EXP = -63 / 3;
private static final int MAX_DECIMAL_DIGITS = 15; // max{n : 10^n <= 2^P}
private static final int FLOG_10_MAX_LONG = 18; // max{i : 10^i ≤ Long.MAX_VALUE}
private static final int SINGLE_EXP_SHIFT = FloatConsts.SIGNIFICAND_WIDTH - 1;
private static final int SINGLE_FRACT_HOB = 1 << SINGLE_EXP_SHIFT;
private static final int SINGLE_MAX_DECIMAL_DIGITS = 7;
private static final int
EXP_SHIFT = DoubleConsts.SIGNIFICAND_WIDTH - 1,
MAX_SMALL_BIN_EXP = 62,
MIN_SMALL_BIN_EXP = -63 / 3,
MAX_DECIMAL_DIGITS = 15, // max{n : 10^n <= 2^P}
FLOG_10_MAX_LONG = 18, // max{i : 10^i ≤ Long.MAX_VALUE}
SINGLE_EXP_SHIFT = FloatConsts.SIGNIFICAND_WIDTH - 1,
SINGLE_FRACT_HOB = 1 << SINGLE_EXP_SHIFT,
SINGLE_MAX_DECIMAL_DIGITS = 7;
private static final long
FRACT_HOB = 1L << EXP_SHIFT, // assumed High-Order bit
EXP_ONE = (long) DoubleConsts.EXP_BIAS << EXP_SHIFT; // exponent of 1.0

How about this style?

Comment on lines 160 to 164
private static final BinaryToASCIIConverter B2AC_POSITIVE_INFINITY = new ExceptionalBinaryToASCIIBuffer(INFINITY_REP);
private static final BinaryToASCIIConverter B2AC_NEGATIVE_INFINITY = new ExceptionalBinaryToASCIIBuffer("-" + INFINITY_REP);
private static final BinaryToASCIIConverter B2AC_NOT_A_NUMBER = new ExceptionalBinaryToASCIIBuffer(NAN_REP);
private static final BinaryToASCIIConverter B2AC_POSITIVE_ZERO = new BinaryToASCIIBuffer(false, new byte[] {'0'});
private static final BinaryToASCIIConverter B2AC_NEGATIVE_ZERO = new BinaryToASCIIBuffer(true, new byte[] {'0'});
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private static final BinaryToASCIIConverter B2AC_POSITIVE_INFINITY = new ExceptionalBinaryToASCIIBuffer(INFINITY_REP);
private static final BinaryToASCIIConverter B2AC_NEGATIVE_INFINITY = new ExceptionalBinaryToASCIIBuffer("-" + INFINITY_REP);
private static final BinaryToASCIIConverter B2AC_NOT_A_NUMBER = new ExceptionalBinaryToASCIIBuffer(NAN_REP);
private static final BinaryToASCIIConverter B2AC_POSITIVE_ZERO = new BinaryToASCIIBuffer(false, new byte[] {'0'});
private static final BinaryToASCIIConverter B2AC_NEGATIVE_ZERO = new BinaryToASCIIBuffer(true, new byte[] {'0'});
private static final BinaryToASCIIConverter
B2AC_POSITIVE_INFINITY = new ExceptionalBinaryToASCIIBuffer(INFINITY_REP),
B2AC_NEGATIVE_INFINITY = new ExceptionalBinaryToASCIIBuffer("-" + INFINITY_REP),
B2AC_NOT_A_NUMBER = new ExceptionalBinaryToASCIIBuffer(NAN_REP),
B2AC_POSITIVE_ZERO = new BinaryToASCIIBuffer(false, new byte[] {'0'}),
B2AC_NEGATIVE_ZERO = new BinaryToASCIIBuffer(true, new byte[] {'0'});

Comment on lines 896 to 905
private static final PreparedASCIIToBinaryBuffer A2BC_POSITIVE_INFINITY =
new PreparedASCIIToBinaryBuffer(Double.POSITIVE_INFINITY, Float.POSITIVE_INFINITY);
private static final PreparedASCIIToBinaryBuffer A2BC_NEGATIVE_INFINITY =
new PreparedASCIIToBinaryBuffer(Double.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY);
private static final PreparedASCIIToBinaryBuffer A2BC_NOT_A_NUMBER =
new PreparedASCIIToBinaryBuffer(Double.NaN, Float.NaN);
private static final PreparedASCIIToBinaryBuffer A2BC_POSITIVE_ZERO =
new PreparedASCIIToBinaryBuffer(0.0d, 0.0f);
private static final PreparedASCIIToBinaryBuffer A2BC_NEGATIVE_ZERO =
new PreparedASCIIToBinaryBuffer(-0.0d, -0.0f);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
private static final PreparedASCIIToBinaryBuffer A2BC_POSITIVE_INFINITY =
new PreparedASCIIToBinaryBuffer(Double.POSITIVE_INFINITY, Float.POSITIVE_INFINITY);
private static final PreparedASCIIToBinaryBuffer A2BC_NEGATIVE_INFINITY =
new PreparedASCIIToBinaryBuffer(Double.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY);
private static final PreparedASCIIToBinaryBuffer A2BC_NOT_A_NUMBER =
new PreparedASCIIToBinaryBuffer(Double.NaN, Float.NaN);
private static final PreparedASCIIToBinaryBuffer A2BC_POSITIVE_ZERO =
new PreparedASCIIToBinaryBuffer(0.0d, 0.0f);
private static final PreparedASCIIToBinaryBuffer A2BC_NEGATIVE_ZERO =
new PreparedASCIIToBinaryBuffer(-0.0d, -0.0f);
private static final PreparedASCIIToBinaryBuffer
A2BC_POSITIVE_INFINITY = new PreparedASCIIToBinaryBuffer(Double.POSITIVE_INFINITY, Float.POSITIVE_INFINITY),
A2BC_NEGATIVE_INFINITY = new PreparedASCIIToBinaryBuffer(Double.NEGATIVE_INFINITY, Float.NEGATIVE_INFINITY),
A2BC_NOT_A_NUMBER = new PreparedASCIIToBinaryBuffer(Double.NaN, Float.NaN),
A2BC_POSITIVE_ZERO = new PreparedASCIIToBinaryBuffer(0.0d, 0.0f),
A2BC_NEGATIVE_ZERO = new PreparedASCIIToBinaryBuffer(-0.0d, -0.0f);

@rgiulietti rgiulietti marked this pull request as ready for review October 14, 2025 17:52
@openjdk openjdk bot added the rfr Pull request is ready for review label Oct 14, 2025
@mlbridge
Copy link

mlbridge bot commented Oct 14, 2025

Webrevs

@rgiulietti
Copy link
Contributor Author

This work's focus is on:

  • Code reduction.
  • Extensive documentation of mathematical aspects.
  • Enhanced performance.

In particular, parsing outcomes of the Double.toString(double) and Float.toString(float) methods should be faster.

Before

Benchmark                               Mode  Cnt     Score    Error  Units
FloatingPointParse.parseDoubleS1        avgt    9   268.213 ±  0.901  ns/op
FloatingPointParse.parseDoubleS10       avgt    9  1091.924 ± 52.040  ns/op
FloatingPointParse.parseDoubleS2        avgt    9   340.953 ± 10.139  ns/op
FloatingPointParse.parseDoubleS4        avgt    9   499.272 ± 13.940  ns/op
FloatingPointParse.parseDoubleToString  avgt    9   272.870 ±  1.677  ns/op
FloatingPointParse.parseFloatS1         avgt    9    72.291 ±  1.574  ns/op
FloatingPointParse.parseFloatS10        avgt    9   321.380 ± 10.046  ns/op
FloatingPointParse.parseFloatS2         avgt    9   104.538 ±  2.701  ns/op
FloatingPointParse.parseFloatS4         avgt    9   159.879 ±  6.711  ns/op
FloatingPointParse.parseFloatToString   avgt    9   140.413 ±  1.844  ns/op

After

Benchmark                               Mode  Cnt    Score   Error  Units
FloatingPointParse.parseDoubleS1        avgt    9   77.753 ± 1.437  ns/op
FloatingPointParse.parseDoubleS10       avgt    9  368.850 ± 0.974  ns/op
FloatingPointParse.parseDoubleS2        avgt    9  112.624 ± 2.094  ns/op
FloatingPointParse.parseDoubleS4        avgt    9  177.373 ± 1.137  ns/op
FloatingPointParse.parseDoubleToString  avgt    9   76.904 ± 1.152  ns/op
FloatingPointParse.parseFloatS1         avgt    9   53.126 ± 0.793  ns/op
FloatingPointParse.parseFloatS10        avgt    9  300.140 ± 1.330  ns/op
FloatingPointParse.parseFloatS2         avgt    9   83.329 ± 0.251  ns/op
FloatingPointParse.parseFloatS4         avgt    9  137.046 ± 2.160  ns/op
FloatingPointParse.parseFloatToString   avgt    9   56.267 ± 0.698  ns/op```

CDS.initializeFromArchive(FDBigInteger.class);
Object[] caches = archivedCaches;
if (caches == null) {
long[] long5pow = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be worthwhile to make these arrays @Stable?

In the future, might be a candidate for StableConstant/LazyValue functionality.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be worthwhile to make these arrays @Stable?

In the future, might be a candidate for StableConstant/LazyValue functionality.

PS If each array is read only once, then perhaps the other data structures would be better initialized in a loop.


private static void testFastPaths() {
/* Exercises the fast paths in jdk.internal.math.FloatingDecimal. */
check("1", 1.0);
Copy link
Member

@jddarcy jddarcy Oct 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something to consider: specify the double values in hex literals -- this would exercise entirely different code paths than the decimal ones.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, I fully agree.
Updating the test to use hexadecimals...

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Oct 29, 2025
@openjdk openjdk bot removed the ready Pull request is ready to be integrated label Oct 29, 2025
@rgiulietti rgiulietti requested a review from jddarcy October 29, 2025 13:58
@openjdk openjdk bot added the ready Pull request is ready to be integrated label Oct 29, 2025
@rgiulietti
Copy link
Contributor Author

/integrate

@openjdk
Copy link

openjdk bot commented Nov 3, 2025

Going to push as commit deb7edb.
Since your change was applied there have been 94 commits pushed to the master branch:

Your commit was automatically rebased without conflicts.

@openjdk openjdk bot added the integrated Pull request has been integrated label Nov 3, 2025
@openjdk openjdk bot closed this Nov 3, 2025
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Nov 3, 2025
@openjdk
Copy link

openjdk bot commented Nov 3, 2025

@rgiulietti Pushed as commit deb7edb.

💡 You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core-libs core-libs-dev@openjdk.org integrated Pull request has been integrated

Development

Successfully merging this pull request may close these issues.

3 participants