11

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?

3 Answers 3

11

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.

1
  • A benefit is that getters and setters can be inlined meaning full abstraction protection (coding to interfaces and all) without automatically paying a penalty everytime. Nov 12, 2009 at 8:44
4

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.

2
  • I understand there exist C++ compilers that can use profile information to determine whether to do these optimisations. The real problem with C++ is that the standard link model means the caller and callee are not necessarily compiled together. Nov 12, 2009 at 4:30
  • Yeah, new gcc can do this kind of link-time optimization (LTO).
    – Adam Goode
    Nov 12, 2009 at 5:11
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.