StringBuilder buffer allocation support in compiler

Fredrik Öhrström oehrstroem at gmail.com
Mon Jun 9 22:39:58 UTC 2014


I do not know how Hotspot optimizes string concatenations, but JRockit did
some fun things as well, like replacing StringBuilder (at runtime) with an
internal JRockit specific StringMaker, that merely stored references to the
constant strings in an array. Thus the .append(x) did very little work, all
the concatenation work then happened in toString() where it first scanned
the appended strings length and could calculate the final length of the
concatenated string. Thus only a single allocation was necessary and there
was no superfluous copying.




2014-06-05 23:16 GMT+02:00 John Rose <john.r.rose at oracle.com>:

> On Jun 5, 2014, at 12:27 PM, Laszlo Hornyak <laszlo.hornyak at gmail.com>
> wrote:
>
>     public static String sayHello(String name, int age) {
>         return "Hello " + name + ", this is your " + age + "th birthday";
>     }
>
>
> Try this example (suitably wrapped) with -XX:-OptimizeStringConcat and
>  -XX:+OptimizeStringConcat.
>
> You should see a substantial difference.  I saw 3x.
>
> Pre-optimizing bytecode shapes (in javac) is usually a bad idea.
>  Bytecodes are best thought of as a portable representation of programmer
> intention, not a vehicle for optimization.
>
> — John
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20140610/69a1576a/attachment-0001.html>


More information about the compiler-dev mailing list