RFR [9]: 8050142: Optimize java.util.Formatter

Andrej Golovnin andrej.golovnin at gmail.com
Mon Jul 14 11:08:14 UTC 2014


Hi Claes,

in the method Formatter$FormatSpecifier#justify(String) you can
pre-calculate the capacity of the StringBuilder to avoid array copying,
e.g. instead of

2931             StringBuilder sb = new StringBuilder();

use this one:

2931             StringBuilder sb = new StringBuilder(s.length() + sp);

And in the method Formatter$FormatSpecifier#justify(StringBuilder) you can
avoid creation of the StringBuilder object in the line 2956, e.g. instead
of:

2956                 StringBuilder tmp = new StringBuilder(sp);
2957                 for (int i = 0; i < sp; i++) {
2958                     tmp.append(' ');
2959                 }
2960                 sb.insert(0, tmp);

you can write this:

2956                 char[] tmp = new char[sp];
2957                 Arrays.fill(tmp, ' ');
2958                 sb.insert(0, tmp);

It's not a big improvement but maybe you can change the line

3781                                 exp.append("0").append(len - 1);

to use the character-based API to append a single character.

Best regards,
Andrej Golovnin

On Mon, Jul 14, 2014 at 12:07 PM, Claes Redestad <claes.redestad at oracle.com>
wrote:

> Hi,
>
>  please review this patch which optimizes away some allocations from
> java.util.Formatter and achieve 1.1-1.3x speedups of micros targetting
> String.format. See bug for more details.
>
>  webrev: http://cr.openjdk.java.net/~redestad/8050142/webrev.0
>  bug: https://bugs.openjdk.java.net/browse/JDK-8050142
>
>  Testing: JPRT, jtreg (java/lang/String, java/util/Formatter), SPECjbb2013
> and microbenchmarks
>
>  Thanks!
>
>  /Claes
>



More information about the core-libs-dev mailing list