How can Java inline over virtual function boundaries?

不打扰是莪最后的温柔 提交于 2019-12-21 08:23:09

问题


I'm reading up some material on whether Java can be faster than C++, and came across the following quote:

Java can be faster than C++ because JITs can inline over virtual function boundaries.

Why Java Will Always Be Slower than C++ (wayback link)

What does this mean? Does it mean that the JIT can inline virtual function calls (because presumably it has access to run time information) whereas C++ must call the function through its vtable?


回答1:


The answer to your question is Yes: that is what the quoted text means.

The JIT will analyse all of the loaded classes. If it can determine that there is only one possible method that can be called at any given point, it can avoid the dispatching and (if appropriate) inline the method body.

By contrast, a C++ compiler does not know all of the possible subtypes, and therefore cannot determine if this optimization can be done for a (virtual) method. (And by the time the linker runs, it is too late ...)

Other answers have said that you can do this optimization by hand in C++ ... but that assumes that you (the programmer) can do the analysis yourself, and change methods from virtual to non-virtual. But if you get it wrong, you've got a bug to track down.

By the way, we can assume that this optimization is worthwhile for the average Java application. If it was not, the JIT compiler guys would not have implement it. After all, a worthless optimization is only going to make Java applications start more slowly.




回答2:


Since compilation of Java bytecode into machine code is deferred until runtime, it is possible for JVMs to perform profile-guided optimization and other optimizations that require information not available until code is running. This may even include "deoptimization", where a previously made optimization is revoked so that other optimizations can occur.

More information about this can be found under adaptive optimization on Wikipedia, which includes optimizations related to inlining.




回答3:


For what its worth, Java, C++, Assembly will provide relatively the same performance.

Yes, better performance can be acheived with handoptimzed C++, C, or Asm ... however, for the majorty of applications out there (try everything outside of serious graphics apps), that is not the bottleneck, -and- the lower cost of implementation makes up for any perceived lower performance.



来源:https://stackoverflow.com/questions/1719581/how-can-java-inline-over-virtual-function-boundaries

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