Replace String.format usage with simple string concatenation#18885
Replace String.format usage with simple string concatenation#18885wendigo wants to merge 2 commits intotrinodb:masterfrom
Conversation
| private static final String ALWAYS_TRUE = "1=1"; | ||
| private static final String ALWAYS_FALSE = "1=0"; | ||
|
|
||
| private static final Joiner SPACE_JOINER = Joiner.on(' '); |
There was a problem hiding this comment.
this seems more like convenience rather than required? You can surely append a trailing space to each line.
I'll look into how Joiner is implemented - maybe I'm overthinking.
There was a problem hiding this comment.
Is this for performance, perhaps? I don't find it more readable or more convenient...
There was a problem hiding this comment.
Actually it is more readable as with the format you need to understand which argument goes where
There was a problem hiding this comment.
Joiner is actually more performant and less garbage generating compared to even concat since it uses good old StringBuilder underneath and some intelligent logic.
Yes, this is for perf.
There was a problem hiding this comment.
Don't underestimate plain concatenation, in Java 9+ it no longer desugars to StringBuilder and uses some INVOKEDYNAMIC magic instead :)
There was a problem hiding this comment.
(I'm not blocking this PR, BTW, do what you have to do)
There was a problem hiding this comment.
Yes, I tested with a JMH harness, Joiner is still faster.
There was a problem hiding this comment.
wait a sec, my measurements were wrong.
Plain concat is the fastest indeed.
JOINER is MUCH faster than format.
format must be avoided.
So if plain concat preserves readability (it seems to do) then let's use it.
For me joiner won because my JMH was generating random inputs for each method being benchmarked instead of each run having same input.
There was a problem hiding this comment.
Benchmark Mode Cnt Score Error Units
DifferentConcats.concat avgt 15 143.218 ± 4.177 ns/op
DifferentConcats.guavaJoiner avgt 15 398.257 ± 12.523 ns/op
DifferentConcats.formatStr avgt 15 750.233 ± 16.783 ns/op
There's a steady 2x improvement between each approach.
There was a problem hiding this comment.
For this case, readability matters more than performance. How many of these operations are being performed per Trino query?
hashhar
left a comment
There was a problem hiding this comment.
Soft LGTM.
I'll look at Joiner impl and give a green light/request changes.
No description provided.