I absolutely understand that this does not answer the question, I just wanted to mention that jdk-9 introduces another optimization, which is called by default:
-XX: + CompactStrings
where Latin1 characters occupy one byte instead of two (via char). Because of this, many internal String methods have changed - they act the same for the user, but internally they are faster in many cases.
Also in the case of strings, to combine two strings together through a plus sign, javac is about to generate another bytecode.
There is no bytecode instruction that concatenates two lines together, so javac will generate
StringBuilder # Append
in the background. Before jdk-9.
Now bytecode delegates
StringConcatFactory # makeConcatWithConstants
or
StringConcatFactory # makeConcat
using the invokedynamic bytecode command:
aload_0 1: aload_2 2: aload_1 3: invokedynamic #8, 0 // InvokeDynamic #0:makeConcatWithConstants:(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String; 8: areturn
Like two concatenated strings are the runtime solution now. it can still be a StringBuilder or it can be a concatenation of byte arrays, etc. All you know is that this can change, and you will get the fastest solution.
EDIT
I just debugged and saw that there are quite a few strategies for adding these lines:
private enum Strategy { /** * Bytecode generator, calling into {@link java.lang.StringBuilder}. */ BC_SB, /** * Bytecode generator, calling into {@link java.lang.StringBuilder}; * but trying to estimate the required storage. */ BC_SB_SIZED, /** * Bytecode generator, calling into {@link java.lang.StringBuilder}; * but computing the required storage exactly. */ BC_SB_SIZED_EXACT, /** * MethodHandle-based generator, that in the end calls into {@link java.lang.StringBuilder}. * This strategy also tries to estimate the required storage. */ MH_SB_SIZED, /** * MethodHandle-based generator, that in the end calls into {@link java.lang.StringBuilder}. * This strategy also estimate the required storage exactly. */ MH_SB_SIZED_EXACT, /** * MethodHandle-based generator, that constructs its own byte[] array from * the arguments. It computes the required storage exactly. */ MH_INLINE_SIZED_EXACT }
Default value:
MH_INLINE_SIZED_EXACT