atom feed2 messages in com.googlegroups.jvm-languagesRe: [jvm-l] Description of -XX:+Optim...
FromSent OnAttachments
Attila SzegediApr 22, 2011 11:06 am 
Rémi ForaxApr 24, 2011 1:57 pm 
Subject:Re: [jvm-l] Description of -XX:+OptimizeStringConcat ?
From:Rémi Forax (
Date:Apr 24, 2011 1:57:20 pm

On 04/22/2011 08:06 PM, Attila Szegedi wrote:

Hi folks,

Hi Attila, All is here:

can anyone in the know summarize to me how exactly does
-XX:+OptimizeStringConcat work? Googling didn't turn up anything useful…

Thanks, and apologies for slightly off-topic, but this group seems to be the
focal point of knowledgable JVM people nowadays.

It recognizes pattern like new StringBuilder().append(...).toString() with ... being a String, a char or an int (it doesn't seem to recognize if it's an Object) and tries to create the corresponding String once.

Recursive patterns like new StringBuilder().append(new StringBuiler().append(...).toString()).toString() are also recognized and then collapsed, append(Integer.toString(number)) is transformed to append(number), and there is a special handling if the string may be null (equivalent of append("null")).

The generated code sums the size of each char, integer or string to be appended then the char buffer of the string is allocated (without zeroig it!) and populated by copying the chars from the values to be appended. Then the String is created. (there is a code to reuse an existing String object but the part that detects which String object to reuse is guarded by a #if 0)

The result is that the String is allocated once and no StringBuilder are allocated at all.