StringConcatFactory中的策略 [英] Strategy in StringConcatFactory
问题描述
我知道invokedynamic指令.
I have know the invokedynamic instruction.
我也知道它是如何实现的基本过程.但是当我到达代码时.我无法理解StringConcatFactory
中的代码.
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
}
推荐答案
makeConcat()
和makeConcatWithConstants()
,它们是StringConcatFactory
API的入口点,都使用doStringConcat()
,该生成的CallSite
由invokedynamic
.
makeConcat()
and makeConcatWithConstants()
, which are StringConcatFactory
API entry points, both use doStringConcat()
, which produces a CallSite
used by the invokedynamic
.
doStringConcat()
调用generate(Lookup, String, MethodType, Recipe)
,其中包含您要查询的枚举上的以下开关:
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");
}
-
BytecodeStringBuilderStrategy
处理BC_SB
,BC_SB_SIZED
和BC_SB_SIZED_EXACT
.如果使用Java代码编写串联,它将生成与javac
相同的使用StringBuilder
的字节代码.主要区别在于,此字节码是在运行时(而不是编译时)生成的,并使用Unsafe.defineAnonymousClass()
加载. -
MethodHandleStringBuilderStrategy
处理MH_SB_SIZED
和MH_SB_SIZED_EXACT
.它使用MethodHandle
机器(包括MethodHandle
组成)在StringBuilder
调用之上构建相同的串联链.它不使用任何私有API(例如Unsafe
),因此它可能是最不可移植的策略. -
MethodHandleInlineCopyStrategy
处理MH_INLINE_SIZED_EXACT
.它还使用MethodHandle
机器来构建具有MethodHandle
组成的串联链,但不是StringBuffer
而是直接与字节数组一起使用,从而避免了可能的复制.为此,它使用了一些内部API和一些有关JDK实现细节的知识(例如,避免复制字符串字节的String
构造函数).因此,此实现更脆弱(对于JDK更改),但它也允许更快的速度.
BytecodeStringBuilderStrategy
handlesBC_SB
,BC_SB_SIZED
andBC_SB_SIZED_EXACT
. It generates the sameStringBuilder
-using byte-code thatjavac
would 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()
.MethodHandleStringBuilderStrategy
handlesMH_SB_SIZED
andMH_SB_SIZED_EXACT
. It usesMethodHandle
machinery (includingMethodHandle
composition) to build the same concatenation chain on top ofStringBuilder
calls. It does not use any private APIs (likeUnsafe
), so it is probably the least non-portable strategy.MethodHandleInlineCopyStrategy
handlesMH_INLINE_SIZED_EXACT
. It also usesMethodHandle
machinery to build concatenation chain withMethodHandle
composition, but instead ofStringBuffer
it 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 (likeString
constructor that avoids copying string bytes). So this implementation is more fragile (to JDK changes), but also it allows more speed.
小结
我们可以看到这三个方面在两个方面有所不同: a)用于构建字符串的缓冲区,以及 b)
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
)使用StringBuilder
和运行时代码生成. -
MethodHandleStringBuilderStrategy
(MH_SB_xxx
)使用StringBuilder
和MethodHandle
s -
MethodHandleInlineCopyStrategy
(MH_INLINE_SIZED_EXACT
)使用字节数组和MethodHandle
s.
BytecodeStringBuilderStrategy
(BC_SB_xxx
) usesStringBuilder
and runtime code generation.MethodHandleStringBuilderStrategy
(MH_SB_xxx
) usesStringBuilder
andMethodHandle
sMethodHandleInlineCopyStrategy
(MH_INLINE_SIZED_EXACT
) uses byte array andMethodHandle
s.
这篇关于StringConcatFactory中的策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!