views:

548

answers:

5

I've been thinking about it lately, and it seems to me that most advantages given to JIT compilation should more or less be attributed to the intermediate format instead, and that jitting in itself is not much of a good way to generate code.

So these are the main pro-JIT compilation arguments I usually hear:

  1. Just-in-time compilation allows for greater portability. Isn't that attributable to the intermediate format? I mean, nothing keeps you from compiling your virtual bytecode into native bytecode once you've got it on your machine. Portability is an issue in the 'distribution' phase, not during the 'running' phase.
  2. Okay, then what about generating code at runtime? Well, the same applies. Nothing keeps you from integrating a just-in-time compiler for a real just-in-time need into your native program.
  3. But the runtime compiles it to native code just once anyways, and stores the resulting executable in some sort of cache somewhere on your hard drive. Yeah, sure. But it's optimized your program under time constraints, and it's not making it better from there on. See the next paragraph.

It's not like ahead-of-time compilation had no advantages either. Just-in-time compilation has time constraints: you can't keep the end user waiting forever while your program launches, so it has a tradeoff to do somewhere. Most of the time they just optimize less. A friend of mine had profiling evidence that inlining functions and unrolling loops "manually" (obfuscating source code in the process) had a positive impact on performance on his C# number-crunching program; doing the same on my side, with my C program filling the same task, yielded no positive results, and I believe this is due to the extensive transformations my compiler was allowed to make.

And yet we're surrounded by jitted programs. C# and Java are everywhere, Python scripts can compile to some sort of bytecode, and I'm sure a whole bunch of other programming languages do the same. There must be a good reason that I'm missing. So what makes just-in-time compilation so superior to ahead-of-time compilation?


EDIT To clear some confusion, maybe it would be important to state that I'm all for an intermediate representation of executables. This has a lot of advantages (and really, most arguments for just-in-time compilation are actually arguments for an intermediate representation). My question is about how they should be compiled to native code.

Most runtimes (or compilers for that matter) will prefer to either compile them just-in-time or ahead-of-time. As ahead-of-time compilation looks like a better alternative to me because the compiler has more time to perform optimizations, I'm wondering why Microsoft, Sun and all the others are going the other way around. I'm kind of dubious about profiling-related optimizations, as my experience with just-in-time compiled programs displayed poor basic optimizations.

I used an example with C code only because I needed an example of ahead-of-time compilation versus just-in-time compilation. The fact that C code wasn't emitted to an intermediate representation is irrelevant to the situation, as I just needed to show that ahead-of-time compilation can yield better immediate results.

+3  A: 
  1. Greater portability: The deliverable (byte-code) stays portable

  2. At the same time, more platform-specific: Because the JIT-compilation takes place on the same system that the code runs, it can be very, very fine-tuned for that particular system. If you do ahead-of-time compilation (and still want to ship the same package to everyone), you have to compromise.

  3. Improvements in compiler technology can have an impact on existing programs. A better C compiler does not help you at all with programs already deployed. A better JIT-compiler will improve the performance of existing programs. The Java code you wrote ten years ago will run faster today.

  4. Adapting to run-time metrics. A JIT-compiler can not only look at the code and the target system, but also at how the code is used. It can instrument the running code, and make decisions about how to optimize according to, for example, what values the method parameters usually happen to have.

You are right that JIT adds to start-up cost, and so there is a time-constraint for it, whereas ahead-of-time compilation can take all the time that it wants. This makes it more appropriate for server-type applications, where start-up time is not so important and a "warm-up phase" before the code gets really fast is acceptable.

I suppose it would be possible to store the result of a JIT compilation somewhere, so that it could be re-used the next time. That would give you "ahead-of-time" compilation for the second program run. Maybe the clever folks at Sun and Microsoft are of the opinion that a fresh JIT is already good enough and the extra complexity is not worth the trouble.

Thilo
For your first and second points, what if I just took the bytecode and compiled it ahead-of-time on the end user's machine, for his specific system, during the installation process? This is why I say these advantages are relative to the intermediate format and not to just-in-time compilation. As of your third point, yeah, I guess it's true. However, if my native code runs twice as fast today as my jitted code runs now, I'm not quite interested in its performances 10 years from now.
zneak
Then you'd have to bundle the ahead-of-time compiler with your program. Which breaks portability, because your compiler needs to be native-code.
Anon.
@Anon: it should be stated that the requirements for just-in-time compilation and ahead-of-time compilation are the same. You can't run .NET programs without the .NET framework, so virtual machines already broke portability there.
zneak
So what you are asking is: "Why do we not have an installation phase that turns byte-code into machine code?" I suppose the difference to JIT is that dynamic adaptation would go away. On the other hand, no time constraints. Maybe it is not worth the extra complexity (the installation phase). Don't know.
Thilo
@Anon: No, it would be using the same (or very similar) compiler that the JVM or CLR is using now.
Thilo
+2  A: 

Simple logic tell us that compiling huge MS Office size program even from byte-codes will simply take too much time. You'll end up with huge starting time and that will scare anyone off your product. Sure, you can precompile during installation but this also has consequences.

Another reason is that not all parts of application will be used. JIT will compile only those parts that user care about, leaving potentially 80% of code untouched, saving time and memory.

And finally, JIT compilation can apply optimizations that normal compilators can't. Like inlining virtual methods or parts of the methods with trace trees. Which, in theory, can make them faster.

vava
I don't get your first argument. Is it supposed to be against just-in-time compilation or against ahead-of-time compilation? As of the memory saves and time, the time wasted in compiling untouched program parts is wasted only once, so that isn't much of an arugment IMO. And it's not saving much memory if the whole virtual bytecode is loaded in memory anyways.
zneak
+1. There is a point is saying "if you do not run a subroutine over and over again, why do you need it to be fast (and spend time to make it fast)?". That is assuming, of course, that the JIT will make it really fast if you run it really frequently. This may not happen at the first round of compilation, but eventually...
Thilo
+2  A: 
  1. Better reflection support. This could be done in principle in an ahead-of-time compiled program, but it almost never seems to happen in practice.

  2. Optimizations that can often only be figured out by observing the program dynamically. For example, inlining virtual functions, escape analysis to turn stack allocations into heap allocations, and lock coarsening.

dsimcha
It is a fact that your programs are compiled into native code before running. Therefore, reflection is obviously possible even for native programs, and that's more a matter of runtime than a matter of how you compile. As of escape analysis, I think it would be a terrible idea to base that on runtime observations rather than static code analysis.
zneak
A: 

Maybe it has to do with the modern approach to programming. You know, many years ago you would write your program on a sheet of paper, some other people would transform it into a stack of punched cards and feed into THE computer, and tomorrow morning you would get a crash dump on a roll of paper weighing half a pound. All that forced you to think a lot before writing the first line of code.

Those days are long gone. When using a scripting language such as PHP or JavaScript, you can test any change immediately. That's not the case with Java, though appservers give you hot deployment. So it is just very handy that Java programs can be compiled fast, as bytecode compilers are pretty straightforward.

But, there is no such thing as JIT-only languages. Ahead-of-time compilers have been available for Java for quite some time, and more recently Mono introduced it to CLR. In fact, MonoTouch is possible at all because of AOT compilation, as non-native apps are prohibited in Apple's app store.

Dmitry Leskov
Yes, of course most jitted languages also have ahead-of-time compilers. In fact, Mono didn't do anything new, as Microsoft had an utility called ngen (http://msdn.microsoft.com/en-us/library/6t9t5wcf(VS.80).aspx) that generates native code from a CLR executable for quite some time. The fact, however, is that most languages use their just-in-time variants whenever they can, and it is this behavior I'm trying to understand.
zneak
+1  A: 

The ngen tool page spilled the beans (or at least provided a good comparison of native images versus jitted images). Here is a list of advantages of executables that are compiled ahead-of-time:

  1. Native images load faster because they don't have much startup activities, and require a static amount of fewer memory (the memory required by the jitter);
  2. Native images can share library code, while jitted images cannot.

And here is a list of advantages of just-in-time compiled executables:

  1. Native images are larger than their bytecode counterpart;
  2. Native images must be regenerated whenever the original assembly or one of its dependencies is modified (which makes sense, since it could screw up virtual tables and stuff like that).

And the general considerations of Microsoft on the matter:

  1. Large applications generally benefit from being compiled ahead-of-time, and small ones generally don't;
  2. From what I understand, binding issues with native images can make long-running applications perform better under just-in-time compilation.

The need to regenerate an image that is ahead-of-time compiled every time one of its components is modified can indeed be a problem as it complexifies versioning. That's actually what's the most convincing in my opinion.

zneak