String.format() is 3x faster in Java 17
https://www.javaspecialists.eu/archive/Issue294-String.format-3x-faster-in-Java-17.html24
u/gavenkoa Oct 30 '21
From http://www.slf4j.org/api/org/slf4j/helpers/MessageFormatter.html
The formatting conventions are different than those of MessageFormat which ships with the Java platform. This is justified by the fact that SLF4J's implementation is 10 times faster than that of MessageFormat. This local performance difference is both measurable and significant in the larger context of the complete logging processing chain.
33
u/dpash Oct 30 '21
That's because it's significantly less complex. The SLF4J is the equivalent of supporting just
%s.
11
u/TheCountRushmore Oct 30 '21
Why isn't this using JMH for the benchmarks?
10
u/cl4es Oct 30 '21
I wouldn't know -- but I did add a small set of JMH benchmarks back when I did this optimization. While not exactly the same tests, speed-ups were in line with what Heinz is reporting here: https://github.com/openjdk/jdk/pull/2830#issuecomment-790788322
21
u/slowfly1st Oct 30 '21
I ran my own tests, without using reflection, and therefore the use of a security manager, and I actually called String.format. That's imho a more realistic approach?
JDK 17
1. this does not have any percentages at all
0.09414233999999999ms.
2. this %s has only a simple field
0.04526432ms.
3. this has a simple field %s and then a complex %-20s
0.12824544000000002ms.
4. %s %1s %2s %3s %4s %5s %10s %22s
0.42732686999999997ms.
JDK 15
1. this does not have any percentages at all
0.18450188ms.
2. this %s has only a simple field
0.07883934ms.
3. this has a simple field %s and then a complex %-20s
0.09579937ms.
4. %s %1s %2s %3s %4s %5s %10s %22s
0.42675903000000004ms.2
u/DasBrain Oct 31 '21
Reflection is orthogonal to using the (deprecated) SecurityManager.
I don't know what you mean by "running without reflection" - the method
Formatter.parse(List, String)is not publicly accessible.1
36
u/nekokattt Oct 30 '21
It is somewhat a shame that Java still doesn't have string interpolation. That would make many uses for String.format totally unneeded and would allow the logic for formatting to be generated at compile time.
60
u/cl4es Oct 30 '21
It's coming!
https://github.com/openjdk/amber-docs/blob/master/site/design-notes/templated-strings.md
Current PoC implementation shows numbers in line with string concatenation.
But it still makes sense to improve existing APIs when we can, due the staggering amount of code that is written for them.
7
u/JoJoModding Oct 31 '21
The java compiler could optimize format() calls at compile time right now. The hot spot optimizer could also generate code replacing format() calls with inlined code.
It just does not right now, because reasons.
C compilers also don't do this, even though printf() has a complete specification.
5
u/sweetno Oct 31 '21
It's not very useful for a real project since the format string will come from the localization files there.
1
-47
u/Worth_Trust_3825 Oct 30 '21
A shame? When java has the superior method of string formatting right out of the box? Were you dropped on your head?
25
u/agentoutlier Oct 30 '21
Interpolation is less about formatting and more about constructing a string without manually concatenating variables. In Scala and Kotlin it is done at compile time.
8
u/pron98 Oct 30 '21 edited Oct 31 '21
String templates aren't just about formatting strings, but about constructing arbitrary objects that are configured with strings. As a special case they can, of course, format strings, but they're able to do it more efficiently than
format, because of a fundamental difference: the template is a Java expression that is known at compile-time, while the format string is not -- it's just an arbitrary string, and not necessarily a constant one (i.e. you can writeString.format(x, a, b, c), wherexis a String variable that needs to be parsed, but that is not the case with templates, which, like today's string concatenation, can always be parsed only once -- at compilation or class-loading time; there just isn't an instance of an unparsed template).19
u/nekokattt Oct 30 '21
No, but from what I can tell, you were thrown at a wall if that is your way of partaking in a sensible conversation.
15
u/jeff303 Oct 30 '21
Interpolation puts the variable names directly in the string literal, so it's easier to see what's what.
-20
u/Worth_Trust_3825 Oct 30 '21
Positional arguments already show you which argument will be put where. In fact, you can abuse it to put same argument into multiple places under multiple formats.
9
u/jeff303 Oct 30 '21
Absolutely. But you still have to figure out which arg corresponds to the index number. And also, if you're reading someone else's code, there's a very good chance they didn't use them anyway.
2
u/DaddyLcyxMe Oct 31 '21
string interpolation would be evaluated at compile time (here in my example i use javascript’s syntax):
int x = 12; System.out.println(\`myVar = ${x}\`);would be evaluated and be the same as
int x = 12; System.out.println("myVar = " + x);string interpolation is just an easier and cleaner way to create strings. this way is also faster because it isn’t having to do any complex formatting/parsing.
tldr; string interpolation would be built at compile time (using concatenation). string format is executed at runtime and will always be slower than interpolated or concatenated strings.
3
3
u/vprise Oct 31 '21
This requires an API change, format just needs Formatter prepareFormat(String) then you can just use the returned object to apply the format and get speed that's as fast as string concatenation.
Yes, it's two stages but it's physically two different operations: parsing and formatting. So that would make sense. Then you don't need complex caching behavior by the API that can cause memory leaks, stale cache etc. e.g. imagine caching a format then changing the locale... Ugh.
I'd love to have templated strings and this can be the underlying implementation for that.
2
u/cl4es Oct 31 '21
The current implementation for the not-yet-proposed templated strings JEP would make use of both compile-time pre-processing and
invokedynamicat runtime to achieve this. This ensures the "format" string is a compile time constant and can be pre-parsed, and that only the first call will have to bootstrap the expression. Subsequent calls will be as performant as a regular String concatenation*. The slightly magical machinery will share code under the hood with the indifiedStringconcatenation that was added in JDK 9.*the impl currently performs slower than String concatenation when more complex formatting is involved - which is only natural. I was recently asked to look for opportunities to improve this.
1
u/vprise Oct 31 '21
If this will land in the next version then we probably won't need an "interim" solution. but if not won't it make sense to expose a two stage API?
2
u/cl4es Oct 31 '21
Depends what you consider to be the next version. Chances are high it'll be final in the next LTS (21)
1
u/vprise Oct 31 '21
18 would be the next version. I guess 21 isn't terrible and it is a huge leap forward... Great stuff.
2
u/cl4es Oct 31 '21
I'll leave it to the JEP author to comment on which release this is going to be targeted to (and possibly previewed in).
2
1
Nov 01 '21
Wait what, is it the same for `String#formatted()`
2
u/cl4es Nov 01 '21
Yes,
String::formattedis just a non-static variant ofString::format. So this optimization applies equally to both.
1
u/NewSchoolBoxer Nov 07 '21
That's pretty cool. Highest version Java I've ever seen in the wild in Fortune 500 software is 11. Next project for me is in 8. Hope we get to 17 someday.
1
u/c_edward Oct 30 '21
Have they removed the regexp horror that sat inside format() previously? Or are regexps faster as well?
2
-9
u/UnexpectedLizard Oct 30 '21
Great but when are we going to get string interpolation?
C# and Javascript added it 5+ years ago.
176
u/cl4es Oct 30 '21
Glad to see some of the small enhancements we did in 17 get recognition. Not sure if this one matters, but String::format shows up in profiles every now and then so it felt reasonable to me to give it some TLC in between larger projects.