智能 JVM 和 JIT 微优化

发布于 2024-07-26 20:32:30 字数 302 浏览 5 评论 0原文

随着时间的推移,Sun 的 JVM 和 JIT 已经变得相当智能。 过去作为必要的微优化而成为常识的事情不再需要,因为它会为您处理好。

例如,过去的情况是,您应该将所有可能的类标记为 Final,以便 JVM 内联尽可能多的代码。 然而现在,JIT 根据运行时加载的类来知道您的类是否是最终的,如果您加载一个类以使原始类成为非最终类,它会取消内联方法并将其标记为最终的。

JVM 或 JIT 还可以为您执行哪些其他智能微优化?

编辑:我将其设为社区维基; 我想随着时间的推移收集这些。

Over time, Sun's JVM and JIT have gotten pretty smart. Things that used to be common knowledge as being a necessary micro-optimization are no longer needed, because it gets taken care of for you.

For example, it used to be the case that you should mark all possible classes as final, so the JVM inlines as much code as possible. However now, the JIT knows whether your class is final based on what classes get loaded in at runtime, and if you load a class to make the original one non-final-able, it un-inlines the methods and un-marks it as final.

What other smart micro-optimizations does the JVM or JIT do for you?

EDIT: I made this a community wiki; I'd like to collect these over time.

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

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

发布评论

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

评论(3

明明#如月 2024-08-02 20:32:31

Java 在内联方面更聪明,因为它可以

  • 内联仅在运行时可用的代码
    甚至是动态生成的。
  • 内联虚拟方法(一次最多两个)
  • 对内联方法及其内联到的方法执行逃逸分析。 (更难做到
    在 C++ 中)

Java is smarter at inlining as it can

  • inline code only available at runtime
    or even dynamically generated.
  • inline virtual methods (up to two at once)
  • perform escape analysis on inlined methods and the methods they were inlined to. (Much harder to do
    in C++)
白云不回头 2024-08-02 20:32:30

这令人印象深刻。 所有这些都是您在 C++ 中无法 做到的事情(当然与 Java 做到的程度相同)。 请记住,Java 的早期版本因没有这些功能而开始获得“缓慢”的声誉,随着时间的推移,我们不断地进行显着改进。 这仍然是一个很大的研究领域。

It's beyond impressive. All of these are things you can't do in C++ (certainly to the same extent Java does). Keep in mind that early versions of Java started the "slow" reputation by not having these things, and we keep improving significantly over time. This is still a big research area.

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