On Fri, 28 Jun 2024 16:29:02 GMT, Paul Sandoz <psan...@openjdk.org> wrote:
>> Shaojin Wen has updated the pull request incrementally with one additional >> commit since the last revision: >> >> fix comment > > That's a clever change and more preferable (but still potentially fragile in > the source to bytecode translation process). However I still don't understand > the overall motivation, why does it matter? @PaulSandoz String.format is widely used. In some performance-critical scenarios, when doing performance profiling, it is often found that String.format calls consume a lot of time. My suggestion is to replace `String.format` with `String concat` or StringBuilder. String info = String.format("xxxx %s", str); replace to String info = "xxxx %s" + str; or String info = new StringBuilder("xxxx ").append(str).toString(); Let's do a Benchmark comparison: class StringFormat{ @Benchmark public String stringIntFormat() { return "%s %d".formatted(s, i); } @Benchmark public String stringIntIndy() { return s + " " + i; } @Benchmark public String stringIntStringBuilder() { return new StringBuilder(s).append(" ").append(i).toString(); } } Benchmark Mode Cnt Score Error Units StringFormat.stringIntFormat avgt 15 87.451 ? 2.890 ns/op StringFormat.stringIntIndy avgt 15 6.471 ? 0.089 ns/op StringFormat.stringIntStringBuilder avgt 15 7.949 ? 0.067 ns/op It can be seen that the performance of using StringConcat and StringBuilder is 10 times that of using String.format. StringTemplate (JEP 430/459/465) was an attempt to improve performance, but the API was not friendly. I was going to contribute to StringTemplate, (PR #16044 / PR #16021/PR #16017,) but StringTemplate has been removed. I'm working on improving the performance of String.format. The PR I submitted before: [8316704: Regex-free parsing of Formatter and FormatProcessor specifiers](https://github.com/openjdk/jdk/pull/15776) This PR is a very minor improvement, We may also need to do further work to provide a fastpath for common scenarios to improve performance. I also plan to do such work. ------------- PR Comment: https://git.openjdk.org/jdk/pull/19926#issuecomment-2197391335