Hi Peter!
On 27.11.2015 18:15, Peter Levart wrote:
On 11/27/2015 01:39 PM, Ivan Gerasimov wrote:
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/
It is tempting, yes. But I think there was a reason that this was
dropped when StringBuilder was introduced and that reason was
thread-safety. StringBuilder doesn't use any synchronization. If a
concurrent thread gets a reference to a StringBuilder and executes
mutating operations while some other thread calls toString() on the
same StringBuilder, then returned String could appear to be changing
it's content (be mutable), which can have security implications:
Yes, it's a good point!
I think it may be possible to fix this through using block-free
synchronization.
However, the overhead may still slow things down in the common case.
Let me try to implement another draft with will address this issue to
see how fast it is.
Sincerely yours,
Ivan