Java的优化:最后的方法返回一个常数(热点/ Dalvik的)优化? [英] Java optimizations: (Hotspot/Dalvik) Optimization of final method returning a constant?

查看:175
本文介绍了Java的优化:最后的方法返回一个常数(热点/ Dalvik的)优化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我如果有一个热点或Dalvik的是足够聪明,内联调用最后的方法返回一个常量(static final的)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.

这在一些code我正在设计的影响。

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

推荐答案

我想,答案是不,优化将不会发生,因为缺乏或$ P $的最后psence 关键词,至少在HotSpot虚拟机。但优化的可能发生,因为其他因素。

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.

下面就是作者Brian Goetz说,在这篇文章(抱歉长引号):

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

像关于Java的性能,错误地认为许多神话的   声明类或方法的最终结果更好的表现   广泛持有,但很少检查。按照这种说法的声明   方法或类为final意味着编译器可以内联方法   呼吁更积极,因为它知道在运行时,这是   一定要调用的方法,它是怎么回事的版本。但   这是不正确的。仅仅因为类X编译反对   最终的Y级并不意味着Y级相同版本会   在运行时加载。所以编译器不能内联这样的跨级   方法调用安全,最终还是不行。只有一个方法是私有的就可以了   编译器内联是自由,而在这种情况下,final关键字会   是多余的。

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.

在另一方面,在运行时环境和JIT编译器有更多的   什么类实际加载,并且可以使更多的信息   更好的优化决策比编译器即可。如果在运行时   环境知道没有类加载扩展Y,那么它   可以安全地内联调用的Y方法,与Y是否   最后的(只要它可以使无效这种JIT编译的code,如果   的Y子类以后加载)。因此,现实是,虽然最终的   可能是一个有用的提示,以哑巴运行时优化,这并不   执行任何全局依赖分析,它的使用实际上不   使很多编译时优化,并且不需要由一个   聪明的JIT执行运行时优化。

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.

还有一个很好的职位,为什么最后是不是最后的更多,至少在Java 5中

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

这篇关于Java的优化:最后的方法返回一个常数(热点/ Dalvik的)优化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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