Skip to content

Latest commit

 

History

History
457 lines (399 loc) · 33.7 KB

MEASUREMENT.md

File metadata and controls

457 lines (399 loc) · 33.7 KB

Measuring performance

What the project has learned about its own measurements, and the harnesses that apply it. Every performance claim in a pull request or in the README comes from one of these, run in CI.

What decides what

  • Instruction counts decide a logic change. Callgrind counts are deterministic: the same commit gives the same count on any runner. A change that deletes work shows as fewer instructions; one that does not, does not.
  • But a Rust total moves with code layout, by up to about 1% of a parse. In a function of thousands of instructions (Expr_nud is about 13k; the lexer's dispatch function likewise) any change, even a cold call site out of line, makes LLVM assign registers differently: single-stepping two builds showed the same branches taken and the cost per call moved by up to about ±10 instructions, in a different direction on different inputs (PR #184). So for a change to parser or lexer logic, read in this order: (1) the work counters: a change that removes work is durable; (2) the Rust totals over the real inputs as a set (checker.ts, typescript.js, parser.ts, typescript.d.ts, gen-textmate.ts, typescript.ts): the set must not get worse, and no real input may rise by more than about 0.3% without an explanation; the synthetic stress inputs (dense-semi, dense-plus, nonascii, synth2mb) are reported and may move inside that band; (3) an input that moves against the counters gets the per-function Callgrind (the full-scope profile), which says whether it is work or layout (same call counts, cost per call changed). Go instructions and TS block counts have no such band: a rise there is real.
  • For a change in memory behaviour, instructions are blind: allocator calls and bytes and page faults decide. (PR #126: instructions flat, wall-clock +46%, explained entirely by page faults.)
  • Work counters (tokens, nodes pushed, checkpoints, restores, peeks) say what the parser did. They are identical between two commits unless the grammar or the parser's search changed.
  • Wall-clock ratios between jobs are not comparable. The same commit's ratio to oxc differed by 25-40% between two runs: runner hardware. Only ratios inside one job, from alternating rounds, compare; and inside one job, moves of up to 5% without instruction-count support have been code-layout noise.
  • Every target is measured on the same inputs, side by side. An improvement belongs to the generator, so a change that moves one target and not the others is listed in the profile's summary as "single-target". Go: Callgrind instructions of one ParseWith, comparable to Rust's (the Go runtime's own work inside the parse, allocation and growth, is included). TS target and emitted JS parser: V8 has no deterministic instruction count with the JIT on, so they are measured by V8 precise block coverage with counts: the block executions of one parse. That is exact and sees control flow (a loop that does fewer rounds, a lookup that becomes a switch, a copy removed), but not the cost inside one block (a string slice against a byte read is the same block count), and V8 reports a block that always runs with its function as part of the function's count. It detects a change of control flow, not its cost or direction: on PR #141 block counts rose 7-10% while time fell 10-28% and allocations 27%. So the exact allocations of the same parse (test/bench-targets/allocs.ts, alloc-work's counting) are reported beside them.
  • Go under Callgrind is deterministic only when built and run as the harness does: -tags faketime (the runtime's clock stands still; with a real clock sysmon preempts the parse every 10 ms of wall time and the scheduler's work lands inside the measured region as often as Valgrind's speed allows), GOGC=off GOMAXPROCS=1 GODEBUG=asyncpreemptoff=1, and collection toggled by a client request around the parse (--toggle-collect=<function> ends early: Callgrind tracks returns by the stack pointer, and the Go runtime switches stacks). What remains random is a Go map's hash seed: a lookup's instructions differ between runs (up to 0.5% of a parse of checker.ts), their number does not. The Go column counts every instruction of the parse, and beside it the number of calls into the map runtime (test/bench-targets/go-ir.ts), which must be 0 for the count to repeat: the Pratt operator tables were the last maps on the typescript grammar's parse path (PR #144); a parse that reads one does not repeat, and its cell is reported NOT RUN. Only reuse mode (a parse into the builder of a first one) is measured: a first parse into a new builder varied by up to 0.14% between runs in the page allocator's search (runtime.(*pageAlloc).find), with or without address randomization; a fresh parse's allocations are reported in docs/ALLOCATIONS.md (regenerated on master). The profile measures M twice; a Go / TS / JS cell whose two measurements differ by more than 0.01% is reported NOT RUN in the summary, without failing the job (the pull request may not touch the code it measures).
  • The fair comparison with oxc is reuse mode on both sides: one kept CstBuilder (parse_with) against one Allocator reset() before each parse. Fresh mode (a new builder / allocator per parse) is reported beside it.

What moves the Pratt loop's instruction count

The Rust LED loop (Expr_bp, and Expr_bp'2, Callgrind's name for its recursive calls) moved by 1-5% between builds that did nearly the same work. Per-line Callgrind and objdump of the profiled binaries (2026-11-24; full profiles 45324707067, 45324710405, 45324753624, tools $SHARED/scratch/sound-8/cgfn.py / cgdiff.py) found three causes. None of them is noise: each repeats exactly.

  1. An inlining flip. Expr_bin(op) (the binary-operator match) was inlined while it had one call site. A second call site (a composite-lead helper) made LLVM call it instead: +10 Ir per operator read, 102,865 calls on checker.ts, net +0.72%. Callgrind shows the function as a new callee.
  2. Lid renumbering. Adding one literal shifts every lid. Expr_post's match op { 27 | 28 } became { 24 | 25 }, which LLVM lowers as (op & 0xfffe) == 24 folded branch-free into the neighbouring conditions: 8 instructions where op - 27 < 2 + jae was 5. That is +4 Ir per operator read (dense-plus +4.2M, typescript.js +1.7M) with no change in work.
  3. A real compare on every read. A second composite lead compared in the operator read (t.lid != GT_LID && t.lid != GT_LID2) costs +3 Ir on every operator read, not only on <. How to read a +1% in this function: compare the callee lists (a new callee is an inlining flip), then the per-line delta keyed by source text (cgdiff.py). Lines that only exist in one build, or "line 0" (spills) moving against them, are codegen. A rise on a line the change did not touch, with identical call counts, is renumbering or register allocation. The remedy is the operator row (parser-ir prattOpRow): one masked table load per LED iteration, whatever the lids and however many call sites there are. The row also carries the led group's dense index, so the led switch is a match on row >> 27 (cases 1..k, a jump table) rather than on the lid: a first version that kept match t.lid there cost typescript.d.ts +0.87%, because LLVM had folded the small rules' binary match into that lid jump table and the row added a second dispatch. A rule without postfix (binary) operators prints no postfix (binary) test at all. A test that can never pass still costs its instructions on every iteration: Type_bp paid +213k on typescript.d.ts for tests of fields its row never sets. What remains is one masked load per iteration (Expr_bp'2 on checker.ts: +397k), partly offset by the composite-lead compare it replaces (-136k). The row landed with Rust at +0.08% to +0.32% on the real inputs (Go -3% to -3.9%, TS blocks -0.5% to -1.2%) because of what that load buys: the loop's cost no longer depends on lid numbering or on how many composite leads and flags a grammar has. A second lead (<, composeLt) is the same bit 21 set on one more lid, with no new compare in the loop.

Large functions at the edge of the register budget

The same symptom showed in the lexer. Making @ a punctuator (one more punctuator site in lex_from) cost +0.35% of a checker.ts parse with identical work and token counts. Per line, the growth was "line 0" +322k and the whitespace arm +120k. In objdump, the dispatch was one jump table in both builds, but the input length n, kept in %r14 in A, was spilled and reloaded inside the whitespace loop in M (stack references 755 -> 791). In the emitted Rust, a very large function (the lexer loop with about 70 inlined emit sites; a Pratt loop with its led arms inlined) sits at the edge of the register allocator's budget, so one more site spills a long-lived value somewhere else. How to recognise it: identical work counters, and the extra Ir in line 0 (spills) or on lines the change did not touch. The cure is structural, never a local tweak: make the varying part data instead of code (the operator row; punctuators as a table with one emit site), and move cold paths out of line so the hot loop stays small whatever the grammar declares.

The harnesses

What Where When
Rust: instructions (Callgrind) per parse in both modes, lexer alone, work counters, allocator calls / bytes, page faults, peak; emitted sizes of every target test/bench-rust-profile/run.sh (PROFILE_SCOPE=pr), test/emit-sizes.ts, report test/bench-rust-profile/pr-summary.ts; workflow measure-profile every pull request touching src/**, a grammar file or the harness; A = merge base, M = head; the job summary shows the tables
Go target: Callgrind instructions per parse (reuse), through the direct engine (CstBuilder) and the builder engine (SlimBuilder); TS target and emitted JS parser: V8 block executions per parse; side by side with Rust per input, single-target changes listed the same run.sh (section "Go and TS targets"), test/bench-targets/go-main.go (count-* modes), test/bench-targets/blocks.ts; on the main inputs in the same measure-profile job
The same plus oxc beside A and M, native timings, phase regions, cache / branch simulation the same run.sh with PROFILE_SCOPE=full on demand: the measure-profile-full label on a pull request, or measure-profile workflow_dispatch with scope full
Rust wall-clock vs oxc: alternating rounds in one job, medians and per-round ratios, one job per mode test/bench-rust-oxc/run.sh (BENCH_BASE, BENCH_EXTRA, BENCH_GRAMMAR, BENCH_MODE); workflow measure-bench on demand: workflow_dispatch, or the measure-bench label on a pull request
TS target, emitted JS parser, Go: allocations and bytes per token and per node, by site, result vs overhead, reported in docs/ALLOCATIONS.md test/alloc-work.ts --write (test/alloc-instrument.ts); the gate alloc-work checks only linearity and Go reuse = 0 the report: the README auto-regen workflow on every master push that touches src/** or a grammar; the gate: every CI run
TS / JS / Go wall-clock A/B: alternating rounds in one job, a fresh process per measurement, medians and per-round ratios test/bench-targets/run.sh (TARGETS_BASE, TARGETS_COMMIT, TARGETS_GRAMMAR, TARGETS_ONLY); workflow measure-targets on demand: workflow_dispatch, or the measure-targets label on a pull request

Failure policy of the pull-request profile: a measurement the harness did not produce fails the job (NOT RUN is never a pass); a Go / TS / JS measurement that did not repeat is shown as NOT RUN and does not fail it. A regression does not fail it (a threshold on instructions would be arbitrary, and a grammar change legitimately changes work); it is listed at the top of the summary: any input whose instructions rise by more than 0.5%, any rise in allocator calls per parse in reuse mode, any emitted size up by more than 1%; and, separately, every input on which one target moves by more than 0.1% while another stays within 0.01%.

Emitted sizes also have a RECORD, docs/emit-sizes.json (grammar × target, an emit option as <target>+<option>), so that growth made of steps each under 1% accumulates visibly (Go +18% in two days, 0016998 → d326d65, was seen only afterwards). The profile summary prints M against the record beside A → M and lists a size more than ±0.25% from its record as a regression; that listing is the check (it is not a gate of node test/check.ts, where it made unrelated landings re-record). A landing that changes a size on purpose re-records with node test/emit-size-record.ts --write and says why in its message: that diff is where the drift so far is accepted.

The inputs are fixed: checker.ts and parser.ts of TypeScript at the repository's one pin (TS_REPO_SHA in the workflows), typescript.js / typescript.d.ts of the locked typescript npm package, gen-textmate.ts and typescript.ts of this repository as of 2632c2c, a 2 MB synthetic file, a non-ASCII-heavy file and two dense one-byte-token files. Three element inputs (test/bench-rust-profile/element-inputs.ts): jsx-corpus.tsx and jsx-corpus.jsx, the element code of the pinned tsc corpus's JSX conformance cases, parsed by typescriptreact and javascriptreact; and gen-textmate.tsx, the text of gen-textmate.ts parsed by typescriptreact, the control: the same tokens and tree as gen-textmate.ts through typescript, so its difference is what the element machinery costs code outside any element.

Recorded result: Rust against oxc

What is compared: monogram's Rust parser builds a CST (every token is a leaf) with the typescript.ts grammar; oxc builds a typed AST. They are not the same product, so a ratio compares two kinds of work on the same input, both parsing as TypeScript. Which of the two costs more to build has NOT been measured here (an earlier version of this page said a CST is the cheaper one; that was an assumption: a CST keeps every token as a leaf, an AST drops most of them); a like-for-like figure needs a monogram parser that builds an AST directly, which is in progress (builders generated from a defineShape shape).

Method: test/bench-rust-oxc against oxc_parser 0.126.0: both parsers in one GitHub-hosted job, 9 alternating rounds, the median of the per-round time ratios; instruction counts from test/bench-rust-profile (Callgrind, one parse plus the drop of what it built). Only ratios inside one job compare. Reuse mode is the like-for-like mode for memory: one CstBuilder kept across parses (parse_with) against one oxc Allocator reset() before each parse; fresh mode is a new builder / allocator per parse.

The default tree keeps every token (2026-11-29, land/lossless-cst)

Since this landing the default CST of the Rust, Go and TS targets holds every token as a leaf, punctuation included, as the interpreter's always did. From #56 until this landing it dropped the punctuation leaves, so every row below this section was measured on a tree without them: a CST that was not lossless. Keeping them costs the Rust parse 2% to 5% more instructions on the real inputs (dense-semi.ts, which is almost all ;, 24%), the Go direct engine about 4%; the TS target executes about 1% fewer blocks but makes 14% to 20% more allocations (one object per leaf; Go fresh parses allocate 4% to 8% more bytes, docs/ALLOCATIONS.md). Instructions per parse, measure-profile full scope, run 46290960497 (A = master 118bee29, M = the landing), monogram ÷ oxc:

input reuse, A reuse, M fresh, A fresh, M
checker.ts 0.896 0.915 0.900 0.919
typescript.js 0.893 0.914 0.894 0.914
parser.ts 0.891 0.914 0.907 0.938
typescript.d.ts 0.997 1.034 1.014 1.053
2 MB synthetic file 0.921 0.957 0.923 0.960
non-ASCII-heavy file 0.879 0.895 0.913 0.923
gen-textmate.ts 0.924 0.947 0.942 0.967
typescript.ts 0.725 0.746 0.788 0.826
dense ; 0.957 1.191 0.959 1.194
dense a+a+… 0.809 0.787 0.811 0.788
jsx-corpus.tsx 1.346 1.409 1.391 1.446
jsx-corpus.jsx 1.224 1.287 1.348 1.433

On checker.ts monogram now runs 106.8M instructions per parse in reuse mode (was 104.6M), oxc 116.7M. typescript.d.ts crosses above oxc: 3.4% more in reuse mode (was 0.3% less). Time was not re-measured.

monogram bf18adc (2026-11-23)

Time: measure-bench run 45244727475 (both modes on AMD EPYC 9V74). Instructions: measure-profile full scope, run 45242296642 at 7b713ea (AMD EPYC 7763); no commit between 7b713ea and bf18adc changes an instruction count (bf18adc's own profile: every input equal to its base). oxc accepted every input in the table (an input either parser rejects gets no ratio). Time per parse and instructions per parse, monogram ÷ oxc (below 1: monogram takes less):

input time, reuse (range over 9 rounds) time, fresh instructions, reuse instructions, fresh
checker.ts (TypeScript compiler, 3.2 MB) 0.776 (0.754-0.786) 0.849 0.885 0.889
typescript.js (npm typescript 5.9.3, 9.1 MB) 0.763 (0.759-0.774) 0.802 0.881 0.882
parser.ts (TypeScript compiler) 0.746 (0.741-0.749) 0.837 0.881 0.897
typescript.d.ts (npm typescript 5.9.3) 0.812 (0.809-0.821) 0.789 1.005 1.022
2 MB synthetic file 0.746 (0.712-0.762) 0.889 0.919 0.921
non-ASCII-heavy file 0.689 (0.668-0.696) 0.848 0.875 0.909
src/editor/gen-textmate.ts at 2632c2c 0.768 (0.760-0.772) 0.980 0.910 0.929
typescript.ts at 2632c2c 0.645 (0.633-0.662) 0.481 0.717 0.780
dense ; (1 MB of empty statements) not timed not timed 0.966 0.968
dense a+a+… (1 MB) not timed not timed 0.805 0.806

How to read it:

  • Monogram's parse takes less time than oxc's on every timed input in both modes, and executes fewer instructions on nine of the ten counted inputs; typescript.d.ts (type declarations) is level (0.5% more in reuse mode, 2.2% more in fresh mode).
  • On checker.ts monogram runs 103.3M instructions per parse, oxc 116.7M.
  • Time ratios depend on the runner's CPU model: one day earlier, cdf0e6b on an Intel Xeon 6975P-C gave 0.680 to 0.860 in reuse mode (run 45222935238; checker.ts 0.805). Compare time ratios only within one job; the instruction columns are the stable ones.
  • The products differ (CST against AST), as stated above; this is not a like-for-like claim.

The element inputs (TSX and JSX)

measure-profile full scope, run 45276033027, monogram at 143e93b (with only the input change of the harness on top), AMD EPYC runner; Callgrind instructions per parse. The inputs (element-inputs.ts) are the tsc conformance corpus's element code filtered to the parts BOTH parsers accept (and tsc's parser): 319 TSX parts of the 425 that contain a tag (tsc's parser accepts 364, the interpreter 360 of those, oxc rejects 2 of them, 39 left out for repeating an earlier part's export), 67 JSX parts of the 366 that tsc's parser accepts (the interpreter 85, 18 left out for a repeated export); each input joins its parts. Both parsers accept every part and each joined input.

input bytes parts oxc, reuse monogram, reuse ÷ oxc oxc, fresh monogram, fresh ÷ oxc
jsx-corpus.tsx (typescriptreact) 168,466 319 10,380,015 13,982,077 1.347 10,366,486 14,436,673 1.393
jsx-corpus.jsx (javascriptreact) 9,335 67 503,319 613,844 1.220 503,660 643,825 1.278

On element code monogram executes 22% to 39% more instructions than oxc, where on plain TypeScript it executes fewer (the table above). Per token of jsx-corpus.tsx (33,089 tokens) monogram runs 422.6 instructions (reuse) against 297.1 on checker.ts in the same run, oxc 313.7 against 334.5: monogram's cost per token rises by 42% on element code and oxc's falls by 6%. Of monogram's +125.5 per token, the lexer alone accounts for +48.5 (190.8 against 142.3: the element frames, text runs and holes of the section "Recorded result: element dialects" below, where the same lexer figure was 203.7 on an earlier corpus), the rest is the parse.

monogram e6cc55c (2026-11-22)

measure-bench run 45042153759 (reuse: AMD EPYC 9V45; fresh: AMD EPYC 9V74) and measure-profile full scope, run 45042161482. Time per parse and instructions per parse, monogram ÷ oxc (below 1: monogram takes less):

input time, reuse (range over 9 rounds) time, fresh instructions
checker.ts (TypeScript compiler, 3.2 MB) 0.908 (0.870-0.935) 0.922 1.03
typescript.js (npm typescript 5.9.3, 9.1 MB) 0.891 (0.868-0.917) 0.872 1.04
parser.ts (TypeScript compiler) 0.898 (0.863-0.912) 0.914 1.04
typescript.d.ts (npm typescript 5.9.3) 0.941 (0.915-0.982) 0.839 1.13
2 MB synthetic file 0.916 (0.876-0.953) 1.032 1.10
non-ASCII-heavy file 0.733 (0.716-0.745) 0.953 1.05
src/editor/gen-textmate.ts at 2632c2c 0.882 (0.873-0.904) 1.048 1.04
typescript.ts at 2632c2c 0.723 (0.700-0.741) 0.549 0.91
dense ; (1 MB of empty statements) not timed not timed 1.34
dense a+a+… (1 MB) not timed not timed 0.99

How to read it:

  • On these inputs and these two CPU models monogram's parse takes less time than oxc's in reuse mode on every timed input, while executing 3% to 13% more instructions on seven of those eight. Both hold at once because the cost per instruction differs; an earlier run of an older commit on an Intel Xeon ordered one input the other way. The ratios are not a property of the parsers alone.
  • The products differ (CST against AST), as stated above. This is not a claim that monogram is the faster parser.
  • Where monogram does more work: typescript.d.ts (type-declaration-heavy) and dense empty statements.
  • On checker.ts monogram runs 120.2M instructions per parse (50.1M of them lexing), oxc 116.6M.

monogram 8d75e66 (2026-11-22, before the lexer and decision work)

The same method, two runs per mode (runner CPUs AMD EPYC 7763, one reuse run on an EPYC 9V74). Time per parse, monogram ÷ oxc:

input reuse mode fresh mode
checker.ts 1.032 / 1.091 1.063 / 1.041
typescript.js 1.024 / 1.039 0.999 / 0.985
parser.ts 1.030 / 1.074 1.048 / 1.052
typescript.d.ts 1.143 / 1.127 1.023 / 1.018
2 MB synthetic file 1.150 / 1.117 1.159 / 1.159
non-ASCII-heavy file 1.435 / 1.402 1.360 / 1.356
src/editor/gen-textmate.ts 0.995 / 1.024 1.144 / 1.141
typescript.ts 0.906 / 0.992 0.718 / 0.714

There monogram executed 26% to 69% more instructions per parse than oxc (1.42 times on checker.ts: 166.0M against 116.6M), measured again in the run above as its baseline.

These tables are hand-written from on-demand runs and are not regenerated. In reuse mode monogram makes no allocator call per parse and neither parser takes a page fault.

Building the AST directly against building the CST

TypeScript target; 2026-11-24; measured on the part of each real input that the shape covers: the outermost expression statements the shape builds, concatenated: checker.ts 11.4% and parser.ts 8.8% under the tsc expression shape, typescript.js 17.2% under the ESTree shape; V8 precise block counts of one parse (test/bench-targets/blocks.ts) and exact allocation counts (alloc-instrument). The fused shape-building parser (a construct plan per arm, slot positions fixed at generation time, one flat object literal per node under the keys the shape declares) allocates fewer objects than the CST build: checker.ts 159,507 against 177,232 (-10%, 5.52 against 6.15 MB), parser.ts 26,166 against 29,229 (-10.5%), typescript.js 868,500 against 871,876 (-0.4%, 27.1 against 28.9 MB). It still executes 3.6% (checker.ts), 3.8% (parser.ts) and 7.8% (typescript.js) more V8 blocks: the engine's share is the same, and the builder's share is 631k blocks against the CST builder's 474k on checker.ts. Part of that gap belongs to the slot-stack form: slot tags and iteration markers, and a missing-value check on each child read, which a partial shape needs and a total one does not. The rest is work an AST has and a CST does not: cooked strings (names and values; about 22k objects on checker.ts), attribute equations (tsc optional / constructorName; ESTree directive, optional, stringBody, constructorName), and, under ESTree's chainTop, one of the two variants built for each chain link and then discarded (64,555 pairs on typescript.js, 103 on checker.ts with the tsc shape). For comparison, before the fused plan (the uniform intermediate, S2) the shape build cost 24-36% more blocks and about twice the allocations. The like-for-like comparison with oxc (typed structs in arenas) is the Rust printer's.

input (shape) CST nodes + leaves shape nodes + lists allocations CST -> shape bytes CST -> shape V8 blocks CST -> shape
checker.ts, covered (tsc) 36,260 + 25,736 45,216 + 5,011 177,232 -> 159,507 (-10.0%) 6.15 -> 5.52 MB 4,139,750 -> 4,288,390 (+3.6%)
parser.ts, covered (tsc) 6,103 + 4,039 7,388 + 969 29,229 -> 26,166 (-10.5%) 0.99 -> 0.87 MB 658,496 -> 683,626 (+3.8%)
typescript.js, covered (ESTree) 182,718 + 128,787 202,392 + 22,745 871,876 -> 868,500 (-0.4%) 28.9 -> 27.1 MB 19,949,949 -> 21,506,871 (+7.8%)

Rust: the first typed build (2026-11-24, measure-profile run 45308730793)

The typed Rust AST build (src/targets/rust-shape.ts: one struct per node kind in its arena) against the CST build of the same tree, Callgrind instructions of one parse (run.sh phase shape, M only), on checker.cov.ts (checker.ts's 4,648 outermost expression statements the shape builds: 381 KB, 12% of the file) under the tsc expression shape's synthesized subset (test/fixtures/shape-tsc-synthesized.ts; 47,649 nodes built). The starting point, stated as measured: the first typed build costs 89% more than the CST build.

mode CST build Ir shape build Ir Δ
fresh 16,125,283 30,487,479 +89.07%
reuse 15,523,392 29,149,033 +87.77%

Where the extra 14.4M go (self instructions per function, fresh): the engine and the lexer are the same in both (Parser 8.96M against 9.09M; lexer 6.37M); Ast::node 4.56M (38k calls: a scratch swap, a match over ~400 engine arm ids, a 48-byte Slot push, and the plans LLVM inlined there); the other construct plans 2.08M; the typed constructors mk<k> 1.92M (a missing check per field, the FromW conversion, the arena push); identifier cooking 1.28M plus memchr 0.53M; allocator +1.27M and memcpy +0.72M (arena growth, Slot copies); value words 0.85M; FromW 0.36M. So the cost is the builder's per-node overhead (an interpreter of the construct plan: a runtime dispatch on the arm the accept site already knows) and identifier cooking, not the AST: the CST build writes a node per rule node and a leaf per token, the typed build 47.6k nodes and no leaves. The reductions follow in that order (direct per-arm construct calls at the accept sites, shared with the TypeScript target; a smaller slot; identifiers uncooked when the lexer saw no escape; constructors without checks a field's rule cannot need), each re-measured here.

Rust: the typed build against CST build + a converter (a bound, 2026-11-29, master 118bee29)

The shape row compares the typed build with the CST build. A user who wants typed nodes does not have the CST build alone, though. They have the CST build plus a converter that walks the CST and builds the same typed tree. That converter was not built, so this section gives a BOUND and an ESTIMATE for it, not a measured row. The Rust CST records each node's rule but not its arm. It also does not record the grammar tags, iteration markers or record positions that the typed constructs read, and on master it drops punctuation leaves. So any converter has to find each node's arm again by matching its children against the rule's arms, the way the reference evaluator does (src/shape/shape2-eval.ts).

The parts below come from the profile artifact of the merge that became 118bee29 (measure-profile run 46288269222, Callgrind per source line, checker.cov.ts). A one-off script over the artifact's Callgrind files split them; the script is not in the repository. The parts are:

  • P: shared work. Lexer and engine lines whose text is the same in both sources, plus each parser function's inlined-std, line-0 and allocator cost up to what the other build spends there.
  • C: work only the CST build does (CstBuilder, kids, nodes, EntryMeta, its own engine lines).
  • T1: typed-only work that ANY route to the same typed tree must do. This is the mk<k> struct initialisation, arena push and arena growth; list slab copies; text spans, escape scan and numeric cooking.
  • T2: typed-only work that exists only because the build goes through slots. This is W encode, FromW decode, construct plans reading slots, Slot scratch traffic beyond the CST build's handles, need! / commit! / tag sites, and the missing checks in mk<k>. The checks are modelled as 84,733 executed checks × 3 instructions and moved from T1 to T2.
mode P C T1 T2
fresh 14.72M 1.77M 3.76M 5.08M
reuse 14.14M 1.71M 2.75M 5.35M
mode CST build (measured) typed build (measured) CST + convert, lower bound CST + convert, estimate
fresh 16,466,918 23,575,738 20,226,891 22.6M-25.2M
reuse 15,865,035 22,219,465 18,612,975 21.0M-23.6M
typed build against it +43.2% / +40.1% +16.6% / +19.4% +4.3% to -6.3% / +5.9% to -5.7%

What each row answers:

  • CST build. What a parse costs a user who reads the CST.
  • Typed build. What a parse costs a user who wants the typed tree and gets it from the parser.
  • Lower bound. CST build + T1: a converter whose walk and arm matching cost nothing. No converter can cost less than this.
  • Estimate. The lower bound plus a walk and an arm rediscovery:
    • The walk is 37,939 nodes + 64,768 kid slots, at 15 to 40 instructions each. 40 is the measured per-token cost of tokenize's mapping pass over checker.ts; 15 is inferred.
    • The arm rediscovery is about 10 instructions per node plus 5 per child, with the first child choosing the arm, plus a re-read of the 26.7k punctuation tokens the CST drops. This is inferred.

So the typed build against the estimate answers "should a user who wants typed nodes use the typed build?". Today that comparison is about even. The typed build is 16.6% above the lower bound, and that excess is T2 - C, the slot overhead beyond the CST builder it replaces. The planned steps (L3a: check only the fields that can be missing; L3c: typed fields decoded where read; L3d: constructs at accept sites) all remove T2. No step can remove T1. The typed build's floor is P + T1 = 18.5M fresh, below the lower bound.

Since land/lossless-cst the CST build keeps the punctuation leaves (measure-profile run 46290960497): 16,610,344 instructions fresh and 16,122,636 reuse on checker.cov.ts (was 16,466,918 and 15,865,035), the typed build unchanged at 23,575,738 and 22,219,465, so the typed build is +41.9% / +37.8% against it (was +43.2% / +40.1%). A converter then reads every punctuation token from the CST and needs no re-read of the text; the parts above were not split again.

Recorded result: element dialects (TSX and JSX)

measure-profile run 45129600578 (AMD EPYC 7763), M = master d326d65. Per token: Rust and Go Callgrind instructions of one parse (reuse mode) and of the Rust lex(src) alone; TS: V8 block executions of one parse(src).

input (grammar) tokens Rust lexer Rust parse Go direct Go builder TS blocks
checker.ts (typescript) 348,866 142.3 298.9 463.0 491.2 87.7
gen-textmate.ts (typescript) 50,156 158.6 329.8 525.9 557.4 114.7
jsx-corpus.tsx (typescriptreact) 37,054 203.7 433.5 755.8 784.5 130.9
control: gen-textmate.tsx (typescriptreact) 50,156 163.5 334.5 534.2 553.5 114.8
control against gen-textmate.ts +3.1% +1.4% +1.6% -0.7% +0.06%

How to read it:

  • A TSX token costs the Rust lexer 45 instructions more than a gen-textmate.ts token; about 5 of them are the element machinery's overhead on code (the control) and the rest is the token mix: text runs, tags and holes, which the frame step lexes.
  • The control's Rust lexer +3.1% is no element work: Callgrind's per-line attribution puts it on register moves at the merges of the lexer's loops, not on an element line. Before the plain rounds ran in an inner loop of their own (25b9ec7), the control cost +18.1% Rust lexer, +8.6% Rust parse, +25.1% Go direct and +21.5% Go builder (measure-profile run 45126133065, A = 11a7c70, AMD EPYC 9V45). The Rust lexer counts of gen-textmate.ts and gen-textmate.tsx are the same in both runs.
  • That loop's one known cost: on element-dense JSX (jsx-corpus.jsx) an ordinary-scan round inside an element goes from the element loop to one plain round and back: +1.2% Rust lexer, +0.5% Rust parse against the single-loop lexer (run 45126133065).

The Go, TS and JS targets have not been compared with another parser on full TypeScript (an earlier Go-vs-tsgo comparison used the JavaScript grammar on repeated snippets, and its harness is gone). What each target allocates per parse is reported in ALLOCATIONS.md; measure-targets times them A/B on demand.