Initial Commit

This commit is contained in:
SoraKatadzuma committed 2026-10-28 22:13:49 -05:00
commit 05756094ac
706 files changed
+135430

No files matched your search

+395
View File
@@ -0,0 +1,395 @@
# Performance
Where the two compilers started, where they are now, and where they need work: how fast
they check and build, how much memory they take, how large they and what they make are,
and how fast the code each makes runs.
"Start" is the two compilers as committed on 2026-10-08: C at `62644a5` ("Close to
self-hosted") and the rebuild at `e5b7636`, its first commit. "Now" is both working
trees on 2026-10-20. Every comparison of start and now was measured on 2026-10-20, all
four compilers 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
VMs. Peak memory is the largest resident size of any run.
- **The rebuild compiles on one thread here (`-j 1`),** as the start compilers and C do.
[Threads](#threads) gives what its default of up to eight does.
- **The compilers:**
- C, built by gcc with `-O3 -g`, by hand as chinook would;
- the rebuild, its own fixed point at `-O2`. "C-built" is the rebuild as C builds it,
the same program from other code.
- **The work:** each compiler checking and building the rebuild's sources of its day, as
the rebuild builds itself (`entry.sakura analyzer compiler machine native`, and
`server` now), against the rebuild's std. Those grew, so each result is also given per
1,000 lines of what was read:
| | Start | Now | |
|---|---|---|---|
| The rebuild'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% |
| C's own sources | 57,444 lines of C | 67,674 lines of C | +18% |
## At a glance
| | Rebuild, start | Rebuild, now | C, start | C, now |
|---|---|---|---|---|
| Building the rebuild | 1.78 s | 3.38 s | 1.71 s | 5.80 s |
| · instructions per 1,000 lines | 121M | 131M (+8%) | 103M | 213M (+106%) |
| · peak memory | 132 MB | 229 MB | 176 MB | 397 MB |
| Checking the rebuild | 0.256 s | 0.353 s | 0.259 s | 0.623 s |
| · instructions per 1,000 lines | 19.0M | 16.0M (−16%) | 13.0M | 18.2M (+40%) |
| · peak memory | 42 MB | 74 MB | 127 MB | 327 MB |
| Checking a one-line program | 0.061 s | 0.093 s | 0.066 s | 0.163 s |
| · instructions per 1,000 lines of std | 13.7M | 11.2M (−18%) | 10.0M | 13.2M (+32%) |
| · peak memory | 15 MB | 27 MB | 45 MB | 112 MB |
| The compiler's executable | 11.6 MB | 8.7 MB | 5.5 MB, 1.2 MB stripped | 6.3 MB, 1.4 MB stripped |
| A program printing a line | 274 KB | 155 KB | 588 KB | 739 KB |
| The rebuild as each builds it, building itself | 8.63G instructions | 15.39G | 10.57G (1.22×) | 29.84G (1.94×) |
What it says:
- **The rebuild 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.
- **C's cost per line doubled for a build** and grew 40% for a check, and its memory more
than doubled. It was level with the rebuild at the start, and is now 1.7 times its time
to build, 1.8 times to check, and 1.7 to 4.4 times its memory.
- **The code C makes has fallen behind:** the same program built by C ran 1.22 times
the instructions the rebuild's build of it did at the start, and 1.94 times now. The
rebuild gained the optimizer and back end work of 2026-10-08 to 10-18, and C only part
of it.
## Speed
Building and checking, each compiler on the sources of its day, at 2.1 GHz:
| | Rebuild, start | Rebuild, now | C, start | C, now |
|---|---|---|---|---|
| Building: wall time, best | 1.778 s | 3.375 s | 1.710 s | 5.803 s |
| · median | 1.787 s | 3.388 s | 1.720 s | 5.823 s |
| · instructions | 8.63G | 15.39G | 7.36G | 24.91G |
| · cycles | 3.49G | 6.44G | 3.40G | 11.75G |
| Checking: wall time, best | 0.256 s | 0.353 s | 0.259 s | 0.623 s |
| · instructions | 1.36G | 1.88G | 0.93G | 2.13G |
| · cycles | 0.49G | 0.65G | 0.43G | 1.04G |
| A one-line program: wall time, best | 0.061 s | 0.093 s | 0.066 s | 0.163 s |
| · instructions | 300M | 451M | 219M | 532M |
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 in the rebuild now, and 4.1 ms in C,
at this clock. Generic code costs two to three times what plain code does, mostly in
C's instances (`checker__clone`), as `std/simd.sakura` measured on 2026-10-14.
### The code each makes
The same program, the rebuild's compiler, built by each compiler and then building the
same sources:
| | Start | Now |
|---|---|---|
| Built by the rebuild | 1.778 s, 8.63G instructions | 3.375 s, 15.39G |
| Built by C | 2.154 s, 10.57G | 5.593 s, 29.84G |
| C's code against the rebuild's | 1.21× the time, 1.22× the instructions | 1.66×, 1.94× |
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, so the ratios are surer than the times:
| What | Rebuild | C | C against the rebuild | Measured |
|---|---|---|---|---|
| `f32` `a * b + c`, scalar | 87.9 ms | 110.7 ms | 1.3× | 10-20 |
| The same, `f32x8` | 5.9 ms | 78.8 ms | 13× | 10-20 |
| Finding a byte, `u8x16` | 1.78 ms | 42.9 ms | 24× | 10-20 |
| `index_of`, a byte at the end of 1 MiB | 62 µs | 698 µs | 11× | 10-20 |
| `is_ascii` of 1 MiB | 62 µs | 86 µs | 1.4× | 10-20 |
| Vectors of three `f32`s, by operators | 166 ms | 417 ms | 2.5× | 10-20 |
| Random draws, 64 bits | 2.5 ns | 5.8 ns | 2.3× | 10-20 |
| A call through a function value | 2.72 ns | 4.61 ns | 1.7× | 10-20 |
| A generic `F`, inlined | 0.48 ns | 1.79 ns | 3.7× | 10-20 |
| A call through a `function` | 2.86 ns | 4.56 ns | 1.6× | 10-20 |
| A list of 1,000,000 `box`es, ten times | 539 ms | 675 ms | 1.25× | 10-19 |
| The same through `raw_allocate` | 414 ms | 563 ms | 1.36× | 10-19 |
| The same in a `pool` | 209 ms | 136 ms | 0.65× | 10-19 |
| Three relaxed atomics | 0.1 ns | 7.4 ns | 74× | 10-20 |
| Building a `u32[16000]` constant | 0.15 s | 5.77 s | 38× | 10-20 |
What makes the difference:
- **Vectors in registers.** The rebuild's `-O2` holds `simd` values in registers and
picks AVX2 or SSE2 as the program runs. C lowers `lanes_held()` to `false`, so its
vectors and std's text functions go through memory or a word at a time.
- **What the rebuild's 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.
- **C's optimizer is quadratic** in a constant array's length.
- **C is ahead in one place:** a `pool`, 136 ms against 209.
### The VMs
Each compiler running the rebuild's compiler on its VM, which then checks a one-line
program. The time includes checking and lowering the compiler for the VM:
| | Start | Now |
|---|---|---|
| The rebuild's VM | 2.69 s, 17.8G instructions, 122 MB | 5.00 s, 29.2G, 244 MB |
| C's VM | 1.99 s, 8.1G, 167 MB | 6.32 s, 27.7G, 403 MB |
The rebuild's VM was a third slower than C's at the start, and is a fifth faster now. On
small calls it's still slower: a call through a function value takes 81.8 ns in the
rebuild's VM and 41.6 ns in C's, and an inlined generic call 24.5 ns and 10.8 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`). 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 rebuild compiles on threads, one for each processor up to eight. Unpinned, on this
machine's twelve:
| | `-j 1` | Default | |
|---|---|---|---|
| Building the rebuild | 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. C compiles on one.
## Memory
Peak resident memory:
| | Rebuild, start | Rebuild, now | C, start | C, now |
|---|---|---|---|---|
| Building the rebuild | 132 MB | 229 MB | 176 MB | 397 MB |
| · per 1,000 lines | 1.85 MB | 1.96 MB | 2.47 MB | 3.39 MB |
| Checking the rebuild | 42 MB | 74 MB | 127 MB | 327 MB |
| · per 1,000 lines | 0.59 MB | 0.63 MB | 1.78 MB | 2.79 MB |
| Checking a one-line program | 15 MB | 27 MB | 45 MB | 112 MB |
The rebuild's memory grew as what it reads did, 6% to 7% more a line. C's grew 37% to
57% more a line, and a check by C takes 4.4 times the rebuild's memory.
The rebuild has no collector since 2026-10-19, nor does C's runtime. Programs hold what
they allocate in owners, arenas and pools, and each thread keeps a cache of free blocks.
## Size
### The compilers
| | Rebuild, start | Rebuild, now | C, start | C, now |
|---|---|---|---|---|
| Executable | 11,570,800 bytes | 8,730,512 | 5,504,368 | 6,337,928 |
| · code | 2.52 MB | 3.01 MB | 1.03 MB | 1.20 MB |
| · constant data | 0.60 MB | 1.02 MB | 0.07 MB | 0.08 MB |
| · line tables, or debugging information | 8.28 MB | 4.63 MB | 4.3 MB | 4.9 MB |
| The rebuild as C builds it | 17,064,568 bytes | 27,417,928 | | |
| · its code | 4.62 MB | 6.70 MB | | |
The rebuild's executable carries its line tables, which fault traces read, as 53% of it.
C's carries DWARF from `-g`, and is 1.4 MB stripped. The rebuild's code is 2.5 times C's,
for a program 14% larger than C's in lines, with std's code in it too. As C builds the
rebuild, its code is 2.2 times what the rebuild makes of the same sources.
### What they make
A program printing a line, at `-O2`:
| | Rebuild, start | Rebuild, now | C, start | C, now |
|---|---|---|---|---|
| Executable | 273,512 bytes | 154,856 | 588,032 | 738,760 |
| · code | 36,046 | 35,618 | 78,276 | 119,713 |
| · constant data | 24,128 | 30,616 | 199,048 | 251,784 |
| · line tables | 203,032 | 74,296 | 248,472 | 271,200 |
The rebuild's 2026-10-18 optimizer drops every function nothing reaches, with its code,
lines and symbols. C's code for the same program is 3.4 times as large.
## Where a build goes now
Each compiler building the rebuild's sources of now, profiled with callgrind: the
rebuild on one thread, built with `-g`, 15.8G instructions in all; C, 25.8G. Shares are
of instructions, inclusive of what each phase calls:
| Phase | Rebuild | C |
|---|---|---|
| Reading the files: lexing and parsing | 3.0% | about 2% |
| Checking | 7.9% | 6.3% |
| Evaluating constants | 0.9% | |
| Lowering, less what follows | 14.8% | about 1% |
| Working out what calls keep | 1.4%, within the rows around it | 43.9% |
| Optimizing, inlining included | 56.5% | 24.6% |
| Choosing code and placing values | 14.8% | 20.3% |
| Writing the executable, closing what's left | about 1% | about 1% |
**C spends 44% of a build working out what each call keeps** (`compiler__find_boxes`,
through `compiler__keeps`, `compiler__scan_body` and `compiler__receives`), which the
rebuild does in 1.4%. C scans functions that call one another by assuming what each
keeps and scanning again until that stops growing, and what it found within such a cycle
is forgotten each time an enclosing one is scanned again. The compiler's checker and
emitter are large cycles of recursive functions, so their scans repeat. The rebuild has
worked out each group of recursive functions once, as one, since 2026-10-18. That is
most of why C's cost per line doubled since the start: about 11 of its 26 billion
instructions.
**In the rebuild, 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. **What calls keep, in C.** It's 44% of C's build, against 1.4% of the rebuild's.
Working each function out once, and recursive ones as a group, as the rebuild does,
would take about 40% off C's builds. C's checking costs 1.4 times a line what it did
too, and its memory 1.4 to 1.6 times a line. See
[Where a build goes now](#where-a-build-goes-now).
2. **The code C makes.** The rebuild's optimizer of 2026-10-14 to 10-18 has no C
counterpart: vectors in registers, check removal, `select`, moving work out of loops,
dropping what nothing reaches, and the orders of atomics. C's executables run 1.94
times the instructions, with 2.2 to 3.4 times the code. C's frames copy a word at
a time, which keeps its AVX code slower than its SSE2 code.
3. **Optimizing in the rebuild.** 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.
4. **The rebuild's threads.** Eight give 1.7 times one. Checking, lowering and choosing
code run on one thread, which caps what more processors give.
5. **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 in the rebuild at this clock, 163 ms and
112 MB in C. 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. In the rebuild 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.
6. **Line tables,** 53% of the rebuild's executable and 48% of a small program's.
7. **The rebuild's VM on calls,** at twice C's VM's time for small calls.
8. **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 rebuild's analyzer, built by C | Checking 23,574 lines: 0.09 s, 19 MB, 495M instructions; C's checker 0.11 s, 54 MB, 363M |
| 10-08 | The scratch arena | The rebuild 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; C 7.72G, 2.01 s, 175 MB |
| 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 |
What made the rebuild 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;
- another promotion pass late in the pipeline, which changed nothing;
- 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;
- 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 compilers come from the commits:
```sh
git -C malunal.sakura archive 62644a5 | tar -x -C c_start
git -C malunal.sakura.rebuild archive e5b7636 | tar -x -C r_start
# Build c_start's sources/*.c with the chinook dependencies, -std=c11 -O3 -g.
# Build r_start with it, then with that stage, to its fixed point:
c_start/sakura build --std r_start/std -O2 -o r_start_c ...
r_start_c build --std r_start/std -O2 -o r_start_s1 ...
```
`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 each
compiler's work 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.