RFC: AbstractStringBuilder sharing its buffer with String

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


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 

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

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 

Here's the benchmark that I've used:

And the corresponding graphs.
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,

More information about the core-libs-dev mailing list