Skip to content

Conversation

@wenshao
Copy link
Contributor

@wenshao wenshao commented Oct 14, 2025

This PR refactors the Double.toHexString method to improve its performance by eliminating the use of StringBuilder and regex operations. The new implementation uses a direct byte array approach to construct the hex string representation, which avoids the overhead of StringBuilder operations and regex pattern matching.

Existing tests in java/lang/Double/ToHexString.java.


Progress

  • Change must not contain extraneous whitespace
  • Commit message must refer to an issue
  • Change must be properly reviewed (2 reviews required, with at least 2 Reviewers)

Issue

  • JDK-8370013: Refactor Double.toHexString to eliminate regex and StringBuilder (Enhancement - P4)

Reviewers

Reviewing

Using git

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

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

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 27811

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

Using diff file

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

Using Webrev

Link to Webrev Comment

@wenshao wenshao changed the title Refactor Double::toHexString( Refactor Double::toHexString Oct 14, 2025
@bridgekeeper
Copy link

bridgekeeper bot commented Oct 14, 2025

👋 Welcome back swen! 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 Oct 14, 2025

@wenshao 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:

8370013: Refactor Double.toHexString to eliminate regex and StringBuilder

Reviewed-by: rgiulietti, 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 40 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 openjdk bot added the core-libs core-libs-dev@openjdk.org label Oct 14, 2025
@openjdk
Copy link

openjdk bot commented Oct 14, 2025

@wenshao 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.

@wenshao wenshao changed the title Refactor Double::toHexString Refactor Double.toHexString to eliminate regex and StringBuilder Oct 14, 2025
@wenshao
Copy link
Contributor Author

wenshao commented Oct 15, 2025

The test results run under Mac Book M1 Pro are as follows

# before 3f11c4307ee1ca918926c8c522d2fc496947980e
-Benchmark            Mode  Cnt    Score    Error  Units
-Doubles.toHexString  avgt   15  228.383 ± 17.172  ns/op

# after c18f7632f2f2cf0752b3f4651d4cadf95bb859c6
+Benchmark            Mode  Cnt   Score   Error  Units
+Doubles.toHexString  avgt   15  15.138 ± 1.070  ns/op

More than 10 times performance improvement

@wenshao wenshao force-pushed the float_to_hex_str_2510 branch 5 times, most recently from 442bad2 to 8dfe482 Compare October 15, 2025 16:25
@wenshao wenshao force-pushed the float_to_hex_str_2510 branch from 8dfe482 to e845b83 Compare October 15, 2025 16:40
@rgiulietti
Copy link
Contributor

@wenshao Seems like a nice improvement.
Will have a closer look in the next days.

@wenshao wenshao changed the title Refactor Double.toHexString to eliminate regex and StringBuilder 8370013: Refactor Double.toHexString to eliminate regex and StringBuilder Oct 16, 2025
@wenshao wenshao marked this pull request as ready for review October 16, 2025 23:35
@openjdk openjdk bot added the rfr Pull request is ready for review label Oct 16, 2025
@mlbridge
Copy link

mlbridge bot commented Oct 16, 2025

@jddarcy
Copy link
Member

jddarcy commented Oct 20, 2025

/reviewers 2 reviewer

@openjdk
Copy link

openjdk bot commented Oct 20, 2025

@jddarcy
The total number of required reviews for this PR (including the jcheck configuration and the last /reviewers command) is now set to 2 (with at least 2 Reviewers).

// Calculate the number of trailing zeros in the significand (in groups of 4 bits)
// This is used to remove trailing zeros from the hex representation
// We limit to 12 because we want to keep at least 1 hex digit (13 total - 12 = 1)
int trailingZeros = Math.min(12, ((Long.numberOfTrailingZeros(signifBits) & 0xFC) >> 2));
Copy link
Member

Choose a reason for hiding this comment

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

Some additional discussion of the computation here would be helpful to casual readers.

I would also be tempted to add

assert 0 <= trailingZeros && trailingZeros <= 12;

@jddarcy
Copy link
Member

jddarcy commented Oct 20, 2025

The existing regression tests may be adequate for this refactoring as the high-level algorithm and special cases are similar, but I'll want to take another look before approving this PR. Thanks.

// Extract 4 bits at a time from left to right
// Shift right by (12 - i) * 4 positions and mask with 0xF
// Integer.digits maps values 0-15 to '0'-'f' characters
chars[index++] = Integer.digits[((int)(signifBits >> ((12 - i) << 2))) & 0xF];
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
chars[index++] = Integer.digits[((int)(signifBits >> ((12 - i) << 2))) & 0xF];
int digit = ((int)(signifBits >> ((12 - i) << 2))) & 0xF;
chars[index++] = digit + digit < 10 ? '0' : 'a';

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Keeping the same as Integer.toHexString/Long.toHexString, I think it is okay to continue using table lookup here. If we want to eliminate the table lookup, we can use vectorization to eliminate it. I have submitted another PR before #22942

@jddarcy
Copy link
Member

jddarcy commented Oct 20, 2025

The existing regression tests may be adequate for this refactoring as the high-level algorithm and special cases are similar, but I'll want to take another look before approving this PR. Thanks.

Looking over test/jdk/java/lang/ToHexString.java and the new code, I think test cases like the following should be added:

0x1.0p1, // Max trailing zeros
0x1.1p1,
0x1.01p1,
...
0x1.000000000001p1 // No trailing zeros

Specifically, numbers with all the possible numbers of trailing zeros should be explicitly tested.

@wenshao
Copy link
Contributor Author

wenshao commented Oct 20, 2025

@wenshao What's wrong with my other suggestions? In particular, the abs() line can be moved after the check for 0.0, and I cannot see the need for the 0x1000_0000_0000_0000L bit in signifBits.

Your suggestion is fine, but I have to deal with them one by one. They should be done by now. Is there anything I missed?

@rgiulietti
Copy link
Contributor

Ah OK, thanks.
I currently have nothing more to add.

@openjdk
Copy link

openjdk bot commented Oct 21, 2025

@wenshao this pull request can not be integrated into master due to one or more merge conflicts. To resolve these merge conflicts and update this pull request you can run the following commands in the local repository for your personal fork:

git checkout float_to_hex_str_2510
git fetch https://git.openjdk.org/jdk.git master
git merge FETCH_HEAD
# resolve conflicts and follow the instructions given by git merge
git commit -m "Merge master"
git push

@openjdk openjdk bot added the merge-conflict Pull request has merge conflict with target branch label Oct 21, 2025
…2510

# Conflicts:
#	test/jdk/java/lang/Double/ToHexString.java
@openjdk openjdk bot removed the merge-conflict Pull request has merge conflict with target branch label Oct 21, 2025
wenshao and others added 2 commits October 21, 2025 15:34
Co-authored-by: Raffaello Giulietti <raffaello.giulietti@oracle.com>
Copy link
Contributor

@rgiulietti rgiulietti left a comment

Choose a reason for hiding this comment

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

@wenshao Thanks for your contribution.

@openjdk openjdk bot added the ready Pull request is ready to be integrated label Oct 22, 2025
@wenshao
Copy link
Contributor Author

wenshao commented Oct 24, 2025

/integrate

@openjdk
Copy link

openjdk bot commented Oct 24, 2025

Going to push as commit 5862358.
Since your change was applied there have been 63 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 Oct 24, 2025
@openjdk openjdk bot closed this Oct 24, 2025
@openjdk openjdk bot removed ready Pull request is ready to be integrated rfr Pull request is ready for review labels Oct 24, 2025
@openjdk
Copy link

openjdk bot commented Oct 24, 2025

@wenshao Pushed as commit 5862358.

💡 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.

4 participants