tags:

views:

4867

answers:

5

What does a JIT compiler specifically do as opposed to a non-JIT compiler? Can someone give a succinct and easy to understand description?

+20  A: 

A JIT compiler runs after the program has started and compiles the code (usually bytecode or some kind of VM instructions) on the fly (or just-in-time, as it's called) into a form that's usually faster, typically the host CPU's native instruction set. A JIT has access to dynamic runtime information whereas a standard compiler doesn't and can make better optimizations like inlining functions that are used frequently.

This is in contrast to a traditional compiler that compiles all the code to machine language before the program is first run.

Mark Cidade
And in contrast to interpreted code, that begins running the bytecode or VM instructions immediately without delay, but will run the instructions slower than machine language.
Aaron
A JIT is often used with interpreted code to convert it to machine language but yes, purely interpreted code (without any JITting) is slow. Even Java bytecode without a JITter is really slow.
Mark Cidade
The target doesn't have to be machine code, though. JRuby has a JIT compiler which will compile Ruby sourcecode to Java bytecode after a couple of invocations. Then, after another couple of invocations, the JVM JIT compiler kicks in and compiles the bytecode to native code.
Jörg W Mittag
Good example: the TraceMonkey engine. A JIT compiler for javascript now included in Firefox. https://wiki.mozilla.org/JavaScript:TraceMonkey
Brian Clozel
It is worth noting that, as alluded to by Jörg, JIT is not necessarily invoked right away. Often, code will be interpreted until it is determined that it will be worth JITting. Since JITting can introduce delays, it may be faster to **NOT** JIT some code if it is rarely used and thus a fast response is more important than overall runtime.
Adam Jaskiewicz
A: 

You have code that is compliled into some IL (intermediate language). When you run your program, the computer doesn't understand this code. It only understands native code. So the JIT compiler compiles your IL into native code on the fly. It does this at the method level.

Charles Graham
+1  A: 

JIT stands for Just-in-Time which means that code gets compiled when it is needed, not before runtime.

This is beneficial because the compiler can generate code that is optimised for your particular machine. A static compiler, like your average C compiler, will compile all of the code on to executable code on the developer's machine. Hence the compiler will perform optimisations based on some assumptions. It can compile more slowly and do more optimisations because it is not slowing execution of the program for the user.

BrianLy
+8  A: 

In the beginning, a compiler was responsible for turning a high-level language (defined as higher level than assembler) into object code (machine instructions), which would then be linked (by a linker) into an executable.

At one point in the evolution of languages, compilers would compile a high-level language into pseudo-code, which would then be interpreted (by an interpreter) to run your program. This eliminated the object code and executables, and allowed these languages to be portable to multiple operating systems and hardware platforms. Pascal (which compiled to P-Code) was one of the first; Java and C# are more recent examples. Eventually the term P-Code was replaced with bytecode, since most of the pseudo-operations are a byte long.

A Just-In-Time (JIT) compiler is a feature of the run-time interpreter, that instead of interpreting bytecode every time a method is invoked, will compile the bytecode into the machine code instructions of the running machine, and then invoke this object code instead. Ideally the efficiency of running object code will overcome the inefficiency of recompiling the program every time it runs.

Craig Trader
Great explanation, shows the difference between interpretation and JITing.
name
A: 

A non-JIT compiler takes source code and transforms it into machine specific byte code at compile time. A JIT compiler takes machine agnostic byte code that was generated at compile time and transforms it into machine specific byte code at run time. The JIT compiler that Java uses is what allows a single binary to run on a multitude of platforms without modification.