ADR 0067: Speed is measured against C, on low-level programs, as one index per release¶
Status: Active Type: Process Created: 2026-10-27 Related-ADRs: 0002 (release mode, which is what is timed), 0053 (the IndexError a loop's shape proves dead), 0066 (a representation chosen for speed). Provenance: the owner, after the interim report of 2026-10-27.
Context¶
The goal (notes/goals.md) is to compile typed Python "as fast as hand-written C". Until now speed was measured against CPython, Cython, Numba and TurboPython (docs/src/benchmarks.md), and once against Go (n-body, notes/measurements.md, 2026-07-28). Being faster than CPython is a low bar, and every compiled program clears it. The goal asks how far a compiled program is from the same program written in C.
The question became urgent as coverage grew. Each feature that makes a real program compile may add a slower general path (ADR 0066 found boxed optionals costing kanoodle more than tenfold, and nothing had flagged it). The only speed gate, the cross-backend benchmark, holds backends to each other and never compares with C.
Decision¶
- The corpus.
benchmarks/lowlevel/holds pairs:name.py, valid Python and POST Python, andname.c, the same algorithm written as a C programmer would write it (plain arrays, stack data, no bounds checks). Programs are low-level: integers, floats, lists, loops and functions, with no classes, polymorphism, coroutines or parallelism. Each pair prints a checksum; both must print CPython's output. The first eight are numeric kernels:square,dot,matmul,nbody,spectral_norm,mandelbrot,fannkuch,sieve. Five more reach past flat numeric loops:nqueens(recursion overlist[bool]),matmul2d(alist[list[float]]matrix, as Python code writes one),levenshtein(indexing and comparingstrcharacters),wordfreq(str.splitand adict[str, int], the corpus's allocation-heavy program) andlife(flat two-dimensional indexing). - Same algorithm, idiomatic C. The C version may use the representation C would (a
boolarray, a fixed-size stack array) and must keep the algorithm. The index exists to measure a gap that comes from representation or from checks Python requires. - Same compiler, same contract. The C version is built by the
ccpostpile uses, at postpile's-O2, with-ffp-contract=off: CPython never fuses a multiply-add, so a fused C version computes a different number and is faster for it. postpile is timed in release mode (ADR 0002). Whatever checks release mode keeps count as postpile's cost. - The index. Each program is timed by the two-point slope of
benchmarks/wholeprog.py(built at two iteration counts, best of three wall-clock runs each, per-iteration time from the difference). A runtime's ratio on a program is its time divided by C's; the geometric mean of those ratios over the corpus is the runtime's speed index. 1.00 is parity, lower is better. The C backend's index is the headline; LLVM, QBE and CPython are columns beside it. - Recorded per release.
python -m benchmarks.wholeprog --jsonat each release, the per-backend index and the per-program ratios written tonotes/measurements.mdwith the machine.benchmarks/lowlevel/baseline.jsonis the reference run on the reference machine (Apple M1). - A gate.
python -m benchmarks.wholeprog --check benchmarks/lowlevel/baseline.jsonfails when any program's postpile-c ratio has grown by more than 25% over the baseline, or when a postpile checksum differs from CPython's. It runs before a release, beside the cross-backend benchmark. It is not part ofmake test: it takes minutes, and wall-clock time on a shared machine is too noisy for a per-commit gate. - Distance is tracked. A program whose postpile-c ratio exceeds 2.0 is a tracked item in
notes/plan.md, with its cause. - The corpus grows. A program joins when postpile compiles it and the C version matches CPython. The native language is C because it needs no new toolchain and removes the compiler as a variable; Rust, Go or Fortran versions may join as extra columns, and change nothing above.
Consequences¶
- The readings live in
notes/measurements.md, with the machine and the method of each; the table of current numbers there holds the latest index. This ADR records what is measured and what the target is. - The distance found so far comes from representation and from checks Python requires: an element wider than C's, a string operation done through a general helper, an IndexError check a loop's shape did not prove dead. Code generation has not been the cause. The IndexError check is Python's semantics, so the way to close those gaps is proof that the check is dead (0053's line); a release mode that dropped it would change what the program computes.
- What this does not measure. Classes are out of the corpus by definition. Strings, dicts and allocation entered with the second four programs, and the distance lies in them. Short runs are noisy, so a long run takes about a second.