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

Shaojin Wen swen at openjdk.org
Thu Oct 16 23:45:17 UTC 2025


On Tue, 14 Oct 2025 23:26:00 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` cover the float conversions too.

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

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

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


More information about the core-libs-dev mailing list