RFR: 8370013: Refactor Double.toHexString to eliminate regex and StringBuilder [v2]
Andrew Haley
aph at openjdk.org
Mon Oct 20 09:01:27 UTC 2025
On Mon, 20 Oct 2025 06:34:39 GMT, Shaojin Wen <swen at openjdk.org> wrote:
>> 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`.
>
> Shaojin Wen has updated the pull request incrementally with one additional commit since the last revision:
>
> from @jddarcy
src/java.base/share/classes/java/lang/Double.java line 712:
> 710: return Double.toString(d);
> 711: else {
> 712: boolean negative = Math.copySign(1.0, d) == -1.0;
Suggestion:
boolean negative = Math.copySign(1.0, d) < 0.0;
src/java.base/share/classes/java/lang/Double.java line 771:
> 769: // Shift right by (12 - i) * 4 positions and mask with 0xF
> 770: // Integer.digits maps values 0-15 to '0'-'f' characters
> 771: chars[index++] = Integer.digits[((int)(signifBits >> ((12 - i) << 2))) & 0xF];
Suggestion:
int digit = ((int)(signifBits >> ((12 - i) << 2))) & 0xF;
chars[index++] = digit + digit < 10 ? '0' : 'a';
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/27811#discussion_r2444259295
PR Review Comment: https://git.openjdk.org/jdk/pull/27811#discussion_r2444255702
More information about the core-libs-dev
mailing list