Java是否通过一个标记为final的单个实现者的接口优化方法调用? [英] Does Java optimize method calls via an interface which has a single implementor marked as final?

查看:247
本文介绍了Java是否通过一个标记为final的单个实现者的接口优化方法调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个类的引用并在其上调用一个方法,并且该类或方法是最终的,我的理解是编译器或JVM将用更便宜的静态调度替换动态调度,因为它可以确定确切调用哪个版本。

If I have a reference to a class and invoke a method on it, and the class or the method is final, my understanding is that the compiler or the JVM would replace the dynamic dispatch with a cheaper static dispatch since it can determine exactly which version would be invoked.

但是,如果我有一个接口的引用,并且接口当前只有一个实现者,并且该实现者是最终的或者该方法在该实现者中是最终的,JVM能否在运行时计算出来并优化这些调用?

However, what if I have a reference to an interface, and the interface currently has only a single implementor, and that implementor is final or the method is final in that implementor, can the JVM figure that out at runtime and optimize these calls?

推荐答案

(插入Knuth引用)这里关于优化。)

(Insert Knuth quote here about optimization.)

参见 Wikis主页> OpenJDK的HotSpot内部结构> PerformanceTechniques



  • 方法通常是内联的。这增加了编译器
    优化的视野。

  • 静态,私有,最终和/或特殊调用很容易
    内联。

  • 如果类层次结构
    允许,则虚拟(和接口)调用通常会降级为特殊
    调用。如果进一步的类加载破坏了
    的东西,则依赖注册

  • 带有不平衡类型配置文件的虚拟(和接口)调用是
    编译的乐观检查历史上常见类型
    (或两种类型)的
    支持。

  • Methods are often inlined. This increases the compiler's "horizon" of optimization.
  • Static, private, final, and/or "special" invocations are easy to inline.
  • Virtual (and interface) invocations are often demoted to "special" invocations, if the class hierarchy permits it. A dependency is registered in case further class loading spoils things.
  • Virtual (and interface) invocations with a lopsided type profile are compiled with an optimistic check in favor of the historically common type (or two types).

内联中有一些有趣的链接。

There are some interesting links from Inlining.

这篇关于Java是否通过一个标记为final的单个实现者的接口优化方法调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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