HotSpot可以优化掉纯方法的冗余调用而无需内联它们吗? [英] Can HotSpot optimize away redundant calls to pure methods without inlining them?

查看:119
本文介绍了HotSpot可以优化掉纯方法的冗余调用而无需内联它们吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

纯方法是那些没有副作用的方法:它们唯一的作用是返回一个值,即它们的参数的函数。

Pure methods are those without side effects: their only effect is to return a value which is a function of their arguments.

对具有相同参数的相同纯方法的两次调用将返回相同的值。因此,如果两次调用具有相同参数的纯方法,HotSpot可以优化第二次调用,只需重新使用第一次调用的值吗?

Two calls to the same pure method with the same arguments will return the same value. So, given two calls to a pure method with identical arguments, can HotSpot optimize away the second call, simply re-using the value from the first call?

例如:

int add(int x, int y) {
  return x + y;
}

int addTwice(int x, int y) {
  return add(x, y) + add(x, y);
}

如果HotSpot不内联添加里面 addTwice 是否理解添加因此调用只加一次并将返回值加倍?

If HotSpot doesn't inline add inside addTwice does it understand that add is pure and thus call add only once and double the return value?

当然,这样一个微不足道的[mcve]不太可能直接感兴趣,但由于内联,发散控制流程,自动生成的代码等,在实践中可能会出现类似的情况。

Of course, such a trivial [mcve] isn't likely to be of direct interest, but similar situations can occur in practice due to inlining, divergent control flow, auto-generated code, etc.

推荐答案

<到目前为止,HotSpot无法做到这一点。

HotSpot cannot do this so far.

如果没有内联,则JIT编译器的方法调用通常是不透明的。很难进行跨方法优化。其中一个原因是方法入口点是易失性的,即由于JIT编译,重新编译,去优化,JVMTI调用等原因,它可以在运行时同时更改。当HotSpot进行显式方法调用时,它不知道目标方法是否被解释或编译,它是否收集JIT统计信息,是否正在调试,是否在内部有断点,或者是否启用了JVMTI方法事件。

If not inlined, a method call is typically opaque for JIT compiler. It is hard to make cross-method optimizations. One of the reasons is that a method entry point is volatile, i.e. it can change concurrently at run-time due to JIT compilation, re-compilation, deoptimization, JVMTI calls and so on. When HotSpot makes an explicit method call, it does not know whether the target method is interpreted or compiled, wether it collects JIT statistics, whether it is being debugged, if it has a breakpoint inside, or if JVMTI method events are enabled.

另一方面,即使存在这样的优化,也不会太有用。纯粹的方法在他们能做的事情上是非常有限的,所以它们通常简短而且有很多机会被内联。在内联之后,JIT可以更容易地在同一编译范围内进行优化。

On the other hand, even if such optimization existed, it would not be too useful. Pure methods are very limited in what they can do, so they are typically short and simple and have much chances to be inlined. It is much easier for JIT to do optimizations within the same compilation scope after inlining.

这篇关于HotSpot可以优化掉纯方法的冗余调用而无需内联它们吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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