Algorithm Search⌘K

Conceptintro

Just-in-time compilation

Also called JIT compilation, dynamic compilation.

Compiles code while the program runs, so the compiler can use the actual types, values and profile seen so far.

Just-in-time (JIT) compilation is the translation of program code into machine code while the program runs, after it has started. A JIT system usually starts the program in an interpreter, finds the parts that run often, and compiles only those parts, at the moment they are needed. Because it compiles late, the compiler can use facts that a compiler before the run does not have: the types and values that actually occur, the paths that are actually taken, and the exact processor. This page gives the general idea. Adaptive optimization decides when to compile and at which level, tracing JIT compilation and meta-tracing compile hot paths instead of whole methods, a self-optimizing AST interpreter gets its compiler from the interpreter by partial evaluation, and copy-and-patch compilation is a fast way to emit the machine code.

In plain words

A program can run in two ways. An interpreter reads the program one step at a time and does what each step says. It starts at once, but each step costs extra work for the reading. A compiler first translates the whole program to machine code. That takes time before the start, but then each step is fast.

A just-in-time compiler mixes the two. The program starts in the interpreter, and the system counts how often each part runs. When a part has run many times, the system translates that part to machine code and uses the fast code from then on. Parts that run only once are never compiled, so no time is lost on them. Because the program is already running, the compiler can also see what the program really does, and make code that is fast for that case.

The idea

The parts of a JIT system

An interpreter is a program that runs another program from a data representation of it, such as an abstract syntax tree or bytecode, a compact list of virtual instructions (see threaded code for the ways to dispatch them). The interpreter keeps the state of the running program in its own data: one frame for each active call, as in an activation record, with the local variables and a virtual program counter that names the next instruction. A JIT system adds these parts to the interpreter:

  • a profiler: counters at method entries and at loop back edges, samples of the program counter, and the receiver types that inline caches record;
  • one or more compilers, from a fast baseline compiler to a slow optimizing one;
  • a code cache: a map from each compiled unit to its machine code and its entry points;
  • the glue between the two worlds: entry from the interpreter into compiled code, exit from compiled code back to the interpreter (deoptimization), and the switch of a loop that is already running to new code (on-stack replacement).

The unit of compilation can be a method, a loop, a straight path through the code called a trace, or a region of basic blocks. The rest of this section uses methods, but the same ideas hold for the other units.

The life of a unit

Each unit is in one state at a time. The system moves it between the states on these events:

state event next state
interpreted its counter reaches the threshold queued for compilation
queued the compiler finishes compiled at level ii
compiled at level ii its counter at this level reaches the next threshold queued for level i+1i + 1
compiled an assumption of the code fails interpreted (the code is invalid)
compiled the code cache is full and the unit is evicted interpreted

A call then goes through a small dispatch step:

CALL(u, args)
L1  if the code cache has code for u then return RUN(code[u], args)
L2  count[u] ← count[u] + 1
L3  if count[u] ≥ threshold then put u on the compile queue
L4  return INTERPRET(u, args)

Line L3 only queues the unit. Many systems compile on a background thread and keep interpreting until the code is ready, so the program does not stop for the compiler. The next call after the code is installed takes line L1.

What the compiled code must do

Write I(u,σ)I(u, \sigma) for the outcome of interpreting unit uu from the interpreter state σ\sigma: a returned value with the final heap, an error, or no end. Compiled code KK for uu is correct when, for each state σ\sigma from which uu can be entered, running KK from the machine state that encodes σ\sigma has the outcome I(u,σ)I(u, \sigma). Two features make this harder than in a compiler that runs before the program.

  • Speculation. Optimized code often holds guards: tests of an assumption, such as "this value is a small integer" or "the receiver has the shape seen before". The code needs to be correct only on the states that pass its guards. A failing guard is an exit point, and at each exit point pp the compiler records a map δp\delta_p from the machine state to interpreter frames. The invariant is: if the run of KK from the encoding of σ\sigma reaches pp in machine state ss, then interpreting uu from σ\sigma reaches the program point of pp in the state δp(s)\delta_p(s). Then the interpreter can go on from δp(s)\delta_p(s) and give the outcome I(u,σ)I(u, \sigma). This is the contract of deoptimization, and on-stack replacement needs the same map in the other direction.
  • Facts about the whole program that can change. A JIT compiler can use a fact such as "class C has no subclass yet" from class hierarchy analysis to devirtualize a call. When a new class is loaded, the fact becomes false, and the system must invalidate each piece of code that depends on it before that code runs again. So the system keeps a list of dependencies from facts to code.

What a JIT compiler knows

A compiler that runs before the program must make code that is correct and reasonably fast for each possible input. A JIT compiler sees one run. It can use:

  • the types that occur, from inline caches and type feedback;
  • values that do not change in this run, such as a configuration flag, a regular expression, or the loaded class hierarchy;
  • the paths that are taken, for block layout and for function inlining decisions. Profile-guided optimization uses the same kind of profile, but from a separate training run;
  • the exact processor, so it can use its vector instructions.

The price is that compile time is now part of the run time. A JIT compiler must be fast, it uses memory for the code cache, and the program is slow while it warms up. It also needs memory that it can write and then execute. Many systems keep each page either writable or executable, never both at the same time, and switch a page when they install code.

When to compile, as a rent-or-buy problem

Take one unit that will run nn times, where nn is not known in advance. One run costs aa in the interpreter and b<ab < a in compiled code, and the compilation costs CC once. If nn were known, the best cost would be

OPT(n)=min⁡(na,  C+nb).\mathrm{OPT}(n) = \min(n a,\; C + n b).

The break-even rule interprets the first k=⌈C/(a−b)⌉k = \lceil C/(a - b) \rceil runs and compiles before run k+1k + 1. Its cost is ALG(n)=na\mathrm{ALG}(n) = na for n≤kn \le k, and ka+C+(n−k)bka + C + (n - k) b for n>kn > k.

Claim. For each n≥0n \ge 0, ALG(n)<2 OPT(n)+(a−b)\mathrm{ALG}(n) < 2\,\mathrm{OPT}(n) + (a - b).

Proof. Let d=a−b>0d = a - b > 0. By the choice of kk, C≤kd<C+dC \le kd < C + d. Case n≤kn \le k: if OPT(n)=na\mathrm{OPT}(n) = na, then ALG(n)=OPT(n)\mathrm{ALG}(n) = \mathrm{OPT}(n). Otherwise OPT(n)=C+nb\mathrm{OPT}(n) = C + nb, and ALG(n)=nb+nd≤nb+kd<nb+C+d=OPT(n)+d\mathrm{ALG}(n) = nb + nd \le nb + kd < nb + C + d = \mathrm{OPT}(n) + d. Case n>kn > k: then nd>kd≥Cnd > kd \ge C, so OPT(n)=C+nb\mathrm{OPT}(n) = C + nb. And ALG(n)=C+nb+kd<2C+nb+d≤2 OPT(n)+d\mathrm{ALG}(n) = C + nb + kd < 2C + nb + d \le 2\,\mathrm{OPT}(n) + d. □\square

This is the ski rental problem, with the saving a−ba - b per run in place of the rent and CC in place of the price. No rule that does not know nn can do better than a ratio of about 2 in the worst case, by the lower bound for ski rental. Real systems do not know aa, bb and CC exactly, so their thresholds are tuned by hand. The SELF system also let its counters decay over time, so that a high rate of calls, not a high total, triggers a compilation (Hölzle 1994, Section 5.3). The model is a guide to the shape of the rule. It says nothing about the speed of a real system.

Examples

Break-even numbers

Take a=10a = 10, b=1b = 1 and C=500C = 500. Then k=⌈500/9⌉=56k = \lceil 500/9 \rceil = 56.

runs nn cost of the break-even rule best cost ratio
10 100 100 1.000
56 560 556 1.007
57 1061 557 1.905
100 1104 600 1.840
1000 2004 1500 1.336

The worst case is n=k+1n = k + 1: the rule pays for 56 interpreted runs and for the compilation, and then the unit runs only once more. The bound of the claim is 2⋅557+9=11232 \cdot 557 + 9 = 1123 there. For large nn the ratio goes to 1, because the cost of the compilation and of the first runs is spread over many runs.

Specializing on the types seen at run time

In a dynamic language, the function add(x, y) = x + y must work for integers, floating-point numbers and strings. The interpreter tests the types of both operands on every call. Suppose that the first 1000 calls all have small integers. A JIT compiler can emit this code:

add_compiled(x, y):
  guard x is a small integer    else exit to the interpreter at add, pc 0, with x and y
  guard y is a small integer    else exit to the interpreter at add, pc 0, with x and y
  r = x + y
  guard r did not overflow      else exit to the interpreter at add, pc 0, with x and y
  return r

Call 1001 is add("ab", "c"). The first guard fails. The exit builds an interpreter frame for add at the start, with x = "ab" and y = "c", and the interpreter returns "abc". The program cannot tell that compiled code was ever involved. That is the invariant of the section above, at an exit point where no work was done yet.

Thompson's regular expression compiler

In 1968, Thompson's search program compiled a regular expression into IBM 7094 machine code at the time of the search, and then ran that code on the text. The code simulates the nondeterministic automaton of the expression, which is the Thompson construction. Aycock lists it among the early JIT systems. The input to the compiler, the regular expression, is known only when the program runs, so compilation must happen at run time.

Smalltalk-80 by Deutsch and Schiffman

Deutsch and Schiffman's Smalltalk-80 system of 1984 translated the bytecode of a method into native code the first time it ran, and kept the native code in a cache of fixed size. When the cache was full, it dropped code, and translated a method again when it was needed. So the native code was a cache of the bytecode, not a replacement for it. The same system introduced inline caching for message sends.

Where it is used

  • Java virtual machines. They start with an interpreter or a baseline compiler and recompile hot methods with optimizing compilers, which is adaptive optimization. Arnold and others survey these systems.
  • JavaScript engines. They have several tiers, from an interpreter to an optimizing compiler, and depend on inline caches, hidden classes, type feedback and deoptimization.
  • Dynamic language runtimes. PyPy gets a tracing JIT compiler from an interpreter by meta-tracing. The Truffle framework uses a self-optimizing AST interpreter and partial evaluation. CPython 3.13 added an experimental JIT compiler that uses copy-and-patch compilation.
  • Binary translation and dynamic optimization. A system such as Dynamo reads machine code, finds hot paths, and writes optimized copies of them into a code cache, which is tracing JIT compilation without a source language.
  • Small languages inside large programs. Regular expression engines, database query engines, packet filters in operating system kernels and graphics shaders are often compiled at run time, because the program text arrives at run time.

References

  1. John Aycock, A brief history of just-in-time, ACM Computing Surveys 35(2), 2003, 97-113, the definition of JIT compilation as any translation done after the program starts, the survey of systems from 1960 to 2000, and the classification of JIT systems
  2. Keith D. Cooper and Linda Torczon, Engineering a Compiler, third edition, Morgan Kaufmann, 2022, Chapter 14 (Runtime Optimization), Section 14.2 on the execution model, compilation triggers and granularity, and Section 14.5 on levels, on-stack replacement and code cache management
  3. L. Peter Deutsch and Allan M. Schiffman, Efficient implementation of the Smalltalk-80 system, POPL 1984, 297-302, dynamic translation of bytecode methods to native code on first use, the bounded code cache, and the inline cache
  4. Matthew Arnold, Stephen J. Fink, David Grove, Michael Hind and Peter F. Sweeney, A survey of adaptive optimization in virtual machines, Proceedings of the IEEE 93(2), 2005, 449-466, the survey of selective optimization, profiling and feedback-directed optimization in virtual machines
  5. Urs Hölzle, Adaptive optimization for SELF: reconciling high performance with exploratory programming, PhD thesis, Stanford University, 1994 (report STAN-CS-TR-94-1520), Section 5.2 (adaptive recompilation) and Section 5.3 (invocation counters with exponential decay)
  6. Ken Thompson, Regular expression search algorithm, Communications of the ACM 11(6), 1968, 419-422, the compilation of a regular expression to IBM 7094 machine code at search time

Where this leads

About this page

The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.

LadderLevelWhat the next level needs
Human proofH0 no proof
  • add a proof in proofs/ (06.5)
LeanL0 no Lean part
  • name a Lean module in the lean field (07)
VisualV0 none
  • add viz/scene.ts or a figure (10)
WritingW4 complete
  • a review of the text: no review record reviews/7Z715T/text.yaml