-
Notifications
You must be signed in to change notification settings - Fork 6.1k
8370013: Refactor Double.toHexString to eliminate regex and StringBuilder #27811
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
👋 Welcome back swen! A progress list of the required criteria for merging this PR into |
|
@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: 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
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 |
|
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/opMore than 10 times performance improvement |
442bad2 to
8dfe482
Compare
8dfe482 to
e845b83
Compare
|
@wenshao Seems like a nice improvement. |
Webrevs
|
|
/reviewers 2 reviewer |
| // 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)); |
There was a problem hiding this comment.
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;
|
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]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| 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'; |
There was a problem hiding this comment.
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
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 Specifically, numbers with all the possible numbers of trailing zeros should be explicitly tested. |
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? |
|
Ah OK, thanks. |
|
@wenshao this pull request can not be integrated into 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 |
…2510 # Conflicts: # test/jdk/java/lang/Double/ToHexString.java
Co-authored-by: Raffaello Giulietti <raffaello.giulietti@oracle.com>
There was a problem hiding this 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.
|
/integrate |
|
Going to push as commit 5862358.
Your commit was automatically rebased without conflicts. |
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
Issue
Reviewers
Reviewing
Using
gitCheckout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/27811/head:pull/27811$ git checkout pull/27811Update a local copy of the PR:
$ git checkout pull/27811$ git pull https://git.openjdk.org/jdk.git pull/27811/headUsing Skara CLI tools
Checkout this PR locally:
$ git pr checkout 27811View PR using the GUI difftool:
$ git pr show -t 27811Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/27811.diff
Using Webrev
Link to Webrev Comment