176 lines
11 KiB
Markdown
176 lines
11 KiB
Markdown
# The analyzer reworked
|
||||
|
|
|
|||
|
|
Asked on 2026-10-29: build every change of "If we were to rewrite the analyzer" (the answer
|
|||
|
|
of 2026-10-28), in stages, each leaving the compiler building itself and every suite passing.
|
|||
|
|
|
|||
|
|
The measure throughout is the compiler checking the sources of the commit 0575609 (the
|
|||
|
|
squashed one), best of five on CPU 2, as `measure.sh` takes it. The baseline is that
|
|||
|
|
commit's compiler built by itself: 0.312 s, 49.7 MB resident, 1,462M instructions, 549M
|
|||
|
|
cycles, 9,853,928 bytes. (Until stage 3 the measure was taken on the commit before it,
|
|||
|
|
which has an error the old checker let through, `&mut` of a `val` in an argument, and a
|
|||
|
|
checker that reports it skips evaluating, so its numbers are not comparable.)
|
|||
|
|
|
|||
|
|
The profile of the same run, by inclusive share of 1.50 billion instructions:
|
|||
|
|
|
|||
|
|
| Part | Share |
|
|||
|
|
|---|---|
|
|||
|
|
| Reading files: lexing, parsing, finding modules | 24.5% (finding modules on disk 5.6%) |
|
|||
|
|
| Checking declarations | 42.5% (the lifetime walk within it 8.9%) |
|
|||
|
|
| Checking instances of generics again | 12.3% |
|
|||
|
|
| Evaluating after checking | 7.7% |
|
|||
|
|
| Declaring, linking, signatures | 4.7% |
|
|||
|
|
|
|||
|
|
## Each stage is done when
|
|||
|
|
|
|||
|
|
- the compiler built by the last stage's builds itself twice alike (`build.sh`);
|
|||
|
|
- the corpus runs alike natively at -O1 and -O2 against the baseline, and `check` says the same of
|
|||
|
|
every corpus program, but where the stage means it to differ, said here (`validate.sh`);
|
|||
|
|
- the blossom suites of sakurac, std (but its known failure), linear and ecs pass;
|
|||
|
|
- it's measured, and what it left is in later.md.
|
|||
|
|
|
|||
|
|
## The stages
|
|||
|
|
|
|||
|
|
1. **Modules from an index.** The resolver lists each library's folders once and finds
|
|||
|
|
modules in memory, not by asking the disk. A file whose first declaration is a `when`
|
|||
|
|
section of another target is not parsed.
|
|||
|
|
2. **Lifetimes from the checking walk.** The checker records what the lifetime warnings
|
|||
|
|
need as it checks a body; the warnings come from that record, not a second walk.
|
|||
|
|
3. **Overloads chosen without checking each.** Each argument is checked once, into what
|
|||
|
|
choosing needs; each candidate is scored by a function that writes no result and makes
|
|||
|
|
no note; the one chosen is applied once. `mute`, `dropped` and `uninferred` go.
|
|||
|
|
4. **Node accessors from the shapes.** Each kind's slots are read by accessors that the
|
|||
|
|
shapes table describes, rather than by `extras[lefts[n] + slot]`.
|
|||
|
|
5. **IDs of their own types.** Declarations, types, nodes, names, tables and instances
|
|||
|
|
each have an ID type of their own, an enum of `u32`, which the checker refuses where
|
|||
|
|
another is wanted; every table keeps entry 0 for none, so nothing is stored as one more
|
|||
|
|
than an index; what keys on a file's node takes one `place`.
|
|||
|
|
6. **Checking on demand.** A declaration's signature, a body, a type's members and a
|
|||
|
|
constant's value are each worked out when first asked for, once. What's checked whole is
|
|||
|
|
the program's own files; a library's declarations are checked when reached.
|
|||
|
|
7. **Results by declaration.** Each top-level declaration has a result table of its own, so
|
|||
|
|
an edit drops the tables of what it changed and nothing else moves; checking again is
|
|||
|
|
asking again for what an edit made stale.
|
|||
|
|
8. **One evaluator, while checking.** The analyzer asks the program's evaluator, the
|
|||
|
|
machine, for a value when it needs one; the checker's own evaluator goes, and so does
|
|||
|
|
holding `compile_error` for later. An `if const` is decided when it's reached, so a
|
|||
|
|
branch not taken is not checked, as where its condition folds now.
|
|||
|
|
9. **One kind of compile-time value.** `constant` becomes a kind of `known`.
|
|||
|
|
10. **Instances check what depends on their arguments.** Checking a template marks each node
|
|||
|
|
whose results depend on its type parameters; an instance checks those, and reads the
|
|||
|
|
rest from the template's results.
|
|||
|
|
11. **Rows only for nodes with results.** A result table holds rows for the nodes that can
|
|||
|
|
have results, numbered by the parser.
|
|||
|
|
12. **Library bodies parsed when reached.** A library's function bodies are skipped at their
|
|||
|
|
braces and parsed when checking first asks for them.
|
|||
|
|
13. **Notes as data.** A note holds what it's about, as IDs, and its text is made when it's
|
|||
|
|
shown.
|
|||
|
|
14. **Bodies checked on threads.** Once the signatures a body needs are worked out, bodies
|
|||
|
|
are checked on several threads, each with its own place in checking, and what they
|
|||
|
|
work out is made the same whatever the threads' order, so a build is still the same
|
|||
|
|
every time.
|
|||
|
|
|
|||
|
|
## Status
|
|||
|
|
|
|||
|
|
| Stage | State | Instructions after |
|
|||
|
|
|---|---|---|
|
|||
|
|
| 1 | Built, validated | 1,445M (−1.4%) |
|
|||
|
|
| 2 | Built, validated | 1,332M (−7.8%) |
|
|||
|
|
| 3 | Built, validated | 1,327M (−0.4%) |
|
|||
|
|
| 4 | Built | 1,327M |
|
|||
|
|
| 5 | Built, validated | 1,332M; 506M cycles, 47.8 MB |
|
|||
|
|
| 6 | Built, validated | 1,277M; a corpus program 116M → 77M |
|
|||
|
|
| 7 | Built, validated | 1,278M |
|
|||
|
|
| 8 | Built, validated | 1,270M; 92 KB smaller |
|
|||
|
|
| 9 | Built, validated | 1,271M |
|
|||
|
|
| 10 | Built, measured, taken out | — |
|
|||
|
|
| 11 | Built, measured, taken out | — |
|
|||
|
|
| 12 | Reading built, bodies not | 1,261M; 482M cycles; a corpus program 77M → 67M |
|
|||
|
|
| 13 | Not built | — |
|
|||
|
|
| 14 | Not built | — |
|
|||
|
|
|
|||
|
|
What the stages built that differs from what they said:
|
|||
|
|
|
|||
|
|
- **3:** muting is gone from choosing overloads: an argument that may need its
|
|||
|
|
parameter's type is checked once, and waits only where its call's one overload wants a
|
|||
|
|
type argument nothing gave, and `path_waits` keeps what it found for checking the path
|
|||
|
|
later. `bounds_met` asks without reporting, and `dropped` and `uninferred`, which read
|
|||
|
|
the text of notes, are gone. One rule stays, `quiet`, for what is checked for its
|
|||
|
|
results alone: a type written for `this`, an attribute's type as reflection reads it,
|
|||
|
|
a template's branch where `if const` names what a parameter is, and an editor's query.
|
|||
|
|
It fixed a checker that let an error through: what was wrong within an argument that
|
|||
|
|
waited, as an undefined name in `take::<i32>(make(nope))`, was never reported, and the
|
|||
|
|
compiler then failed lowering it.
|
|||
|
|
- **5:** an enum can't be an ID type, since a value cast to an enum must be one of its
|
|||
|
|
members, and a struct of one `u32` is kept in memory, not a register, so IDs of their
|
|||
|
|
own types would slow the checker. Nodes of files and of tables are told apart instead:
|
|||
|
|
`file_node` and `table_node`, and `keyed<K>` maps that take no bare node. Instances,
|
|||
|
|
tables, bindings, and constants keep entry 0 for none; a struct's traits are marked
|
|||
|
|
worked out by a flag, so their run is held as it is.
|
|||
|
|
- **6:** every declaration's signature is still resolved first, since much of checking
|
|||
|
|
reads what a declaration is without asking (a field of `maybe` read unresolved gave
|
|||
|
|
a binding no value, and a range of an enum printed nothing); what is checked on
|
|||
|
|
demand is bodies and values. A declaration is reached where `depend` first records
|
|||
|
|
it, and a named type depends on its declaration; a struct reaches its fields, whose
|
|||
|
|
defaults a value of it takes. The compiler checks late what only it reaches, as the
|
|||
|
|
runtime's helpers, under the guard instance checking had, and lowers only checked
|
|||
|
|
globals; `system::dispatch` is reached with `system::handle`.
|
|||
|
|
|
|||
|
|
- **7:** as designed below, a table for an edit's nodes and the file's tables found by
|
|||
|
|
halving; per declaration tables were not needed for an edit to move nothing.
|
|||
|
|
- **8:** as designed below. Evaluating found two more of the old limits gone: a local
|
|||
|
|
constant reads another, or a struct's field, through its own evaluation, and an
|
|||
|
|
`if const` in an instance decides what follows it. A call written as a constant
|
|||
|
|
type argument is still refused, as before; a constant a call gives works there.
|
|||
|
|
- **9:** as said: a value is a `known` of its own kind, number or real, found again by
|
|||
|
|
`value_known` through an index of its own; the constants table is gone.
|
|||
|
|
- **10:** built and measured, then taken out. Of the compiler's templates, 21% of the
|
|||
|
|
top-level statements, which hold 53% of their nodes, depend on no type parameter. An
|
|||
|
|
instance reading those from its template's table left lowering an instance recursing
|
|||
|
|
without end. What's left is in later.md.
|
|||
|
|
- **11:** built and measured, then taken out. Of the rows a file's table holds, 72% are
|
|||
|
|
of nodes with results checking the compiler, and 15% checking a corpus program, whose
|
|||
|
|
libraries are mostly not reached. Numbering only those nodes saved 1.5% of the
|
|||
|
|
instructions and no memory: the peak rose. What's left is in later.md.
|
|||
|
|
- **12:** what was built reads less, not later: entry reads in rounds, growing what it
|
|||
|
|
holds each round, with the reporting thread started once reading ends; and the resolver
|
|||
|
|
lists a folder without asking again whether the folder holding it lists it, and passes
|
|||
|
|
a file that isn't Sakura by its name. Bodies parsed when reached were not built. Of a
|
|||
|
|
corpus program's library bodies, those never reached are 59% of the tokens (19,529 of
|
|||
|
|
them in generic bodies, 15,680 in others), and of the compiler's 7%. A generic's
|
|||
|
|
instance table covers its template's nodes in one run, so its body must be parsed with
|
|||
|
|
it; the checker holds the syntax's columns by address while it checks, so nodes added
|
|||
|
|
while checking move them; and the resolver finds the modules a body names from its
|
|||
|
|
paths. Parsing the other bodies when reached would save at most about 4M of a corpus
|
|||
|
|
program's 67M, and under 1% of checking the compiler.
|
|||
|
|
- **13:** not built. The text made for notes left out is 0.01% of checking the compiler,
|
|||
|
|
so making it only when shown saves nothing measurable. A note
|
|||
|
|
of IDs whose text is made when shown would need the checker's tables where it's shown,
|
|||
|
|
but `sakurac` shows notes from another thread while checking goes on, so the text
|
|||
|
|
would still be made where the note is given. 85 notes write lists or numbers that a
|
|||
|
|
few IDs don't hold. Nothing reads a note's IDs yet: it waits for the server offering
|
|||
|
|
fixes.
|
|||
|
|
- **14:** not built. Checking bodies is 48% of checking the compiler, but it writes what
|
|||
|
|
every body shares: of the 14,391 types, 3,246 instances, 903 knowns and 4,300 tables
|
|||
|
|
checking the compiler makes, its bodies make 6,157 types, 3,241 instances, 291 knowns
|
|||
|
|
and 3,987 tables, and reach 8,195 of the 9,249 declarations checked. Their IDs reach
|
|||
|
|
what the compiler builds, so threads need those tables made the same in any order
|
|||
|
|
first, as by IDs given in declaration order after the bodies are checked. What's left
|
|||
|
|
is in later.md.
|
|||
|
|
|
|||
|
|
## Designs of the stages still to build
|
|||
|
|
|
|||
|
|
- **7:** an edit adds a table for the nodes it added, past the file's last, rather than
|
|||
|
|
a table for the whole file with what was kept copied into it; a file keeps its tables
|
|||
|
|
in order, and a node no table around one being written holds is found among them by
|
|||
|
|
halving, outside an instance. A file parsed whole again gets one table anew.
|
|||
|
|
- **8:** the machine makes one evaluator before checking, whose lowerer and interpreter
|
|||
|
|
live through it, and gives the checker a function asking it for a value. Asking
|
|||
|
|
checks what checking reached first, keeps `at`, lowers the evaluation and what it
|
|||
|
|
calls, loads what is new into the interpreter, and runs it. A constant's value is
|
|||
|
|
asked for where it is read and not known; a local constant, an array's length, a
|
|||
|
|
range's end, an `if const`'s condition, and `compile_error`'s text where checking
|
|||
|
|
reaches them. A program with errors is not run. Without an evaluator, what only
|
|||
|
|
running gives is not known, and an `if const` it would decide has both branches
|
|||
|
|
checked quietly. After checking, the evaluator works out what the compiler needs
|
|||
|
|
that nothing asked for. A declaration checked within another's body sees none of
|
|||
|
|
that body's bindings.
|