RFR: 8315585: Optimization for decimal to string [v4]

Johannes Graham duke at openjdk.org
Sat Feb 1 21:06:52 UTC 2025


On Sat, 1 Feb 2025 08:42:32 GMT, Shaojin Wen <swen at openjdk.org> wrote:

>> Continue to complete PR #16006 and PR #21593 to improve BigDecimal::toString and BigDecimal::toPlainString performance and reduce duplicate code
>
> Shaojin Wen has updated the pull request with a new target base due to a merge or a rebase. The pull request now contains 29 commits:
> 
>  - Merge remote-tracking branch 'upstream/master' into dec_to_str_202501
>    
>    # Conflicts:
>    #	src/java.base/share/classes/jdk/internal/util/DecimalDigits.java
>  - remove getChars(long, int, char[])
>  - copyright
>  - Merge remote-tracking branch 'upstream/master' into dec_to_str_202501
>    
>    # Conflicts:
>    #	src/java.base/share/classes/jdk/internal/util/DecimalDigits.java
>  - simplify and comments
>  - simplify
>  - simplify
>  - code style
>  - revert change
>  - bug fix
>  - ... and 19 more: https://git.openjdk.org/jdk/compare/651ac3cc...f9af0b02

src/java.base/share/classes/java/math/BigDecimal.java line 4329:

> 4327: 
> 4328:     private static String scale2(int intCompact) {
> 4329:         int highInt = intCompact / 100;

Something to experiment with here:

        int highInt = intCompact / 100;
        int lowInt = intCompact - highInt * 100;
        short packed=DecimalDigits.pair(lowInt);

        return new StringBuilder()
                .append(highInt)
                .append('.')
                .append((char) (packed & 0xFF))
                .append((char) (packed >> 8))
                .toString();


C2 seems to be able to optimize out the SB here, so it might do as well as `newStringNoRepl`.

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

PR Review Comment: https://git.openjdk.org/jdk/pull/23310#discussion_r1938345667


More information about the core-libs-dev mailing list