StringConcatFactory中的策略 [英] Strategy in StringConcatFactory

查看:98
本文介绍了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(),该生成的CallSiteinvokedynamic.

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");
}

  1. BytecodeStringBuilderStrategy处理BC_SBBC_SB_SIZEDBC_SB_SIZED_EXACT.如果使用Java代码编写串联,它将生成与javac相同的使用StringBuilder的字节代码.主要区别在于,此字节码是在运行时(而不是编译时)生成的,并使用Unsafe.defineAnonymousClass()加载.
  2. MethodHandleStringBuilderStrategy处理MH_SB_SIZEDMH_SB_SIZED_EXACT.它使用MethodHandle机器(包括MethodHandle组成)在StringBuilder调用之上构建相同的串联链.它不使用任何私有API(例如Unsafe),因此它可能是最不可移植的策略.
  3. MethodHandleInlineCopyStrategy处理MH_INLINE_SIZED_EXACT.它还使用MethodHandle机器来构建具有MethodHandle组成的串联链,但不是StringBuffer而是直接与字节数组一起使用,从而避免了可能的复制.为此,它使用了一些内部API和一些有关JDK实现细节的知识(例如,避免复制字符串字节的String构造函数).因此,此实现更脆弱(对于JDK更改),但它也允许更快的速度.
  1. BytecodeStringBuilderStrategy handles BC_SB, BC_SB_SIZED and BC_SB_SIZED_EXACT. It generates the same StringBuilder-using byte-code that javac 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 using Unsafe.defineAnonymousClass().
  2. MethodHandleStringBuilderStrategy handles MH_SB_SIZED and MH_SB_SIZED_EXACT. It uses MethodHandle machinery (including MethodHandle composition) to build the same concatenation chain on top of StringBuilder calls. It does not use any private APIs (like Unsafe), so it is probably the least non-portable strategy.
  3. MethodHandleInlineCopyStrategy handles MH_INLINE_SIZED_EXACT. It also uses MethodHandle machinery to build concatenation chain with MethodHandle composition, but instead of StringBuffer 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 (like String 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.

  1. BytecodeStringBuilderStrategy(BC_SB_xxx)使用StringBuilder和运行时代码生成.
  2. MethodHandleStringBuilderStrategy(MH_SB_xxx)使用StringBuilderMethodHandle s
  3. MethodHandleInlineCopyStrategy(MH_INLINE_SIZED_EXACT)使用字节数组和MethodHandle s.
  1. BytecodeStringBuilderStrategy (BC_SB_xxx) uses StringBuilder and runtime code generation.
  2. MethodHandleStringBuilderStrategy (MH_SB_xxx) uses StringBuilder and MethodHandles
  3. MethodHandleInlineCopyStrategy (MH_INLINE_SIZED_EXACT) uses byte array and MethodHandles.

这篇关于StringConcatFactory中的策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆