8 ms·
Java StringBuffer and StringBuilder performance
- briane80 10y agoI wonder what effect +OptimiseStringConcat has on those benchmarks. It's supposed to collapse a lost of .append calls and tries to use the minimum amount of StringBuilder objects
- jcdavis 10y agoHuge, like several times faster (3x when I benchmarked IIRC). I'm a little surprised the author didn't mention it anywhere - it might account for some of the differences he saw. However with a normal JVM the only way to know if its happening (AFAIK) is to look at the compilation logs or JIT'd assembly, bytecode isn't enough
- alblue 10y agoFWIW I didn't mention/test the -XX:+OptimizeStringConcat, because the place where the changes were being made didn't have that flag enabled and as such wouldn't have been representative of the use case behind the blog post. In any case, I re-ran the benchmarks with -appendJvmArgs -XX:+OptimizeStringConcat and saw no significant difference in the timing on OSX. You can of course run the gist linked at the bottom and see for yourself whether there is an impact for your OS but there is JEP280 which is looking at generating invoke dynamic for Java 9 which will change the measurements again and make the -XX:+OptimizeStringConcat flag obsolete.
- jcdavis 10y ago-XX:+OptimizeStringConcat is enabled by default everywhere I can remember, AFAIK
- vardump 10y agoHow about just using StringBuilder unless you really (that 0.01% case) need thread synchronization of StringBuffer? I'm a C++ programmer mainly, surely all Java programmers know why StringBuffer is bad by now? Article also has a pretty pointless microbenchmark. Most synthetic microbenchmarks are questionable by default. Even worse when there's any kind of limited resource utilization such as thread synchronization involved. Synthetic microbenchmarks assume all the resources are just for them. Whole CPU instruction and data cache, all synchronization bandwidth, all memory bandwidth, etc. Use profiling instead of microbenchmarks.
- lmm 10y ago> How about just using StringBuilder unless you really (that 0.01% case) need thread synchronization of StringBuffer? You wouldn't necessarily know, or notice - your users would just experience occasional random crashes. How about just using StringBuffer unless you really (that 0.01% case) need the performance of StringBuilder? As you say, profiling is the right way to approach these things.
- vardump 10y agoAre you playing a devil's advocate? I just don't think this is the right place to do the synchronization. Wouldn't a Double box (that's really a "SynchronizedDouble") be silly? Besides, can you think of any legitimate reason to concurrently append to a StringBuffer in the first place?
- kodablah 10y ago> How about just using StringBuffer unless you really (that 0.01% case) need the performance of StringBuilder? Because when writing new code you should opt-in to thread safety, not out. Collections are not concurrent by default, there is no `unsynchronized` or `non-volatile` keywords, etc. StringBuilder:StringBuffer::HashMap:ConcurrentHashMap. Also, if the performance benefit requires negligible upfront cost (e.g. providing length to ArrayList if known) you should do it and build it in your muscle memory as the default approach. The problem is StringBuffer (unlike many other parts of the stdlib) was the only approach available for a long time so people need to change their default way of thinking. Luckily it's been a long time since 1.5 and basically everyone has changed.
- Someone 10y agoI don't think thread-safety is a big concern for string building APIs. I expect 90% or more of all uses of StringBuffer are easily verified to be used from only one thread because they allocate an instance, assign it to a local variable, append to it a few times, and finally get the string that was built exactly once. Actually, I don't see why it would take a very smart compiler to do this kind of escape analysis and automatically and safely replace StringBuffer by StringBuilder where possible. The only large downside I can see of that is that it would require the compiler to know quite a bit of the semantics of the standard library. If you refactor your code and pass the StringBuffer to (small) helper functions, doing this would be more work, but it would not get essentially harder.
- js2 10y agoI was surprised by the difference between the Simple and Chain results. Naively, I had thought they'd generate the same byte code.
- kodablah 10y agoThe main deal is that an instance method (e.g. append) uses the latest stack value as the instance and has its return value put on the stack. So when chaining, it doesn't need to re-put the variable on the stack to call the next method. When not chaining, it has to pop (i.e. discard) that previous return value and then re-put the builder/buffer variable on the stack each time. But this is seriously micro-opimtization only beneficial in the most strict of situations. Only in this crazy situations would I ever use this as justification to choose one approach over the other.
- leothekim 10y agoWondering if the similarity in performance of StringBuffer and StringBuilder has to do with lock elision. Can see if the compiler is doing this by examining the generated bytecode.
- logn 10y agoIt might. There are other optimizations for synchronization introduced around Java 1.6 after wisdom around StringBuilder was already common. I think it's still safe to say StringBuilder at least can't hurt performance. More info: https://www.infoq.com/articles/java-threading-optimizations-p1 https://www.infoq.com/articles/java-threading-optimizations-... Separately, it's interesting to me chained calls can be more efficient. Personally I prefer these longer, multi-line type of statements from a readability perspective. So I'm happy it happens to be efficient.