Description of -XX:+OptimizeStringConcat ?

3,328 views
Skip to first unread message

Attila Szegedi

unread,
Apr 22, 2011, 2:06:33 PM4/22/11
to jvm-la...@googlegroups.com
Hi folks,

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.
Attila.

Rémi Forax

unread,
Apr 24, 2011, 4:57:43 PM4/24/11
to jvm-la...@googlegroups.com
On 04/22/2011 08:06 PM, Attila Szegedi wrote:
> Hi folks,

Hi Attila,
All is here:
http://hg.openjdk.java.net/jdk7/jdk7/hotspot/raw-file/1d1603768966/src/share/vm/opto/stringopts.cpp

> 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.

> Attila.
>

R�mi

Reply all
Reply to author
Forward
0 new messages