The optimization is to have the (JIT) compiler identify StringBuilder and StringBuffer usages where a StringBuilder or StringBuffer is allocated, things are appended to the StringBuilder or StringBuffer and StringBuilder.toString() or StringBuffer.toString() is called. Usages where the StringBuilder or StringBuffer does not escape or becomes dead, the char[] allocated by the StringBuilder or StringBuffer could be assigned to the String returned by the toString() method of StringBuilder or StringBuffer instead of allocating an additional char[] in a new String and copying the char's from the StringBuilder or StringBuffer's char[] to the String's newly allocated char[].
The end result is the saving of an unnecessary char[] allocation and a subsequent call to arraycopy.
The end result is the saving of an unnecessary char[] allocation and a subsequent call to arraycopy.
- duplicates
-
JDK-6500646 Use Escape Analysis to optimize StringBuffer.toString()
- Closed
-
JDK-6892658 C2 should optimize some stringbuilder patterns
- Closed
- relates to
-
JDK-6650182 new String(string1 + string2) optimization
- Closed