8

I have read and heard a lot about how JIT compilers can make optimizations that are impossible for Native Code Compilers and that these optimizations can give huge performance boosts.

So I was wondering, what are the most important optimizations that, say, the .NET Framework or the JVM do that a native compiler cannot do? Also, how do these give huge performance boosts?

I don't know whether I've phrased this question properly, guess I may have a lot of explaining to do in the comments

1
  • @TimMedora: The link appears to talk about optimizations performed by the C# ahead-of-time compiler, not the CLR just-in-time compiler. Apr 13, 2012 at 8:22

2 Answers 2

4

I can give an example of one optimization. Suppose you have a function somewhere. (Think of this as C-like pseudocode.)

void function(MyClass x)
{
    x.doSomething();
    for (obj in x.getWidgets())
        obj.doSomethingElse();
}

This is suitably vague. Suppose, however, that you only have one concrete class in your entire image that inherits from MyClass: MyConcreteClass. In that case, the JIT can inline doSomething and getWidgets. If it knows about the type returned from getWidgets, then maybe it can inline doSomethingElse as well.

Assuming here that MyClass is not a final/sealed class, an ahead-of-time compiler cannot inline its method (it wouldn't know which functions to inline); for all the compiler knows, there are a hundred different implementations of MyClass.

However, a JIT can optimize for the current state of the image. It can install a check in the beginning of each call to function that makes sure that x is a MyConcreteClass, and then run the inlined version. If you dynamically load a module with another concrete class inheriting from MyClass, then the check will fail and the JIT will recompile the function to be generic.

These are the only kinds of optimizations available to JIT compilers that aren't available to ahead-of-time compilers: optimizations that make use of information about the dynamic state of the program and recompile the program accordingly.

Note that some ahead-of-time compilers are capable of doing tricks typically ascribed to JIT compilers. For example, interprocedural optimization (or global optimization) and profile-driven optimization. GCC and Clang can use both of those tricks, but most people leave them off since it requires extra (human) work to turn them on. JIT compilers can leave those options enabled without bothering end users.

Huge performance boost: I haven't heard of any huge performance boost in general from JIT compilers. C and C++ programs are still fast without JIT. And many people still prefer Fortran for numerical work (with good reason).

Footnote: I'm not sure about your terminology. Aren't most JITs also native code compilers? The other types of compiler besides JIT I would call "ahead of time" or AOT, or perhaps "static". (And then there's the incredibly fuzzy line between "compiled" and "interpreted".)

8
  • -1 for "These are the only kinds of optimizations..." JIT Compilers use profile guided optimization to improve callsite performance on hot program paths. (Both the MSFT CLR and Hotspot do this). That is not the "state" of the program but rather the dynamics of the program. JIT Compilers can use information about the machine on which they run to guide optimization. The MSFT CLR does this, and Hotspot does this. That has nothing to do with the program. JIT compilers can also use information state of the machine on which they run to guide optimization. Jul 16, 2012 at 10:38
  • I don't know that any current VMs do that last one, but it is certainly possible. Jul 16, 2012 at 10:40
  • @MichaelGraczyk: The reason I said "these are the only kind of optimizations" is because the optimizations you mention can be, and in fact are, supported by non-JIT compilers. GCC will do profile-driven optimization. The question is only about optimizations which ahead-of-time compilers can't do. Jul 16, 2012 at 14:30
  • I'd also consider it fair to consider ahead-of-time compilers to compile code for specific machines. After all, you see it a lot in the wild, at least on Linux. Jul 16, 2012 at 14:34
  • Some comments on this answer: 1. a C++ could inline doSomething and doSomethingElse and function if it had the source code available - 2. In c++ all classes are final/sealed by default - you need to explicitly declare a function as virtual, where as in Java all methods are virtual by default. 3. It is correct that with a c++ compiler, if doSomething and friends were virtual methods and not marked as final (per c++11), they would not be inlined, and in fact in order to get the address of the method, the compiler would have to look up in the virtual method table, (as java does without jit).
    – matiu
    Sep 16, 2012 at 3:44
1

Javascript is a much better example as it isn't as friendly towards compilers as the JVM or CLR are.

JIT compilers can generate concrete specialized representations for Javascript classes which is hard for static compilers as the classes can modified at any point during program execution. You can also speculatively inline the called functions based on the actual types (Dietrich Epp already explained that in his answer).

The videos at http://code.google.com/p/v8/ nicely explain those optimizations.

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.