RFR: 8335366: Improve String.format performance with fastpath [v3]

David Schlosnagle duke at openjdk.org
Sat Jun 29 17:44:19 UTC 2024


On Sat, 29 Jun 2024 16:16:51 GMT, David Schlosnagle <duke at openjdk.org> wrote:

>> Shaojin Wen has updated the pull request incrementally with one additional commit since the last revision:
>> 
>>   rename `digit` to `width`
>
> src/java.base/share/classes/java/lang/StringFormat.java line 82:
> 
>> 80:                 conv = format.charAt(off + 2);
>> 81:             }
>> 82:         }
> 
> is it worth handling `width > 9`?
> 
> Suggestion:
> 
>         for (int i = 2; conv >= '1' && conv <= '9' && off + i < max; i++) {
>             width = (width * 10) + (conv - '0');
>             if (off + i < max) {
>                 conv = format.charAt(off + i);
>             }
>         }

a quick local benchmark on 2021 MacBook M1 Pro before/after motivating this:


    @Benchmark
    public String wideStringIntFormat() {
        return "%123s %42d".formatted(s, i);
    }


#### Before:

Benchmark                          Mode  Cnt    Score    Error  Units
StringFormat.wideStringIntFormat   avgt   15  377.603 ± 85.742  ns/op


#### After:

Benchmark                          Mode  Cnt    Score    Error  Units
StringFormat.wideStringIntFormat   avgt   15   54.104 ±  0.079  ns/op

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

PR Review Comment: https://git.openjdk.org/jdk/pull/19956#discussion_r1659916636


More information about the core-libs-dev mailing list