问题
I have know the invokedynamic instruction.
Also I have known the basic process how it implements. But when I arrive the code. I can't understand the code in StringConcatFactory.
Can you tell me how the six strategies implements by the source code. Only the default strategy is also done. As a university student, I can't under the source code.
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
}
回答1:
makeConcat() and makeConcatWithConstants(), which are StringConcatFactory API entry points, both use doStringConcat(), which produces a CallSite used by the invokedynamic.
doStringConcat() calls generate(Lookup, String, MethodType, Recipe) which contains the following switch on the enum you are asking about:
switch (STRATEGY) {
case BC_SB:
return StringConcatFactory.BytecodeStringBuilderStrategy.generate(lookup, className, mt, recipe, StringConcatFactory.Mode.DEFAULT);
case BC_SB_SIZED:
return StringConcatFactory.BytecodeStringBuilderStrategy.generate(lookup, className, mt, recipe, StringConcatFactory.Mode.SIZED);
case BC_SB_SIZED_EXACT:
return StringConcatFactory.BytecodeStringBuilderStrategy.generate(lookup, className, mt, recipe, StringConcatFactory.Mode.SIZED_EXACT);
case MH_SB_SIZED:
return StringConcatFactory.MethodHandleStringBuilderStrategy.generate(mt, recipe, StringConcatFactory.Mode.SIZED);
case MH_SB_SIZED_EXACT:
return StringConcatFactory.MethodHandleStringBuilderStrategy.generate(mt, recipe, StringConcatFactory.Mode.SIZED_EXACT);
case MH_INLINE_SIZED_EXACT:
return StringConcatFactory.MethodHandleInlineCopyStrategy.generate(mt, recipe);
default:
throw new StringConcatException("Concatenation strategy " + STRATEGY + " is not implemented");
}
BytecodeStringBuilderStrategyhandlesBC_SB,BC_SB_SIZEDandBC_SB_SIZED_EXACT. It generates the sameStringBuilder-using byte-code thatjavacwould generate if you just wrote the concatenation in your Java code. The major difference is that this byte-code is generated at runtime (and not compile time) and loaded usingUnsafe.defineAnonymousClass().MethodHandleStringBuilderStrategyhandlesMH_SB_SIZEDandMH_SB_SIZED_EXACT. It usesMethodHandlemachinery (includingMethodHandlecomposition) to build the same concatenation chain on top ofStringBuildercalls. It does not use any private APIs (likeUnsafe), so it is probably the least non-portable strategy.MethodHandleInlineCopyStrategyhandlesMH_INLINE_SIZED_EXACT. It also usesMethodHandlemachinery to build concatenation chain withMethodHandlecomposition, but instead ofStringBufferit works with a byte array directly, avoiding copying where possible. To do it, it uses some internal APIs and some knowledge about JDK implementation details (likeStringconstructor that avoids copying string bytes). So this implementation is more fragile (to JDK changes), but also it allows more speed.
A little summary
We can see that the three differ in two aspects: a) what is used as a buffer to build up the string and b) how the concatenation chain is created.
BytecodeStringBuilderStrategy(BC_SB_xxx) usesStringBuilderand runtime code generation.MethodHandleStringBuilderStrategy(MH_SB_xxx) usesStringBuilderandMethodHandlesMethodHandleInlineCopyStrategy(MH_INLINE_SIZED_EXACT) uses byte array andMethodHandles.
来源:https://stackoverflow.com/questions/50084240/strategy-in-stringconcatfactory