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