JIT Compilation
Just-in-Time Compilation
- In Turkish
- JIT derleme
In short
JIT compilation turns the busiest parts of a program into machine code while it runs, combining an interpreter's quick start with compiled speed.
What is JIT compilation?
A just-in-time (JIT) compiler works inside a running program. The program starts in an interpreter, which can run code right away, while the runtime counts how often each function and loop runs. Code that turns out to be hot, meaning it runs many times, is compiled into native machine code for the processor, and later calls run that faster version instead.
Because it compiles while the program runs, a JIT compiler can optimize for what actually happens: if a function is only ever called with numbers, it can produce code made for numbers. If that guess later proves wrong, the runtime throws the optimized code away and falls back to slower code, which is called deoptimization. V8 in Chrome and Node.js, the Java HotSpot virtual machine and .NET all work this way, usually with several tiers, from quick to highly optimized.
JIT compilation sits between the two classic approaches. Ahead-of-time (AOT) compilation, as in C, Go and Rust, translates everything before the program runs; a plain interpreter translates nothing. JIT pays in warm-up time, since code is slow until it gets compiled, and in memory, which is why short-lived programs, and platforms that forbid generating code at runtime such as iOS apps, use AOT instead. Python added an experimental JIT compiler in version 3.13.
Key takeaways
- A JIT compiler turns frequently run code into machine code while the program runs.
- Programs start in an interpreter, and hot code is compiled once it proves worth it.
- What it sees at runtime lets a JIT specialize code, and deoptimize when a guess fails.
- The cost is warm-up time and memory; ahead-of-time compilation avoids both.
Example
// Run with: node --trace-opt hot.js
function add(a, b) {
return a + b;
}
let sum = 0;
for (let i = 0; i < 1e6; i++) {
sum = add(sum, i); // a million calls make add "hot"
}
console.log(sum); // 499999500000
// Among V8's log lines, add is compiled by Maglev, then by TurboFan:
// [marking … <JSFunction add …> for optimization to MAGLEV, …, reason: hot and stable]
// [marking … <JSFunction add …> for optimization to TURBOFAN_JS, …, reason: hot and stable]Readers ask
Is JIT compilation faster than ahead-of-time compilation?
Not in general. Ahead-of-time compiled programs start fast and need no compiler while they run. A JIT starts slower but can use what it sees at runtime, so long-running programs such as servers can match, and sometimes beat, ahead-of-time code. Which wins depends on the workload.
Does Python have a JIT compiler?
The standard CPython added an experimental JIT compiler in version 3.13, switched off by default. PyPy, an alternative Python implementation, has used a JIT for years and often runs pure Python code several times faster.
See also
- CompilerProgramming Fundamentals, p. 11A compiler is a program that translates source code written in a programming language into a lower-level form, such as machine code, that a computer can run.
- InterpreterProgramming Fundamentals, p. 30An interpreter is a program that runs source code directly, step by step, instead of first translating the whole program into a separate executable file.
- JavaScriptWeb Development, p. 24JavaScript is the programming language that runs in web browsers to make pages interactive, and it also runs on servers through runtimes like Node.js.
- JavaProgramming Languages, p. 15Java is a statically typed, object-oriented programming language that compiles to bytecode for the Java Virtual Machine, so programs run on many platforms.
- Virtual MachineDevOps & Cloud, p. 53A virtual machine is a software-based computer that runs its own operating system on shared physical hardware, isolated from other machines on the same host.
- Garbage CollectionProgramming Fundamentals, p. 23Garbage collection is automatic memory management in which the language runtime finds data a program can no longer use and frees that memory for reuse.
- JVMProgramming Languages, p. 17The JVM runs Java bytecode, so the same compiled program works on any operating system; Kotlin, Scala and Clojure compile to that bytecode too.
Sources
Spotted a mistake or something missing on this page?Suggest an edit