Files
sakurac/docs/performance.md
T
2026-12-06 13:52:57 -06:00

369 lines
21 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# Performance
Where the compiler started, where it is now, and where it needs work: how fast it checks
and builds, how much memory it takes, how large it and what it makes are, and how fast
the code it makes runs.
"Start" is the compiler as committed on 2026-10-08, at `e5b7636`, its first commit.
"Now" is its working tree on 2026-10-20. Every comparison of start and now was measured
on 2026-10-20, on one machine in the same hour. The probes of other programs are dated
where they're given.
## How it was measured
- **The machine:** an i5-13420H, on battery, which caps its performance cores at
2.1 GHz. Wall times are about half what the same runs take plugged in, so they compare
with each other and not with older notes. Instructions and cycles are the processor's
own counts of the program's work outside the kernel, and don't depend on the clock.
- **Each run** was pinned to CPU 2, a performance core, the runs of a group taken in
turns: the best of 5 for builds, 9 for checks, 21 for a one-line program, 3 for the
VM. Peak memory is the largest resident size of any run.
- **The compiler runs on one thread here (`-j 1`),** as the start compiler does.
[Threads](#threads) gives what its default of up to eight does.
- **The compiler:** its own fixed point at `-O2`.
- **The work:** sakurac checking and building its own sources of its day, as it builds
itself (`entry.sakura analyzer compiler machine native`, and `server` now), against
its std. Those grew, so each result is also given per 1,000 lines of what was read:
| | Start | Now | |
|---|---|---|---|
| sakurac's sources | 49,307 lines, 112 files | 76,816 lines, 160 files | +56% |
| std | 21,892 lines | 40,241 lines | +84% |
| All read, per build or check | 71,199 lines | 117,057 lines | +64% |
## At a glance
| | Start | Now |
|---|---|---|
| Building sakurac | 1.78 s | 3.38 s |
| · instructions per 1,000 lines | 121M | 131M (+8%) |
| · peak memory | 132 MB | 229 MB |
| Checking sakurac | 0.256 s | 0.353 s |
| · instructions per 1,000 lines | 19.0M | 16.0M (−16%) |
| · peak memory | 42 MB | 74 MB |
| Checking a one-line program | 0.061 s | 0.093 s |
| · instructions per 1,000 lines of std | 13.7M | 11.2M (−18%) |
| · peak memory | 15 MB | 27 MB |
| The compiler's executable | 11.6 MB | 8.7 MB |
| A program printing a line | 274 KB | 155 KB |
**The compiler held its cost per line** while what it reads grew by two thirds: checking
costs less a line than it did, and building 8% more, which the 2026-10-18 optimizer
passes cost and pay back in the code they make.
## Speed
Building and checking, the compiler on the sources of its day, at 2.1 GHz:
| | Start | Now |
|---|---|---|
| Building: wall time, best | 1.778 s | 3.375 s |
| · median | 1.787 s | 3.388 s |
| · instructions | 8.63G | 15.39G |
| · cycles | 3.49G | 6.44G |
| Checking: wall time, best | 0.256 s | 0.353 s |
| · instructions | 1.36G | 1.88G |
| · cycles | 0.49G | 0.65G |
| A one-line program: wall time, best | 0.061 s | 0.093 s |
| · instructions | 300M | 451M |
Every program is checked with the whole of std, so the one-line program is what std
costs each check: about 2.3 ms for each 1,000 lines now, at this clock. Generic code
costs two to three times what plain code does, as `std/simd.sakura` measured on
2026-10-14.
### The code it makes
Other programs, from the plans' probes, at `-O2`. These were measured as each plan was
built or brought up to date, on a busy machine:
| What | Time | Measured |
|---|---|---|
| `f32` `a * b + c`, scalar | 87.9 ms | 10-20 |
| The same, `f32x8` | 5.9 ms | 10-20 |
| Finding a byte, `u8x16` | 1.78 ms | 10-20 |
| `index_of`, a byte at the end of 1 MiB | 62 µs | 10-20 |
| `is_ascii` of 1 MiB | 62 µs | 10-20 |
| Vectors of three `f32`s, by operators | 166 ms | 10-20 |
| Random draws, 64 bits | 2.5 ns | 10-20 |
| A call through a function value | 2.72 ns | 10-20 |
| A generic `F`, inlined | 0.48 ns | 10-20 |
| A call through a `function` | 2.86 ns | 10-20 |
| A list of 1,000,000 `box`es, ten times | 539 ms | 10-19 |
| The same through `raw_allocate` | 414 ms | 10-19 |
| The same in a `pool` | 209 ms | 10-19 |
| Three relaxed atomics | 0.1 ns | 10-20 |
Where the speed comes from:
- **Vectors in registers.** The compiler's `-O2` holds `simd` values in registers and
picks AVX2 or SSE2 as the program runs.
- **Values out of memory.** A statement's temporary cell holds each value it's given in
a cell of its own, so one call given its address no longer keeps the rest in memory,
and a cell written as a vector and read by its lanes, or the other way, holds one
width. Before, small structs as temporaries went through the stack, and four writes
read back as one vector waited for the cache: a contact solved with `vec2`s took
three times the same written a number at a time, and now takes 1.06 times it.
- **`f32`s as `f32`s.** Comparing, negating, and choosing keep an `f32` single, a choice
by a comparison is `maxss`, `minss`, or a mask rather than a branch, and no conversion
waits on what its register held before, which chained a loop's iterations together.
Against native code, physics's benchmarks, after 2026-10-31's work, take three to four
times as long as Box2D's scalar build and five to six times Jolt's, each built by GCC
16.2 at `-O2`. What's left, as profiled:
- **Registers.** Values get 11 of the 16 SSE registers, the rest being scratch for
vectors' operations, so a loop of `f32x8`s or many `f32`s spills where GCC's doesn't.
- **`std::math::min` and `max` of reals** follow IEEE 754's minimum and maximum: a NaN
test of each, and one of signed zeros, around `minss`, three branches where the
physics wants one instruction.
- **Inlining.** The limit stays 16, but since the later 10-31 work a call is inlined by
what its copy costs where it is, so `cross` and `mat3`'s helpers, taking vectors in
memory, are inlined where their reads promote. A loop already holding seven vectors
is left its calls, as the registers above would spill what inlining promotes:
inlining `cross` into the contact solver of Jolt's scenes made it 50% slower so.
- **A cell's values that cross blocks,** and a named local's, stay in one cell, as an
`apart` struct and a `vec2` temporary do in physics2d's `furthest_apart`.
- **Parameters held in memory** are copied by the function given them, though the caller
made a copy too.
- **What the optimizer takes out:** checks of a view's data, indexes checked again,
bounds in counted loops, work on constants moved out of loops, small branches as
`cmov`, the stack's limit in small leaf functions, and the order of an atomic known
while compiling.
### The VM
The compiler running itself on its VM, which then checks a one-line program. The time
includes checking and lowering the compiler for the VM:
| | Start | Now |
|---|---|---|
| The VM | 2.69 s, 17.8G instructions, 122 MB | 5.00 s, 29.2G, 244 MB |
A call through a function value takes 81.8 ns in the VM, and an inlined generic call
24.5 ns.
### Compiled code in the machine
Since 2026-10-28 the machine compiles each function to x86-64 as it is first called, where
it runs as a Windows executable (`machine/jit.sakura`), and since 2026-10-29 as a Linux one. Measured on a Ryzen 9 9950X3D,
plugged in, each benchmark a function of its own called through a value, so none is
inlined into another; `run --fast` against `build -O2`, the checksums alike:
| Work | Built | Interpreted | Compiled by the machine | Compiled ÷ built |
|---|---|---|---|---|
| Multiplies and shifts mixing a word, 20M times | 9.4 ms | 93.6 ms | 9.1 ms | 0.97 |
| `fib(30)`, calls | 2.5 ms | 50.1 ms | 3.8 ms | 1.5 |
| 10,000 particles of `f32`, 300 steps | 5.5 ms | 148 ms | 19.8 ms | 3.6 |
| Methods of a struct of three `f32`s | 10.8 ms | 121 ms | 24.1 ms | 2.2 |
| A sieve of 20M bytes | 30.5 ms | 246 ms | 37.9 ms | 1.2 |
| A `dict` of 200,000, then 1M lookups | 26.0 ms | 102 ms | 26.3 ms | 1.0 |
| Sorting 1M words | 68.9 ms | 444 ms | 78.7 ms | 1.1 |
| Interpolating 200,000 texts | 13.5 ms | 33.4 ms | 36.4 ms | 2.7 |
hearth's scripts, 2,000 entities each running a behavior (`hearth.scripting.bench`, run as
its executable): an `update` turning its transform took 311 ns an entity interpreted and
121 ns compiled, against 27 ns as a system of the program's own.
Where it falls short of built code:
- **Words, not values.** The machine's code holds values in words of the frame shared by
values whose lives don't overlap, an `f32` and an `f64` alike, and compiled code keeps a
word a single only where every value in it is one. Splitting each word into the values
it holds, by liveness, would let `f32` arithmetic stay single throughout.
- **What the bytecode reloads.** A vector's data pointer is read from its cell each time
round a loop, which built code moves out of the loop.
- **Formatting** calls the machine's own code for each part of a text.
- **A function inlining many loops** shares its registers among the words its loops use
most: all eight benchmarks inlined into one `main` ran mix in 18.6 ms, not 9.1.
### Threads
The compiler runs on threads, one for each processor up to eight. Unpinned, on this
machine's twelve:
| | `-j 1` | Default | |
|---|---|---|---|
| Building sakurac | 3.34 s, 226 MB | 1.97 s, 257 MB | 1.70× |
| Checking it | 0.353 s | 0.301 s | 1.17× |
Reading files, optimizing, and placing values run on threads. Checking, lowering, and
choosing code are one thread's.
## Memory
Peak resident memory:
| | Start | Now |
|---|---|---|
| Building sakurac | 132 MB | 229 MB |
| · per 1,000 lines | 1.85 MB | 1.96 MB |
| Checking sakurac | 42 MB | 74 MB |
| · per 1,000 lines | 0.59 MB | 0.63 MB |
| Checking a one-line program | 15 MB | 27 MB |
The compiler's memory grew as what it reads did, 6% to 7% more a line.
The compiler has no collector since 2026-10-19. Programs hold what they allocate in
owners, arenas and pools, and each thread keeps a cache of free blocks.
## Size
### The compiler
| | Start | Now |
|---|---|---|
| Executable | 11,570,800 bytes | 8,730,512 |
| · code | 2.52 MB | 3.01 MB |
| · constant data | 0.60 MB | 1.02 MB |
| · line tables | 8.28 MB | 4.63 MB |
The compiler's executable carries its line tables, which fault traces read, as 53% of
it.
### What it makes
A program printing a line, at `-O2`:
| | Start | Now |
|---|---|---|
| Executable | 273,512 bytes | 154,856 |
| · code | 36,046 | 35,618 |
| · constant data | 24,128 | 30,616 |
| · line tables | 203,032 | 74,296 |
The compiler's 2026-10-18 optimizer drops every function nothing reaches, with its code,
lines and symbols.
## Where a build goes now
The compiler building its own sources of now, profiled with callgrind: on one thread,
built with `-g`, 15.8G instructions in all. Shares are of instructions, inclusive of what
each phase calls:
| Phase | Share |
|---|---|
| Reading the files: lexing and parsing | 3.0% |
| Checking | 7.9% |
| Evaluating constants | 0.9% |
| Lowering, less what follows | 14.8% |
| Working out what calls keep | 1.4%, within the rows around it |
| Optimizing, inlining included | 56.5% |
| Choosing code and placing values | 14.8% |
| Writing the executable, closing what's left | about 1% |
**Optimizing is over half of a build,** and no pass stands out: the optimizer's files
take 23% of the instructions by themselves, and the code they walk and edit
(`compiler/intermediate/`) another 14%. Spread through every phase:
- **indexing vectors** (`vector::at`) is 13% of all instructions, and appending 4%. The
checks on them are about 3% of a build, as the checks plan measured;
- **`dict`** is 2.8%, `memset` and `memcpy` 3.5%, and the heap 1.8%;
- **the checker's own lifetime warnings** are 1.8%.
## Where work is needed
In order of what each would gain.
1. **Optimizing.** It's 56% of a build, and the 2026-10-18 passes cost 13% of a build
for code 10.5% faster. No pass stands out; what the passes walk and index does, as
`vector::at` at 13% of all instructions. Code held in fewer, denser arrays, or walked
without an index checked at each step, would serve every pass.
2. **The compiler's threads.** Eight give 1.7 times one. Checking, lowering and choosing
code run on one thread, which caps what more processors give.
3. **std's cost to every check.** Before the std plan, a one-line program cost a check
of 40,241 lines of std: 93 ms and 27 MB at this clock. The std plan, built on
2026-10-20, made `core`, a library of the language's own that every program reads,
and reads std's modules only where a program refers to them. An empty `main` now
checks in 21.4 ms and 7.3 MB, and a program printing a line in 42.2 ms and 11.0 MB,
since `std::io` still reads std's root. The plan had probed 30.1 ms for it. What's
read still costs about 2.3 ms each 1,000 lines, and two to three times that for
generic code.
4. **Line tables,** 53% of the compiler's executable and 48% of a small program's.
5. **The VM on calls,** 81.8 ns through a function value and 24.5 ns for an inlined
generic call.
6. **Smaller costs that programs meet** ([later.md](later.md)):
- reading the clock on Linux is a system call, 211 ns, where the vDSO takes 25;
- lambdas given to adapters cost 3 to 4 times functors;
- `box` is 1.31 to 1.38 times `raw_allocate`.
## How it got here
Each step as it was measured then, on the machine and clock of its day.
| Date | What | Measured |
|---|---|---|
| 10-05 | The analyzer, before self-hosting | Checking 23,574 lines: 0.09 s, 19 MB, 495M instructions |
| 10-08 | The scratch arena | The compiler compiling itself: 5.34G to 4.84G instructions, 100 MB to 89 MB |
| 10-08 | First self-build | 15.4G instructions, about 3 s; then 8.37G, 1.65 s, 129 MB, an 11.3 MB executable |
| 10-08 | The back end's remaining work | Code 2,644,785 to 2,517,459 bytes; building 9.42G to 8.96G |
| 10-14 | Simplifying and speeding up | Building 11.10G to 8.31G, 2.20 s to 1.66 s; checking 0.31 s to 0.21 s; executable 14.4 MB to 6.8 MB |
| 10-14 | Vectors in registers | Building 8.61G to 7.99G; checking 1.36G to 1.27G; `f32x8` loop 21.6 ms to 8.7 ms |
| 10-16 | Compiling on threads | On 32 processors under load: building 838 ms to 564 ms on eight threads |
| 10-16 | One-word references, text by words | Building 8.37G to 7.85G, 171 MB to 166 MB; checking 1.355G to 1.302G |
| 10-18 | The optimizer's new passes | Code 10.5% faster; building the old sources 13% slower; executable 9.51 MB to 8.17 MB |
| 10-19 | Iterators | The fixed point 7.80 MB to 8.67 MB, from inlining `next` |
| 10-19 | Memory without a collector | A list of a million nodes, ten times: 3,046 ms collected in the plan's probe, 539 ms in `box`es released one by one as built; four threads allocating, 8.5 s to 0.29 s with caches |
| 10-20 | `std::function` and `void` | The fixed point 8,730,512 bytes |
| 10-20 | `core`, and std read as it's imported | Checking an empty `main` 93.1 ms to 21.4 ms, 27.7 MB to 7.3 MB; a program printing a line 93.3 ms to 42.2 ms; the compiler checking itself 352.5 ms to 338.6 ms, 72.4 MB to 54.0 MB |
| 10-20 | Contracts as statements | A contract that holds costs 0.11 ns more than a plain `if`, with the blocks that only stop the program laid out last; it was 5.7 ns as a call into std |
| 10-20 | Checks in checked programs only | One frozen copy built with `--checks` and without: building 3,523 ms to 3,372 ms (16.2G to 14.0G instructions), checking 350.8 ms to 323.5 ms, executable 9,209,216 to 8,732,296 bytes |
| 10-20 | The rules settled (roadmap step 5) | The compiler checking itself on one thread, step 4's checker against step 5's: 324.7 ms and 323.9 ms, no difference; the fixed point 8,770,256 bytes |
| 10-20 | Accessors with `this` and `value` implied (step 6) | Checking itself: 324.5 ms with step 5's checker on its form of the sources, 324.4 ms with step 6's; the fixed point 8,773,288 bytes |
| 10-28 | `f32` in single precision, instructions chosen by the processor, stores merged into vectors, spilling the value read furthest ahead, a loop's head that jumps through a table at a multiple of 64 | In cycles at this machine's low-power clock: `linear`'s `cross` 30 to 12, `quat * quat` 35 to 18, `normalized` 34 to 6; a loop of `vec3<i32>` 17 ms to 11 ms; the machine's loop 1,264M to 1,017M cycles, and no longer 1,180M when the code before it moves |
| 10-31, later | Bit tracking, code lifting, constants a branch learned, constants grouped and added last, loops regrouping what they keep, and inlining by what each call's copy costs, after LLVM's passes | physics's benchmarks, the installed compiler against this one, best of three in turns, in ms a step: Box2D's large pyramid 7.27 to 7.19, its many pyramids 30.36 to 29.89; Jolt's pyramid 33.39 to 30.17, its convex hulls on a mesh 13.13 to 12.54, the same to the bit. Building itself on one thread 1.68 s to 1.76 s, on eight 0.84 s to 0.86 s; the fixed point 10,943,790 bytes |
| 10-31 | Conversions that wait on nothing; `f32` comparisons, negation, and choosing in single precision; reals chosen by a comparison without a branch; `x != x` tested by the parity flag; versions made from the code as lowered; a temporary cell's values split apart; vectors read by lanes and lanes by vectors; `scatter` written out; promotion once more after numbering | physics's benchmarks, each against the 10-30 compiler side by side on a busy machine, in ms a step: Box2D's large pyramid 13.4 to 7.6, its many pyramids 60.1 to 40.3; Jolt's pyramid 52.0 to 40.6, its convex hulls on a mesh 21.5 to 16.0. A contact solved with `vec2` temporaries 11.7 ns to 4.35, with GCC's `-O2` at 2.98. Building itself 0.76 s to 0.85 s; the fixed point 10,530,038 bytes |
What made the compiler faster, in the order of what each saved:
- a build optimized every function twice (12% of building);
- dependencies recorded only for the language server, and results held per slot rather
than in dictionaries (13% of checking);
- what calls keep worked out, so receivers and locals stay off the heap: boxes made
while building itself fell from 4.7M to 30K;
- the scratch arena for each pass's temporaries;
- one-word references;
- the per-thread caches of blocks, and the blocks kept for reuse over 2 KiB;
- vectors in registers, and `memset`, `memcpy` and frames zeroed 16 bytes at a time;
- the lexer interning through an open table of hashes, finding punctuators by tables,
and skipping comment lines eight bytes at a time;
- line tables of 16-byte rows (10.2 MB to 3.0 MB then);
- leaving the checks of indexes and the rest to checked programs, and null and division
by zero to the processor's faults (4% of building, 8% of checking).
Tried and dropped, so they needn't be tried again:
- an open-addressing `dict`, at loads of 3/4 and 1/2: 0.5–1.7% slower to check;
- inline limits of 20 and 24: checking 8–11% faster, building 1–4% slower, code 9–19%
larger;
- one arena for every function's code: more instructions and 122 MB;
- a radix sort for the register allocator, which ran more than the heap sort;
- forwarding a call's result into the cell it's copied to, which saved nothing;
- waiting a while for the heap's lock before sleeping;
- a table of names for each file, which lexed every name again on each thread;
- compacting in the middle of the passes as well as at the end;
- inlining every function called once into callers of up to 6,000 instructions: 6%
more time;
- inlining by each call's cost with no regard for the vectors a loop holds: Jolt's
convex hulls 14% slower, its contact solver spilling a value at a time;
- raising the limit within loops for functions of several blocks too, as
`clamp`'s IEEE tests are: Box2D's scenes 4–7% slower;
- working out the bits known of every word at once, round after round until a
loop's settle: 2% of a build, where asking of each word a few steps back, as LLVM
does, costs a third of that;
- copying the test of `vector::close` into its callers: 400 KB more code;
- alias classes by type, which a reinterpreting cast would make wrong;
- words for trimming text, YAML's plain scalars, and the lexer's words and spacing,
each slower than bytes, as most runs are short;
- aligning every loop's head to 16 bytes, as functions are: the machine's loop took about
10% more cycles, and no native loop gained. What its loop needed was the head that jumps
through the table of operations within one 64-byte line, which a 16-byte boundary
doesn't promise: such heads begin at a multiple of 64 since 2026-10-28.
## Reproducing this
The start's numbers stand as measured on 2026-10-20: today's compiler doesn't read
`e5b7636`'s sources, so nothing builds the start again.
`tools/profile/count.c` runs a command pinned to a processor and prints its wall time,
peak memory, instructions and cycles, and `tools/profile/measure.py` times the
compiler's work, start and now, in turns through it. Its paths are at its top. `perf`
isn't installed here, and the counters need `perf_event_paranoid` at 2 or less.
For where the time goes, build the compiler with `-g`, profile it with callgrind, and
read the profile with `tools/profile/costs.py`, as its header says.