Tuple Logo
Just-in-time compilation (JIT)

SHARE

Just-in-time compilation (JIT)

What is just-in-time compilation?

Just-in-time compilation (JIT) is a technique where code is only turned into machine code while a program is running. It doesn't happen in advance, but at the moment that code is actually needed. The component that does this is called a JIT compiler.

A processor only understands machine code. Whatever a developer writes has to be translated at some point, and there are three ways to do it. Comparing them to translators makes the difference easy to see.

Imagine you want an English book read aloud to a French-speaking audience. You have three options:

That last option is exactly what JIT does. The program starts quickly, just as it would with an interpreter. Code that runs often soon becomes as fast as compiled code. Sometimes it's even faster, because the JIT compiler can see how the program is actually being used.

Where the name comes from

The name is borrowed from just in time manufacturing, the Toyota method of only making what's needed, when it's needed. The term was popularised by Java, with James Gosling using it from 1993. The idea fits neatly. You only compile when it's needed, and only what's needed.

The technique itself is older than the name. Software systems have been using JIT techniques since the 1960s. Early examples include LISP and tools for regular expressions. Later, the Smalltalk and Self languages took the idea further. What was learnt there found its way into Java's HotSpot JVM, which brought JIT compilation to a mass audience.

Compiler, interpreter or JIT?

There are three ways to translate code into machine code. Each has its own strengths and weaknesses.

Ahead-of-time compilation

With ahead-of-time compilation (AOT), a compiler turns all the code into machine code before the program runs. You end up with a file the processor can execute directly. Languages such as C, C++, Rust and Go work this way.

The upside is that the program starts quickly and runs at full speed from the outset. The downside is that the machine code is built for one type of processor and operating system. To run the program anywhere else, you have to compile it again.

Interpretation

An interpreter reads the code and runs it line by line. There's no separate compile step, so you can get going straight away. The same code runs anywhere the interpreter runs.

The trade-off is speed. An interpreter translates the same code over and over, even if it runs a thousand times in a row. That adds up.

JIT: the middle ground

JIT combines the two. Source code is often converted in advance into bytecode, an intermediate form that isn't tied to any one processor. While the program runs, an interpreter executes that bytecode. Code that runs often is handed to the JIT compiler and turned into machine code after all.

That way, you keep the flexibility of an interpreter. At the same time, you get the speed of compiled code for the parts that really matter.

This table puts the three side by side:

AOT compilation

Interpretation

JIT compilation

When it's translated

In advance, all at once

While running, over and over

While running, once per piece of code

Speed right after start-up

High

Low

Low, then rises

Speed after a while

High

Low

High

Runs on any platform

No, needs recompiling

Yes

Yes

Memory use

Low

Low

Higher

Examples

C, C++, Rust, Go

Shell scripts, standard Python

Java, C#, JavaScript

How does a JIT compiler work?

Every JIT compiler works slightly differently, but the basic process is the same.

Diagram of a JIT compiler. Source code becomes bytecode, which an interpreter runs. Code that rarely runs stays interpreted. Code that runs often goes to the JIT compiler, which turns it into machine code. That machine code is stored in the code cache and run directly by the processor.

From source code to bytecode

With languages such as Java and C#, the process starts before the program even runs. A standard compiler turns the source code into bytecode. In Java this is called Java bytecode. In .NET it's called Intermediate Language (IL). That bytecode runs on any machine with the right runtime, such as the Java Virtual Machine (JVM) or the .NET runtime.

JavaScript skips this step. The browser receives the source code itself and builds its own bytecode internally.

Spotting hot code

When the program starts, an interpreter runs the bytecode. Meanwhile, the runtime keeps track of which pieces of code run most often. This is called profiling. A function called thousands of times or a loop that keeps going round counts as hot code.

Only hot code is worth compiling. Code that runs just once is better left to the interpreter, because compiling takes time too.

Compiling and optimising

Once code is hot, the JIT compiler turns it into machine code for the processor the program is running on at that moment. At the same time, it optimises the code.

This is where JIT really shines. An AOT compiler has to guess how the program will be used. A JIT compiler can see it. It knows which paths through the code are actually taken, which data types are passing through and which processor is in the machine. It tailors the machine code to match.

Sometimes an assumption stops holding true, for example when a different data type suddenly turns up. The runtime then throws away the optimised code. The code goes back to being interpreted and is recompiled later. This is known as deoptimisation.

Reusing compiled code

The machine code produced by the JIT compiler is stored in the code cache. When the same function is called again, the runtime picks up the cached version straight away. Nothing needs translating.

Tiered compilation

Optimising takes time. The better the optimisation, the longer compilation takes. That's why many runtimes work with several levels, an approach known as tiered compilation.

The JVM is a well-known example. It has a client compiler (C1) and a server compiler (C2). C1 compiles quickly with simple optimisations, so code speeds up early on. If a piece of code keeps running heavily after that, C2 takes over. C2 takes longer but produces much faster machine code. JavaScript engines in browsers use a similar multi-tier set-up.

Where you'll come across JIT compilation

Chances are you've already used code today that ran through a JIT compiler. These are the most common places:

Benefits of JIT compilation

There's a reason JIT compilation is so widely used. These are the main benefits:

Drawbacks of JIT compilation

Those benefits come with a few clear drawbacks:

JIT or AOT: which should you choose?

The right approach mainly depends on how long your program runs.

If start-up time matters most, AOT is usually the better fit. Think of these use cases:

If your program runs for a long time, JIT is often the better choice. Think of web servers, APIs and back-end systems that run for days or weeks at a stretch. By the time everything is running at full speed, the warm-up is long forgotten. On top of that, the JIT compiler keeps optimising based on how the system is actually used.

You don't always have to pick one or the other. Java and .NET both offer ways to compile code to machine code in advance, for example to speed up start-up in the cloud. Some runtimes can also save data from a previous run to shorten the warm-up. So focus on what your application needs, not just on what a language does by default.

Compiling at the moment it pays off

JIT compilation only turns code into machine code once the program is running. That makes for a smart division of labour. Code that runs once is interpreted. Code that keeps coming back is compiled and optimised based on how the program is really used.

The result is fast code that runs anywhere. The price is a longer warm-up, higher memory use and extra attention to security. If your software runs for a long time, JIT is often the best choice. If every millisecond at start-up counts, AOT is the better fit.

The name says it all, really. Just like just in time on the factory floor, it's about doing the right work at the right moment.

Frequently Asked Questions
Is Java compiled or interpreted?

Both. Java code is compiled in advance into bytecode. While the program runs, the JVM first executes that bytecode with an interpreter. Code that runs often is then turned into machine code by the JIT compiler.


Does JavaScript use a JIT compiler?

Yes. Modern JavaScript engines use JIT compilation, such as V8 in Chrome and Node.js and SpiderMonkey in Firefox. Without JIT, demanding web applications would be far slower.


What's the difference between JIT and AOT?

AOT compiles all the code before the program runs. JIT compiles while the program runs, and only the code that's used often. AOT starts up faster. JIT can optimise better based on real-world use.


Why is a JIT-compiled program slower at start-up?

At start-up, all the code still runs through the interpreter. The runtime first has to find out which code is hot and compile it. Only then does the program run at full speed. This is known as the warm-up.


Is a JIT compiler the same as an interpreter?

No. An interpreter translates code every time it runs. A JIT compiler turns code into machine code once and reuses it after that. In practice, the two often work together: the interpreter gets things going and the JIT compiler takes over the work that keeps coming back.


What does JIT compilation have to do with just in time manufacturing?

The name is borrowed from it. As with Toyota's method, it's about only making what's needed, when it's needed. In the factory, that means parts. In JIT compilation, it means machine code.


Articles you might enjoy

Piqued your interest?

We'd love to tell you more.

Contact us
Tuple Logo
Veenendaal (HQ)
De Smalle Zijde 3-05, 3903 LL Veenendaal
info@tuple.nl‭+31 318 24 01 64‬
Quick Links
Customer Stories