RFR 8170348: Appendable.appendN(char, int) method to append multiple copies of char

Peter Levart peter.levart at gmail.com
Sun Dec 11 22:14:43 UTC 2016


Hi Ivan,


On 12/11/2016 10:03 PM, Ivan Gerasimov wrote:
> Thank you Peter for the suggestion!
>
>
>> An alternative to a new virtual method on Appendable (or maybe a 
>> complement to it) could be a special internal CharSequence 
>> implementation (CharRepetitions) with a static factory method on 
>> CharSequence like the following:
>>
>
> I think it's a clever idea!
>
> Though it might be harder to implement the special optimized handling 
> of such sequences by Appendable implementations outside java.lang.

You are right. But even without such optimization, appending of such 
sequence is still faster than appending a solid String although slower 
than optimized appendN().

>
>
>> http://cr.openjdk.java.net/~plevart/jdk9-dev/8170348_Appendable.appendN.alt/webrev.01/ 
>>
>>
>> Together with special-case optimization in 
>> AbstractStringBuilder.append(CharSequence) it can perform equally 
>> well when JITed. I took your benchmark and modified it a bit:
>>
>> http://cr.openjdk.java.net/~plevart/jdk9-dev/8170348_Appendable.appendN.alt/AppendNTest.java 
>>
>>
>> ...I moved sb.setLength(0) into a special @Setup method so that it 
>> doesn't cause the remaining tested code to be over-optimized. You can 
>> try just this change in your benchmark and you'll notice a difference. 
> Actually, in the benchmark I tried to follow the suggestions found here:
> http://hg.openjdk.java.net/code-tools/jmh/file/ef24f1b5de08/jmh-samples/src/main/java/org/openjdk/jmh/samples/JMHSample_38_PerInvokeSetup.java 
>
> Note, how the Level.Invocation setup is avoided in the measureRight 
> benchmark.

Ups, I missed that comment on Level.Invocation. I suspected that 
something was not right since the results were so different (appending 
seemed much slower)....

So here are the results with sb.setLength(0) moved back to benchmark 
methods:

Benchmark                  (size)  Mode  Cnt   Score   Error  Units
AppendNTest.test_0_New          0  avgt   10   2.380 ± 0.081  ns/op
AppendNTest.test_0_New          1  avgt   10   4.323 ± 0.139  ns/op
AppendNTest.test_0_New          5  avgt   10   9.233 ± 0.859  ns/op
AppendNTest.test_0_New         10  avgt   10   8.977 ± 0.577  ns/op
AppendNTest.test_0_New         20  avgt   10   9.454 ± 0.157  ns/op
AppendNTest.test_1_Old          0  avgt   10   2.262 ± 0.282  ns/op
AppendNTest.test_1_Old          1  avgt   10   4.534 ± 0.037  ns/op
AppendNTest.test_1_Old          5  avgt   10  15.798 ± 0.734  ns/op
AppendNTest.test_1_Old         10  avgt   10  29.086 ± 0.757  ns/op
AppendNTest.test_1_Old         20  avgt   10  56.417 ± 6.240  ns/op
AppendNTest.test_2_Solid        0  avgt   10   6.124 ± 0.112  ns/op
AppendNTest.test_2_Solid        1  avgt   10  10.305 ± 0.084  ns/op
AppendNTest.test_2_Solid        5  avgt   10  10.599 ± 0.109  ns/op
AppendNTest.test_2_Solid       10  avgt   10  11.574 ± 0.716  ns/op
AppendNTest.test_2_Solid       20  avgt   10  11.646 ± 0.878  ns/op
AppendNTest.test_3_Repeat       0  avgt   10   2.514 ± 0.187  ns/op
AppendNTest.test_3_Repeat       1  avgt   10   4.457 ± 0.096  ns/op
AppendNTest.test_3_Repeat       5  avgt   10   7.744 ± 0.295  ns/op
AppendNTest.test_3_Repeat      10  avgt   10   9.523 ± 1.507  ns/op
AppendNTest.test_3_Repeat      20  avgt   10   9.195 ± 0.009  ns/op

Still comparable.

Regards, Peter

>
> With kind regards,
> Ivan
>



More information about the core-libs-dev mailing list