How can Java inline over virtual function boundaries?

前端 未结 3 888
轮回少年
轮回少年 2021-02-20 10:03

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 in

相关标签:
3条回答
  • 2021-02-20 10:52

    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.

    0 讨论(0)
  • 2021-02-20 10:54

    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.

    0 讨论(0)
  • 2021-02-20 11:03

    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.

    0 讨论(0)
提交回复
热议问题