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

Shaojin Wen duke at openjdk.org
Sat Jun 29 18:55:18 UTC 2024


On Sat, 29 Jun 2024 17:41:47 GMT, David Schlosnagle <duke at openjdk.org> wrote:

>> 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

But this is a fastpath optimization, is width > 9 common enough?

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

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


More information about the core-libs-dev mailing list