JDK-6951567 : Pass StringBuilder's buffer directly to String
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 6
  • Priority: P3
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2010-05-11
  • Updated: 2010-08-11
  • Resolved: 2010-08-11
Related Reports
Duplicate :  
Description
A DESCRIPTION OF THE REQUEST :
Consider the example code.  StringBuilder is not going to escape.  A String is going to be created at the end.  We can eliminate a memory allocation and buffer copy by simply having the String object reference the char[] in StringBuilder.  Since the StringBuilder can't be used any more, the optimized code can simply have String reference the char[] in StringBuilder.

JUSTIFICATION :
Consider the example code.  StringBuilder is not going to escape.  A String is going to be created at the end.  We can eliminate a memory allocation and buffer copy by simply having the String object reference the char[] in StringBuilder.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The char[] referenced in StringBuilder be passed to String.
ACTUAL -
A separate char[] is created for String and populated with the contents of StringBuilder's char[].

---------- BEGIN SOURCE ----------


StringBuilder result;

result = new StringBuilder();

result.append("hello");
...

return(result.toString());
---------- END SOURCE ----------

Comments
EVALUATION Server VM already implemented such optimization: 6650179: StringBuilder.toString() and StringBuffer.toString() optimization This optimization is currently considered as experimental which is not enabled by default.
11-08-2010