Java 优化:(Hotspot/Dalvik)返回常量的最终方法的优化?

发布于 2024-11-19 11:33:19 字数 126 浏览 7 评论 0原文

谁能告诉我 Hotspot 或 Dalvik 是否足够聪明,可以内联调用返回常量(静态最终)int 值的最终方法?理想情况下,方法调用将被常量替换。这可能是在类加载时或通过 JIT。

这对我正在处理的一些代码的设计有影响。

Can anyone tell me if either Hotspot or Dalvik is smart enough to inline calls to a final method returning a constant (static final) int value? Ideally the method call would be replaced by the constant. This might either be at class load time or through JIT.

This has implications in the design of some code I'm working on.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

素手挽清风 2024-11-26 11:33:19

我认为答案是“不,由于 final 关键字的存在或不存在,优化不会发生”,至少在 HotSpot VM 上是这样。但由于其他因素,优化可能会发生。

以下是 Brian Goetz 在本文中所说的话(抱歉长引用):

就像许多关于 Java 性能的神话一样,错误的信念是
将类或方法声明为更好性能的最终结果是
被广泛持有但很少被审查。该论点认为,声明一个
方法或类作为final意味着编译器可以内联方法
调用更加积极,因为它知道在运行时这是
绝对是要调用的方法的版本。但
这根本不是真的。只是因为 X 类是针对
最终的 Y 类并不意味着 Y 类的相同版本将是
在运行时加载。所以编译器无法内联这样的跨类
方法调用安全,无论是否最终。只有当方法是私有的时才可以
编译器自由内联它,在这种情况下,final 关键字将
是多余的。

另一方面,运行时环境和JIT编译器有更多
有关实际加载的类的信息,并且可以做很多
比编译器更好的优化决策。如果运行时
环境知道没有加载扩展 Y 的类,那么它
可以安全地内联调用 Y 的方法,无论 Y 是否为
最终(只要它可以使此类 JIT 编译的代码无效,如果
Y 的子类稍后加载)。所以现实是,虽然最终
对于一个愚蠢的运行时优化器来说可能是一个有用的提示,但它不会
执行任何全局依赖性分析,它的使用实际上并不
启用很多编译时优化,并且不需要
智能 JIT 执行运行时优化。

还有一篇很好的文章,解释为什么final 不再是final,至少在Java 5 中

I would think that the answer is "no, optimization will not happen because of absence or presence of the final keyword", at least on the HotSpot VM. But optimization will likely happen because of other factors.

Here's what Brian Goetz says in this article (sorry for the long quote):

Like many myths about Java performance, the erroneous belief that
declaring classes or methods as final results in better performance is
widely held but rarely examined. The argument goes that declaring a
method or class as final means that the compiler can inline method
calls more aggressively, because it knows that at run time this is
definitely the version of the method that's going to be called. But
this is simply not true. Just because class X is compiled against
final class Y doesn't mean that the same version of class Y will be
loaded at run time. So the compiler cannot inline such cross-class
method calls safely, final or not. Only if a method is private can the
compiler inline it freely, and in that case, the final keyword would
be redundant.

On the other hand, the run-time environment and JIT compiler have more
information about what classes are actually loaded, and can make much
better optimization decisions than the compiler can. If the run-time
environment knows that no classes are loaded that extend Y, then it
can safely inline calls to methods of Y, regardless of whether Y is
final (as long as it can invalidate such JIT-compiled code if a
subclass of Y is later loaded). So the reality is that while final
might be a useful hint to a dumb run-time optimizer that doesn't
perform any global dependency analysis, its use doesn't actually
enable very many compile-time optimizations, and is not needed by a
smart JIT to perform run-time optimizations.

There's also a good post why final is not final any more, at least in Java 5.

盛夏已如深秋| 2024-11-26 11:33:19

内联是 JIT 编译器在检测到热点时可能会执行的操作,该热点是字节代码中经常被调用的方法。可能值得花费一些 CPU 时间将字节代码编译成机器代码。

JIT 编译器很有可能会内联一个 final 方法(因为它不能被覆盖)。如果该方法只返回一个常量值,那么机会会更好。

但我的理解是 - 如果调用方法不是热点,那么它将不会被编译,并且不会内联final方法。

(德语信息源)

Inlining is something the JIT compiler might do if it detects a hot spot, a method in the byte code that has been called that often that it probably worth spending some CPU time on compiling the byte code into machine code.

There's a very good chance that the JIT compiler will inline a final method (as it can't be overwritten). And chances will be even better if that method just returns a constant value.

But it's my understanding - if the calling method is not a hot spot, then it will not be compiled and there'll be no inlining of the final methods.

(Information source in german language)

舂唻埖巳落 2024-11-26 11:33:19

或者,Soot 有望针对这种情况优化 Java 字节码。

Alternatively, Soot is expected to optimize Java bytecode for such case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文