Smart JVM and JIT Micro-Optimizations

◇◆丶佛笑我妖孽 提交于 2019-12-03 13:34:40

问题


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.


回答1:


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.

  • Efficient interface dispatch.
  • Inlining and direct dispatch of virtual method calls.
  • Very fast object allocation with bump pointers (slide 19 or so) and escape analysis.



回答2:


Oracle has a wiki on Performance techniques used in the Hotspot JVM.




回答3:


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++)


来源:https://stackoverflow.com/questions/1144624/smart-jvm-and-jit-micro-optimizations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!