RFC: AbstractStringBuilder sharing its buffer with String

Ivan Gerasimov ivan.gerasimov at oracle.com
Fri Nov 27 12:39:09 UTC 2015


Hello!

Prior to Java5, StringBuffer used to be able to share its internal 
char[] buffer with the String, returned with toString().
With introducing of the new StringBuilder class this functionality was 
removed.

It seems tempting to reintroduce this feature now in AbstractStringBuilder.
The rationale here is that StringBuilder already provides a way of 
accepting the hint about the result's size.
The constructor with the explicitly specified capacity is used for 
increasing the efficiency of strings concatenations.
Optimizing this case by avoiding additional memory allocation and 
copying looks sensible to me.

Here's the draft webrev
http://cr.openjdk.java.net/~igerasim/XXXXXXX-ASB-shared-value/00/webrev/

This variant showed a significant speed improvement for the cases, when 
the the capacity is equal to the result's size (up to +25% to throughput).
For the other cases, the difference isn't very clear based on my 
benchmarks :)
But is seems to be small enough, as it only adds a few comparisons, 
coupled with already relatively heavy operations, like allocation and 
copying.

Here's the benchmark that I've used:
http://cr.openjdk.java.net/~igerasim/XXXXXXX-ASB-shared-value/MyBenchmark.java

And the corresponding graphs.
http://cr.openjdk.java.net/~igerasim/XXXXXXX-ASB-shared-value/ASB-Shared-bench-test15.png
http://cr.openjdk.java.net/~igerasim/XXXXXXX-ASB-shared-value/ASB-Shared-bench-test17.png
The blue line here stands for the baseline throughput.

If people agree, this is a useful addition, a test should also be added, 
of course.

Sincerely yours,
Ivan




More information about the core-libs-dev mailing list