RFR: 8370013: Refactor Double.toHexString to eliminate regex and StringBuilder [v9]

Joe Darcy darcy at openjdk.org
Tue Oct 21 03:20:14 UTC 2025


On Tue, 21 Oct 2025 01:28:50 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:
> 
>   fix test comment

src/java.base/share/classes/java/lang/Double.java line 725:

> 723:         // so that the string representation has a known length.
> 724:         // This ensures we always have 13 hex digits to work with (52 bits / 4 bits per hex digit)
> 725:         long signifBits = Double.doubleToLongBits(d) & DoubleConsts.SIGNIF_BIT_MASK;

I think it would be clearer if the results of Double.doubleToLongBits(d) were stashed in a variable as this method is also called for the computation of the negative predicate.

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/27811#discussion_r2446621869


More information about the core-libs-dev mailing list