<Swing Dev> Replace concat String to append in StringBuilder parameters
Claes Redestad
claes.redestad at oracle.com
Sun Aug 10 23:03:32 UTC 2014
+1
Some suggestions (mostly nits):
- in places like src/share/classes/java/util/regex/Pattern.java you
introducesingle-char
strings which might use a char instead:
- result.append("|"+next);
+ result.append('|').append(next);
- in places like src/share/classes/sun/security/provider/PolicyFile.java
you end up with a sequence of String literal appends which could be
merged into one:
- sb.append(principalInfo[i][0] + " " +
- "\"" + principalInfo[i][1] + "\"");
+ sb.append(principalInfo[i][0]).append(" \"")
+ .append(principalInfo[i][1]).append('"');
- in some places like src/share/classes/java/text/ChoiceFormat.java
you end up doing append(""). I guess the empty string concatenation was used as a form
of primitive-to-string coercion and was probably always redundant already, but care needs
to be taken that doing the append directly behave as intended.
I think it should be safe to just remove the empty string append in most cases:
- result.append(""+choiceLimits[i]);
+ result.append(choiceLimits[i]);
Thanks!
/Claes
On 2014-08-10 23:33, Otávio Gonçalves de Santana wrote:
> *Motivation:* Make another append instead of concat String inside of append
> parameter in StringBuilder class. To avoid an extra StringBuilder created
> for the purpose of concatenating. So it will save memory and will faster
> than concat String.
> Doing a code to benchMark[1], the result is:
>
> Benchmark Mode Samples
> Mean Mean error Units
> m.StringBuilderConcatBenchMark.stringBuilder thrpt 10
> 6317444.705 108673.584 ops/s
> m.StringBuilderConcatBenchMark.stringBuilderWithConcat thrpt 10
> 3354554.435 68353.924 ops/s
>
> The webrev of all code is:
> https://dl.dropboxusercontent.com/u/16109193/open_jdk/string_builder_concat.zip
> <https://dl.dropboxusercontent.com/u/16109193/open_jdk/string_builder_concat.zip>
>
> [1]
>
> @State(Scope.Thread)
> @OutputTimeUnit(TimeUnit.SECONDS)
> public class StringBuilderConcatBenchMark {
>
>
> private static final String F = "!!!!";
> private static final String E = " running in Java ";
> private static final String D = " in the code ";
> private static final String C = " to try impact ";
> private static final String B = " with some text ";
> private static final String A = "Doing a test";
>
> @GenerateMicroBenchmark
> public void stringBuilder(BlackHole bh) {
> bh.consume(createBuilder(A, B, C, D, E, F));
> }
>
> @GenerateMicroBenchmark
> public void stringBuilderWithConcat(BlackHole bh) {
> bh.consume(createBuilderWithConcat(A, B, C, D, E, F));
> }
>
> private StringBuilder createBuilder(String... values) {
> StringBuilder text = new StringBuilder();
> text.append(values[0]).append(values[1])
> .append(values[2]).append(values[3])
> .append(values[4]).append(values[5]);
> return text;
> }
> private StringBuilder createBuilderWithConcat(String... values) {
> StringBuilder text = new StringBuilder();
> text.append(values[0] + values[1])
> .append(values[2] + values[3])
> .append(values[4]+ values[5]);
> return text;
> }
> }
>
More information about the swing-dev
mailing list