Foundations of ComputingDeeper

Kayavak / Foundations of Computing / CS·10 Compilers & Runtimes / Deeper

Deeper · Foundations of Computing

Compilers & Runtimes

The questions the chapter doesn't stop for: why this exists, the model to keep in your head, what changes when an assumption does, how sure we are of each claim, and what's still open. Read it after the chapter; back to the chapter.

Why does this exist?

In the early 1950s most programmers believed a machine could never translate formulas into code as good as a person's. John Backus's team at IBM built FORTRAN (released 1957) to prove otherwise, and spent much of their effort on optimisation, because they knew nobody would adopt a compiler that produced slow programs. They succeeded, and programming in mathematical notation became normal.

The rest of the pipeline followed. The ALGOL 60 report described its syntax in a notation now called BNF (after John Backus and Peter Naur), so grammars became formal objects that parsers could be built from. John McCarthy's Lisp (1958–60) introduced garbage collection, freeing memory automatically once nothing could reach it. Research on Smalltalk and then Self (David Ungar, Craig Chambers and Urs Hölzle, late 1980s–1990s) made just-in-time compilation with speculative optimisation practical; that work led, through a startup Sun acquired, to Java's HotSpot (1999). In 1991 Ron Cytron and colleagues published an efficient way to build SSA form, now the core of most optimising compilers, and from 2000 Chris Lattner's LLVM made a reusable optimiser and code generator that many languages (Rust, Swift, Zig, Clang's C and C++) share.

The mental model

Try it yourself first: write your own model in the Mental Model Explorer, then compare.

A compiler is a series of translations, each into a form where some question is easy to answer: tokens for "what words are these", a tree for "what's the structure", typed IR for "is this valid and what can be simplified", machine code for "what does the CPU do". A runtime is the code that keeps running alongside yours.

Question Answer
What is it? A translator from source to an executable form, plus the runtime services the program needs while running
Problem it solves Letting people write in a language suited to them while the machine runs code suited to it
Parts Lexer, parser, semantic analysis, IR, optimiser, code generator, linker; runtime: start-up, stacks, threads, GC, JIT
How it works Each stage checks and transforms; optimisations rewrite IR into faster IR that means the same thing
Assumptions The program follows the language's rules; optimisations preserve meaning for every valid program
When they change Code with undefined behaviour isn't valid, so "preserve meaning" promises nothing and optimised code can do anything
Where it fails Compile time and binary size grow with optimisation; JIT warm-up; GC pauses and memory overhead
Related ideas Grammars and trees (ch03, ch05); calling conventions and registers (ch07); runtimes use threads and virtual memory (ch08)

What if?

What if a JIT's assumption turns out wrong?

Reason it through, then open

A JIT compiles hot code assuming what it has seen so far: this call always reaches one class, this number is always an integer. When a different class or a string appears, the guard check fails and the runtime deoptimises: it discards the fast code, rebuilds the interpreter's state for the exact point it was at, and continues slowly, maybe recompiling later with the wider assumption. Correctness is kept; the cost is a pause and a slower path. It's also why benchmarks must warm up and why types that change unpredictably make JavaScript or Java code slower.

What if a language had neither garbage collection nor ownership?

Reason it through, then open

That's C: the programmer frees memory by hand. It's fast and predictable, and it's also the source of use-after-free, double-free and leak bugs, which security teams at Microsoft and Google have each reported cause around 70% of their serious security vulnerabilities in C and C++ code. Garbage collection removes those bugs by tracking reachability at run time; Rust's ownership removes them by checking lifetimes at compile time instead.

Where the boundary is

Aspect Ahead-of-time compiler Interpreter JIT compiler
Translates Before running, all at once Not to machine code; executes as it reads While running, only hot code
Start-up Fast Fast Slow until warm
Peak speed High Low High, sometimes higher (it knows run-time facts)
Examples C, Rust, Go, Zig CPython's bytecode loop Java HotSpot, V8, PyPy
Confused because Most real systems combine them: Java compiles to bytecode ahead of time, then interprets and JIT-compiles it

Derive it

Why a generational garbage collector is cheap.

  1. A copying collector moves the live objects to a fresh area and discards the rest wholesale, so its cost is proportional to the live data, not to the space being reclaimed.
  2. Observation (the weak generational hypothesis): most objects die young. Temporary strings, iterators and request objects become garbage almost immediately.
  3. So allocate new objects in a small "young" area and collect it often. If, say, 95% of it is dead each time, the collector copies only 5% and reclaims the rest for free.
  4. Objects that survive several collections are moved to an "old" area that is collected rarely.
  5. The catch: old objects can point to young ones, so the collector must track those pointers (a write barrier and a remembered set), or it would free young objects still in use. That bookkeeping is the price of the speed.

How sure are we?

Claim How it's known Where from
Optimisations preserve the meaning of valid programs Theorem, per optimisation, when proven; in practice compilers have bugs Verified compilers such as CompCert prove it for a C subset
Most objects die young Measured True of most programs studied since the 1980s; some workloads (caches) break it
Around 70% of serious security bugs in big C/C++ codebases are memory-safety bugs Measured Reported separately by Microsoft (2019) and Chromium (2020)
HotSpot's C1 and C2 thresholds Measured Vary by version and flags; the chapter gives rough figures

Still open

How to get memory safety into the vast amount of existing C and C++ is an open and urgent question: rewriting in Rust, hardware support (memory tagging, CHERI capabilities), safer C++ subsets. Garbage collectors keep improving on pause times; making JITs both fast and secure (JIT compilers are a frequent source of browser vulnerabilities) is ongoing.

What would change your mind? If a large codebase in a memory-safe language showed the same rate of serious security bugs as similar C++ code, the case for memory-safe languages would weaken. Published data so far points the other way.

Recall

What does a JIT do when its speculative assumption fails?

Deoptimises: discards the fast code, rebuilds the interpreter's state and continues, perhaps recompiling later.

Why is a copying collector's cost proportional to live data?

It only moves the live objects; everything left behind is reclaimed at once.

What is the weak generational hypothesis?

Most objects die young, so collecting a small young area often is cheap.

Why can undefined behaviour make optimised code do "impossible" things?

Optimisers assume valid programs never have UB, so they may remove checks or reorder code based on that assumption.

Why did FORTRAN's team care so much about optimisation?

Programmers believed compiled code would be slower than hand-written code, and wouldn't adopt it otherwise.

Reflect

Be honest with yourself; this stays in your browser. Anything you can't tick yet is what to revisit.