CallSite解释需要

huangapple go评论86阅读模式
英文:

CallSite explanation needed

问题

CallSite文档中:

> CallSite是一个持有变量MethodHandle(称为其目标)的容器。与CallSite关联的所有invokedynamic指令都委托调用到该站点的当前目标。一个CallSite可以与多个invokedynamic指令关联,也可以是“自由浮动”的,即未关联任何指令。在任何情况下,都可以通过关联的方法句柄调用其动态调用者来调用CallSite。

问题:

  1. CallSite内的目标是否可以更改?如何更改?请提供代码示例。

  2. 如何将CallSite与多个invokedynamic指令关联?

  3. “自由浮动” CallSite是什么意思?如何创建?

编辑:
注:我想了解编写代码/lambda的不同方法,以使这3个要点在程序中实现。

英文:

From the CallSite documentation

> A CallSite is a holder for a variable MethodHandle, which is called its target. An invokedynamic instruction linked to a CallSite delegates all calls to the site's current target. A CallSite may be associated with several invokedynamic instructions, or it may be "free floating", associated with none. In any case, it may be invoked through an associated method handle called its dynamic invoker.

Questions:

  1. Can the target within CallSite be changed? how? Please provide a code sample.

  2. How can a CallSite be associated with multiple invokedynamic instructions?

  3. What is meant by "free-floating" CallSite? How can it be created?

EDIT:
Note: I want to know the different ways to write code/lambda so that these 3 points occur in program.

答案1

得分: 3

  1. 是的。在MutableCallSiteVolatileCallSite中,目标可以通过CallSite#setTarget进行更改。
  2. JVM规范的此部分定义了动态计算的CallSite如何初始化。但为了回答这个问题,我认为这是有意设计的。即JVM规范表示JVM实现应该这样做,而编译器可能会利用它来获得优势。
  3. 您不会直接在您的代码中直接创建CallSites,因为编译器会完成这一过程,它只是用来说明可能会存在“浮动”的CallSites。

编辑:我认为您误解了文档,我认为您不应该直接从您的代码中初始化CallSite,因为这没有用处,文档的存在是为了说明它的作用,而JVM则使用它为invokedynamic指令提供MethodHandle。

英文:
  1. Yes. The target in a MutableCallSite or a VolatileCallSite can be changed via CallSite#setTarget
  2. This section of the JVM Specification defines how dynamically computed CallSites are initialized. But to answer the question, I think it is by design. i.e. the JVM Spec says it should for JVM Implementations, and compilers may use it for their advantage
  3. You do not create the CallSites directly in your code, as the compiler does it, and it is just there to state that there might be CallSites that are "free-floating".

Edit: I think you misinterpreted the documentation, I think you should never initialize CallSite directly from you code, since there is no use, and the documentation exists to inform what it does, and the JVM uses it to provide a MethodHandle for a invokedynamic instruction.

huangapple
  • 本文由 发表于 2020年8月31日 17:31:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/63668261.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定