Benchmarking¶
The postpile repository ships a benchmark harness that measures its native backends against each other, the postpyc reference baseline, CPython and NumPy. The harness tracks the postpile-vs-postpyc trend over time (requirement 8). postpile began as a lift of postpyc; each recorded session shows any divergence as the backends evolve.
The harness is dev tooling: it lives in the top-level benchmarks/ package, outside the installed compiler. Run it with python -m benchmarks (or make bench).
Quick start¶
python -m benchmarks run # run the suite, print a summary
python -m benchmarks run --save # also record a session to benchmarks.db
python -m benchmarks run --save --label wip # tag the session
python -m benchmarks list # list recorded sessions (newest first)
python -m benchmarks compare # diff the two most recent sessions
run exits non-zero if any runtime produced an incorrect result, so it doubles as a coarse correctness gate in CI.
Commands¶
| Command | Flags | Purpose |
|---|---|---|
run |
--save, --db PATH, --label L |
Run every (kernel, runtime), print a min-time table and geomean ratios. --save records a session. |
list |
--db PATH, --limit N |
List recorded sessions (id, timestamp, git commit, label). Default --limit 10. |
compare |
--db PATH |
Compare the two most recent sessions per (kernel, runtime); +% means slower now. |
The database defaults to benchmarks.db in the working directory; override it with --db.
The runtimes¶
Each kernel is run through six contenders:
| Runtime | What it is |
|---|---|
postpile-c |
postpile's C backend, compiled with cc -O2. |
postpile-qbe |
postpile's QBE backend. |
postpile-llvm |
postpile's LLVM backend, assembled by llvmlite at -O2 (skipped if llvmlite is absent). |
postpyc |
The postpyc reference implementation: the baseline postpile is measured against. A dev dependency (requirement 8). |
cpython |
The pure-Python computation, interpreted. |
numpy |
The vectorized NumPy computation. |
postpile-c and postpyc both lower to C and hand it to cc -O2, so under this suite they compile byte-for-byte the same program (see below) and their times are expected to be equal. postpile-llvm also runs at -O2, so it too lands at parity with postpile-c, while cpython and numpy provide order-of-magnitude references.
If a runtime's build or setup fails (for example QBE is not installed), it is skipped with a note and the rest of the suite proceeds.
The kernels¶
Four kernels exercise different bottlenecks. Each is a POST @vectorize / @guvectorize source with a {pkg} placeholder for the compiler package, so the same source is templated for both postpile and postpyc, which compile identical programs.
| Kernel | Shape | Batch | Stresses |
|---|---|---|---|
square |
Float64 -> Float64 |
1,000,000 | Memory bandwidth |
dot |
(n),(n)->(), n=64 |
100,000 | Inner reduction |
gaussian |
Float64 -> Float64 via exp |
500,000 | libm exp |
matmul |
(m,k),(k,n)->(m,n), 4×4 |
100,000 | Nested-loop codegen |
Each kernel is compiled to a <name>_ufunc_loop symbol and driven over a large NumPy batch through the generalized-ufunc ABI (byte strides come straight from NumPy), so the inner computation runs entirely in compiled code: the Python-side call overhead is amortized across the whole batch.
matmul is included because square is memory-bandwidth-bound and gaussian is exp-bound, so neither reflects codegen quality. matmul's nested reduction is where the QBE backend's gap versus cc -O2 shows.
Methodology¶
- Round-robin adjacent timing. The compiled backends (
postpile-c,postpile-qbe,postpile-llvm,postpyc) are timed with their calls adjacent within each round (40 rounds, after 5 warmup passes). Timing them in adjacent slots means slow-machine drift hits all of them together and cancels in the ratio, so the backend-vs-backend comparison is trustworthy. The interpreted references are timed separately with fewer rounds; only their magnitude matters. - Min as the headline. The reported number per (kernel, runtime) is the minimum observed time, the measurement least polluted by scheduler noise. Mean and coefficient of variation (CV) are also recorded.
- Correctness-checked. Before timing, each runtime's output is compared with a NumPy reference with
np.allclose(rtol=1e-6, atol=1e-12). Incorrect results are flagged in the output and makerunexit non-zero.
The summary prints a per-kernel min-time table followed by three geomean ratios: postpile-c vs postpyc, postpile-qbe vs postpile-c, and postpile-llvm vs postpile-c (in each, <1 means the first is faster).
Every backend must build to a distinct .so path
dlopen caches loaded libraries by path. If two backends build their
shared object to the same filename, the second dlopen returns the
first-loaded library without recompiling; every backend silently
measures the C library and the suite reports fake parity. The harness avoids
this by giving each runtime its own path (<kernel>_<tag>.so, e.g.
matmul_qbe.so). Preserve that invariant when adding a backend.
Current results¶
python -m benchmarks run gave this on 2026-11-11 (Apple M1, Python 3.14, no postpyc, whose row is skipped):
| min time, ms | postpile-c | postpile-qbe | postpile-llvm | numpy | cpython |
|---|---|---|---|---|---|
square |
0.202 | 0.430 | 0.197 | 0.719 | 95.6 |
dot |
3.341 | 3.436 | 3.275 | 9.208 | 75.6 |
gaussian |
1.316 | 1.442 | 1.327 | 2.379 | 80.3 |
matmul |
4.891 | 3.923 | 4.884 | 5.236 | 136.3 |
postpile-qbetakes 1.18×postpile-c's time by geomean: 2.13× onsquare, where the C and LLVM backends emit a unit-stride fast path QBE did not get, and 0.80× onmatmul, where QBE's hand-rolled unrolling of the nested reduction wins.postpile-llvmtakes 0.99×, every kernel within 2%: it emits naive IR and lets LLVM's-O2do the LICM, strength reduction and unrolling, so it lands where the C compiler does.postpile-candpostpycboth go throughcc -O2on the same generated C, so they are expected at parity; the last run withpostpycinstalled (uv pip install -e ../postpython) measured ≈ 1.0×.
See QBE backend and LLVM backend for the codegen details and C backend for the baseline.
Cross-backend comparison (with WebAssembly)¶
run follows postpile and postpyc over time. A second command, cross, answers a different question (how do all the backends compare head-to-head, WebAssembly included?) and renders a shareable report.
python -m benchmarks cross # colour terminal table
python -m benchmarks cross --html report.html # + a self-contained HTML report
python -m benchmarks cross --quick # tiny batches (smoke run)
To put WebAssembly on equal footing with the native backends, cross uses whole-array kernels: a plain POST function that loops over the batch internally (def k(x: Array[Float64], out: Array[Float64]) -> None). The native pipeline uses a generalized-ufunc loop. (A WebAssembly module is not a linkable .o and has no ufunc loop, so the ufunc-driven run suite can't include it.) Every backend runs the same compiled kernel in a single call over the same data:
| Runtime | How it runs |
|---|---|
postpile-c · postpile-qbe · postpile-llvm |
native .so, driven over the __pp_array ctypes ABI |
postpyc |
the original postpyc compiler, same source and ABI, the baseline postpile was lifted from (requirement 8); parity with C is the health check |
wasm-wasmtime |
the C backend's kernel built for wasm32-wasip1, a WASI reactor in wasmtime (Cranelift, AOT); the arrays go into linear memory through the module's own malloc |
wasm-node |
the same .wasm in node (V8's TurboFan JIT), through node:wasi |
numpy · cpython |
the same maths, for scale |
Any runtime whose toolchain is absent (no zig, node, wasmtime, or postpyc) is skipped. The headline metric is throughput (elements/second); the report leads with an overall bar chart (each backend's geomean throughput vs C) and closes with a WASM engine head-to-head: wasmtime (Cranelift) vs V8 on the byte-identical module.
Two WebAssembly engines¶
wasmtime (Cranelift, ahead of time) and V8 (node, a JIT) are different engines; both run a WASI reactor and let the host write into the module's memory. The modules are 32-bit, so any engine that runs WASI preview 1 could join; deno is V8 again.
The four kernels are square (bandwidth), sumsq (reduction), gaussian (libm exp), and logistic (a tight scalar inner loop, the codegen-quality probe). Typical observations:
python -m benchmarks cross on 2026-11-11 (Apple M1, Python 3.14, wasmtime 47.0.1, node 26.5.0; postpyc not installed) gave, as throughput relative to postpile-c: LLVM 0.96, 1.01, 1.01 and 1.02 on square, sumsq, gaussian and logistic, 1.00 overall; QBE 0.53, 1.02, 0.94 and 0.67, 0.76 overall; WASM 0.14 (wasmtime) and 0.15 (V8) overall, V8 1.11× wasmtime. The notes below were written as the kernels were tuned (2026-07 to 2026-08); their figures are of that time.
square: bandwidth-bound and not SIMD: disassembly shows no backend auto-vectorizes here. All of C, LLVM, and postpyc saturate memory bandwidth with a 5-instruction post-increment pointer-walk inner loop; the win is-O2hoisting the array base/offset out of the loop (LICM), strength-reducing the index, and CSEingx[i]*x[i]to a single load. QBE used to trail badly (~0.23×): the hand-rolled passes neither CSEd the duplicatex[i]load nor strength-reduced the contiguous loop. Both were added (a local CSE pass, and constant-stride recognition in the strength-reduction pass so it unrolls like the ufunc kernels), lifting QBE to ~0.55×; the residual is QBE-the-tool's instruction selection: it emitsldr [x0]; add x0, 8where a post-increment / scaled-index load would do, which the postpile backend can't influence. This kernel also exposed an LLVM-backend gap; see the box below.sumsq: now at parity on every compiled backend (QBE included, once the contiguous loop strength-reduces and unrolls).logistic: LLVM tracks C; QBE trails ~1.5× on this scalar-heavy inner loop (no array strength reduction applies), the same class of gap the ufunc suite'smatmulshows.postpyc: lands at parity with C (both lower to C at-O2), confirming postpile has not regressed from the compiler it was lifted from.- WebAssembly (since 2026-11-16, the C backend's output built for
wasm32-wasip1): about 0.6 to 0.7 of native throughput by geomean,sumsqandlogisticnear parity,gaussianlowest because wasi-libc'sexpis slower than the host's. Before, a separate backend that emitted WebAssembly text ran at about 0.12 and leftgaussianout for its libm import.
The benchmark found (and fixed) a real LLVM-backend gap
Early runs had postpile + LLVM at only ~0.73× C overall, dragged down to ~0.3× on square. Disassembly showed why: the LLVM backend's naive IR had no alias metadata, so -O2 could not prove the store to out[i] doesn't clobber the __pp_array descriptor and reloaded x.data/x.offset/out.data/out.offset on every iteration. The fix was to emit TBAA metadata tagging descriptor loads and element loads/stores as disjoint types (exactly what clang does), which lets LICM hoist the descriptor loads. That's sound (unlike !invariant.load, it makes no constant-memory claim, so it stays correct when the same array is passed twice or a ufunc rebinds a view's data pointer). LLVM now runs at ~0.98× C across the board, clearly ahead of QBE (~0.54×). See the LLVM backend page.
Correctness in cross is a checksum sanity gate, compared with NumPy's; the byte-identity oracle remains the test suite.
Tracking over time¶
run --save records a session: a git commit (git rev-parse --short HEAD), a timestamp, and an optional label, plus one row per (kernel, runtime) with min/mean time, CV, run count, and correctness, all in SQLite.
python -m benchmarks run --save --label "post qbe unroll"
python -m benchmarks list
# 12 2026-07-10T14:22:31 13bcf30 post qbe unroll
# 11 2026-07-09T09:05:12 e1972d5
python -m benchmarks compare # newest vs previous, per (kernel, runtime)
compare reports new.min_time / old.min_time - 1 as a percentage per pair, so a positive number means the newer session is slower. That is the signal to watch: the goal is to keep postpile-c pinned to postpyc while closing the postpile-qbe gap without regressing anything.
Tip
The schema is two plain tables (sessions, results) in
benchmarks.database. If you want a chart or a longer-term dashboard,
query benchmarks.db directly with any SQLite client.
Shed Skin's C++ build¶
The census times each program against Shed Skin's own C++ build of it and CPython (0.3's task 1). Shed Skin is not a dependency of this repository. The sources are in sandbox/shedskin (version 0.9.13); Shed Skin is installed in a separate environment, so the repository's environment is never touched.
On macOS, with Homebrew's bdw-gc, pcre and cmake:
uv venv /tmp/shedskin-env --python 3.13
uv pip install --python /tmp/shedskin-env/bin/python ./sandbox/shedskin
mkdir /tmp/ss-sudoku5 && cp sandbox/shedskin/examples/sudoku5/sudoku5.py /tmp/ss-sudoku5/
cd /tmp/ss-sudoku5
PATH=/tmp/shedskin-env/bin:$PATH Python_ROOT_DIR=/tmp/shedskin-env \
/tmp/shedskin-env/bin/shedskin build --build-type Release sudoku5.py
./build/sudoku5
Shed Skin's build needs two settings its help does not mention. The CMake file Shed Skin writes finds Python with CMake's FindPython, which takes the first interpreter it finds, usually not the environment's: Python_ROOT_DIR points it there, and with the environment first on PATH the import shedskin succeeds. The build type defaults to Debug, so --build-type Release is what a timing uses. The Boehm collector and PCRE are built from Shed Skin's bundled sources into ~/Library/Caches/shedskin on the first build. On Linux they go to ~/.cache/shedskin. On Fedora, CMake installs the collector into lib64, where Shed Skin's build does not look; the link then fails on a missing lib/libgc.a. ln -s lib64 ~/.cache/shedskin/lib after the first build fixes it (Fedora 44, 2026-11-16).
sudoku5 prints one line, its own running time, so the output is CPython's with that line's number aside (TIME 0.74 where CPython prints TIME 1.24 on an Apple M1, 2026-11-14); the census compares such lines as volatile.