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

Shaojin Wen swen at openjdk.org
Mon Oct 20 15:37:42 UTC 2025


On Wed, 15 Oct 2025 00:02:58 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`.
>
> 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 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?

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

PR Comment: https://git.openjdk.org/jdk/pull/27811#issuecomment-3422618871


More information about the core-libs-dev mailing list