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.
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.
With kind regards,
Ivan