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

Raffaello Giulietti rgiulietti at openjdk.org
Tue Oct 21 07:26:09 UTC 2025


On Tue, 21 Oct 2025 05:14:48 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 with a new target base due to a merge or a rebase. The pull request now contains 17 commits:
> 
>  - codestyle
>  - Merge remote-tracking branch 'upstream/master' into float_to_hex_str_2510
>    
>    # Conflicts:
>    #	test/jdk/java/lang/Double/ToHexString.java
>  - copyright
>  - fix test, from @jddarcy
>  - extract doubleToLongBits, from @jddarcy
>  - fix test comment
>  - add tests for removed trailing zeros, from @jddarcy
>  - from @rgiulietti
>  - from @rgiulietti
>  - code style, from @rgiulietti
>  - ... and 7 more: https://git.openjdk.org/jdk/compare/eee29088...822c0259

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

> 766:             // Integer.digits maps values 0-15 to '0'-'f' characters
> 767:             chars[index++] = Integer.digits[((int)(signifBits >> ((12 - i) << 2))) & 0xF];
> 768:         }

Suggestion:

        for (int sh = 48, end = 4 * trailingZeros; sh >= end; sh -= 4) {
            // Extract 4 bits at a time from left to right
            // Shift right by sh positions and mask with 0xF
            // Integer.digits maps values 0-15 to '0'-'f' characters
            chars[index++] = Integer.digits[((int)(signifBits >> sh)) & 0xF];
        }

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

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


More information about the core-libs-dev mailing list