-
Enhancement
-
Resolution: Duplicate
-
P5
-
None
-
1.4.0
-
generic
-
solaris_7
for
String s1, s2;
s1 += s2;
we can generate slightly better code by using
s1 = s1.append(s2);
which allocates twice: a String and a byte[] once, compared to what we currently generate
new StringBuffer().append(s1).append(s2).toString();
which allocates at least thrice: a StringBuffer, a byte[], a String, and possible more byte[] arrays during buffer expansion. An intermediary would be
new StringBuffer(s1).append(s2).toString();
which would at least avoid expansion for the first append.
Apparently the old compiler performed this optimization.
String s1, s2;
s1 += s2;
we can generate slightly better code by using
s1 = s1.append(s2);
which allocates twice: a String and a byte[] once, compared to what we currently generate
new StringBuffer().append(s1).append(s2).toString();
which allocates at least thrice: a StringBuffer, a byte[], a String, and possible more byte[] arrays during buffer expansion. An intermediary would be
new StringBuffer(s1).append(s2).toString();
which would at least avoid expansion for the first append.
Apparently the old compiler performed this optimization.
- duplicates
-
JDK-4059189 Compiler generated code for String.operator+() could avoid allocations.
- Closed
- relates to
-
JDK-8014432 Throwable.printStackTrace does unnecessary String concatenation
- Closed