JIT COMPILATION

Compile at runtime, optimize what matters

L215 MINCOMPILERS
PREDICT

add(a, b) was compiled for (int, int) with no guard, nothing that checks the argument types at run time. Strings are heap objects. What does add('ab', 'cd') return?

A JIT starts every function in an interpreter that counts calls and records argument types. Once a function is hot, the JIT compiles it for the types it has seen. Int-only code is fast because it adds machine words and skips every type check.

That code is a bet. A guard checks the real types where the specialised code relies on them; here that is right after the counter increment. If they differ, the JIT deoptimises: it rebuilds the interpreter frame at the current pc, drops the compiled code, and lets the interpreter finish. Later it recompiles with the broader types.

▶ SIMULATION
INTERPRETER
calls since last compile: 0 / 3
profile: a ∈ —, b ∈ —
deopt frame: —
CALLS (last 5)
—
COMPILED CODE
none · will guard
compiles 0 · deopts 0
ran on unguarded types: 0
last—
calls (global) / invocations0 / 0
heap objects (newest)—
✓ compiled code never runs on types it did not guard✓ every result and the calls counter equal the interpreter's
SCHEDULE
01[CONFIG]config: compiled code trusts the profile, no guard
02[CALLER]call add(1, 2)
03[CALLER]call add(3, 4)
04[CALLER]call add(5, 6)
05[JIT]JIT: compile add if it has run 3× in the interpreter
06[CALLER]call add(7, 8)
07[CALLER]call add(20, 22)
08[CALLER]report
Start of no guard, ints only scenario. 8 steps.
; add(a, b) compiled for (int, int), no guard
add:
  inc  dword [calls]     ; pc 0
  mov  eax, [a]          ; payload word: an int... or an address
  add  eax, [b]
  ret

Watch the CALLS box and the invariant strip. NO GUARD, STRING ARRIVES returns 8208 for add(“ab”, “cd”). UNSAFE: DEOPT TO pc 0 guards correctly but restarts the interpreter at the top of add: every result is right, yet the calls counter runs one ahead of the invocations. FIX: GUARD + DEOPT sends the same call back to the interpreter from pc 1, right after the increment that already ran, and later recompiles for int|str.

BREAK IT NOT YET

The JIT compiled add for (int, int) with no type guard, and so far every caller passes ints. Inject one call with types the profile never saw, at a point where it produces a result the interpreter would not.

GOAL: some call returns a result the interpreter would not

FIX IT NOT YET

Write the entry stub for compiled code. profile[i] lists the types argument i was compiled for ('int', 'float', 'str'). fn.typeOf(v) classifies a value, fn.compiled(args) runs the specialised code (wrong on any type outside the profile), and fn.interpret(args) runs the interpreter (always right, slow). Return the correct result, and use the compiled code whenever the types allow it.

Mental model:

compiled code is a speculation on what the profile saw. Speculation is safe only with a guard that checks the bet on every call, before the specialised operation uses it, and an exit that rebuilds the interpreter frame at the exact point it bailed, so no side effect runs twice. Fast path when the bet holds, the interpreter when it does not, and never a third option.

IN PRODUCTION

V8 (Ignition and TurboFan), SpiderMonkey, JavaScriptCore, HotSpot C2, PyPy, LuaJIT. You see it as warm-up curves, “deoptimized” lines in --trace-deopt, and polymorphic call sites that never get fast. Missing or wrong guards are a classic class of browser JIT security bugs.

IN INTERVIEWS

“How does a JIT make a dynamic language fast, and what happens when its assumptions are wrong?” Explain profiling, type specialisation, guards, deoptimisation with frame reconstruction, and why monomorphic code is faster than code that sees many types.

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.
NEXT: PARSING: PRECEDENCE & RECURSIVE DESCENT →