# perf — measured numbers for the paths a keystroke and a hover run through ## Goal Give the server's hot paths — `analyze`, opening a buffer, answering a hover, reindexing one saved file, and (since round 2) one flush stage by stage and one keystroke's round trip over the wire — a number that is *measured* rather than guessed, taken the same way every time, so a later round can say whether it made something faster or only different. This is the record; the measurements themselves are `#[ignore]`d tests in `crates/lucisv_server/src/perf.rs` and in `crates/lucisv_server/tests/e2e.rs`, run on demand and asserting nothing about duration. Nothing here is a performance *budget*: a test that fails when a machine is busy is a test that gets deleted. ## Assumptions - **No benchmark dependency.** `criterion` and friends are out: `CLAUDE.md` requires a written justification for any new dependency, and the workspace has six direct ones (`rayon`, `lsp-server`, `lsp-types`, `serde`, `serde_json`, plus their pins). `std::time::Instant` + `eprintln!` is the shape this repo already uses for perf notes (`hover.rs`'s QA measurement, `includes.rs`, `inlay.rs`, `file_ops.rs`) and it is what is used here. - **`--test-threads=1` or the numbers are noise.** The harness otherwise runs these concurrently with each other and with 1 200 other tests. The first run of these measurements was taken without it and reported a cross-file hover as *cheaper* than the single file read it performs; that is how obvious the contamination is. - **Means, not single runs.** Each measurement is the mean of 10–1 000 runs (`perf::mean`), with `std::hint::black_box` around the inputs and results so a release build cannot optimize the work away. - **Generated input, not the corpus.** `perf::generated_module` builds an ANSI-headed module of repeated four-line blocks (a wire, an assign, a register, an update) at 1 000 and 10 000 lines. It is uniform, which real RTL is not — the numbers are a scale, not a prediction for any given file. The corpus-scale numbers this repo already has live where they were taken (`docs/features/incremental-index`, `index-persistence`, `references-nav`). ## Reproducing ```text cargo test --release -p lucisv_server perf:: -- --ignored --nocapture --test-threads=1 ``` Each test's own doc comment names the narrower filter for running it alone. The over-the-wire measurement lives with the other end-to-end tests, because it spawns the shipped binary, so it has its own command: ```text cargo test --release -p lucisv_server --test e2e a_keystroke -- --ignored --nocapture --test-threads=1 ``` The one older measurement quoted below has a third: ```text cargo test --release -p lucisv_server large_closed -- --ignored --nocapture --test-threads=1 ``` ## The machine | | | |---|---| | CPU | Apple M1 Max (`uname -m` = `arm64`) | | OS | macOS 27.0 (build 26A5421a) | | build | `--release`, workspace at 0.12.1 + rounds 1 and 2 uncommitted | | date | 2026-08-28 (every figure below re-measured this day, round 2) | ## Numbers All figures are the mean of the runs named above. "1k" and "10k" are the generated module's line counts (29 KB / 501 symbols and 309 KB / 5 001 symbols respectively). | what | 1k lines | 10k lines | |---|---|---| | `vlog_semantic::analyze` (tree already parsed) | 252 µs | 3.76 ms | | `Document::new` — lex + parse + analyze + `resolve_external` | 2.54 ms | 31.1 ms | | `hover`, target declared in the same open buffer | 2.68 µs | 1.10 µs | | `hover`, target in a **closed** file (disk path) | 38.1 µs | 159 µs | | — of which one read + one `LineIndex` of that file | 27.4 µs | 140 µs | | `WorkspaceIndex::update_file`, 20 files of this size | 2.76 ms | 32.1 ms | | — of which the clone the measurement needs to repeat itself | 60 µs | 567 µs | | one name looked up among the file's symbols: **linear scan** | 767 ns | 8.14 µs | | one name looked up among the file's symbols: **name-sorted lookup** | 93 ns | 131 ns | | one token found at the cursor: **sweep** | 6.58 µs | 77.5 µs | | one token found at the cursor: **binary search** | 15 ns | 20 ns | | the balanced-paren window `completion.rs` needs: **sweep** | 8.63 µs | 130 µs | | the balanced-paren window `completion.rs` needs: **binary search** | 27 ns | 36 ns | Every figure above comes from one run of the command in *Reproducing*, on the machine above. A local hover is a handful of pointer chases and comes out under 3 µs at both sizes; the 1k figure being the larger of the two is that measurement's noise floor, not a result. Three things stand out: - **A hover into a closed file is a file read.** 27 µs of the 1k hover's 38 µs, and 140 µs of the 10k hover's 159 µs, is reading that file and building a `LineIndex` over it. Everything else — resolution, guards, rendering — is single-digit microseconds. - **`Document::new` is where a keystroke's cost lives**, and it is dominated by lexing and parsing, not by `analyze` (31 ms against 3.8 ms at 10k lines). The stage-by-stage table below says exactly how much. - **A sweep of the token vector costs about what the parse of one line costs**, and there are several per keystroke. That is round 2's Phase 2. ### One flush, stage by stage `Document::new` runs the first seven of these in order; the eighth is not inside it at all — `diagnostics::all` is what a *publish* runs, on every flush, and round 1's table did not account for it. `parse_tolerant` consumes its token vector, so each run of the parse needs a fresh clone. The clone has its own row and the parse row still contains it; subtracting one from the other is the honest reading, and it is what `Document::new` really pays. | stage of one flush | 1k lines (9 461 tokens) | 10k lines (94 961 tokens) | |---|---|---| | `tokenize` | 1.14 ms | 12.5 ms | | `tokens.clone()` (the copy `Document` keeps) | 230 µs | 2.40 ms | | `parse_tolerant`, clone included | 1.16 ms | 14.3 ms | | — the parse alone, by subtraction | 0.93 ms | 11.9 ms | | `collect_defines` | 7.09 µs | 121 µs | | `analyze` | 278 µs | 5.55 ms | | `resolve_external` | 943 ns | 49.3 µs | | `LineIndex::new` | 10.5 µs | 115 µs | | `diagnostics::all` (parse errors + closers + lint) | 104 µs | 1.40 ms | Read this table as *shares of a flush*, not as the last word on any one stage: it runs eight measurements back to back on one heap, and its `analyze` row came out 5.55 ms in the same run where the dedicated `analyze` test — which measures nothing else — came out 3.76 ms. The dedicated figure is the cleaner one. What the table is for is the proportion, and the proportion is unambiguous: - **Lexing and parsing are 79 % of a flush** at 10 000 lines (12.5 + 11.9 of 30.9 ms, taking the dedicated `analyze` figure for the rest). Everything else together is a fifth. - **`tokens.clone()` is 7.7 %**, and it is load-bearing rather than waste: `parse_tolerant` takes ownership of its tokens and *steals the comments out of them*, so the copy `Document` keeps is the only one that still has its trivia. It is also already the cheap option — before the one-lex change this was a second `tokenize`, which is 12.5 ms. - **`resolve_external` is 49 µs, one part in six hundred.** Making the workspace overlay skippable was on this round's list of candidates; the measurement says there is nothing there to save. - **The lint pass is 1.40 ms on top of the flush**, about 4 %. It walks the tree once per rule. ### Over the wire — what a keystroke actually costs a user Round 1 listed this as its own open TODO, because it cannot be taken from inside the crate: the number includes two pipes, a JSON encoder and decoder, the change debounce's quiet-period timer and a whole flush. It is measured by spawning the shipped binary and speaking framed JSON-RPC at it (`tests/e2e.rs::a_keystroke_round_trip_over_the_wire`, command above). | `didChange` → `publishDiagnostics` | 1k lines | 10k lines | |---|---|---| | one keystroke, nothing following it | 164 ms | 202 ms | | a burst of ten keystrokes, no pause | 165 ms | 207 ms | | — publishes that burst produced | **1** | **1** | | one keystroke **a request follows** (mean of 10) | 4.35 ms | 37.6 ms | The three rows say three different things. - **The debounce is the felt latency when a client asks for nothing.** 150 ms of the 164 ms is `CHANGE_DEBOUNCE`'s quiet period; the work is the rest. At 10 000 lines the same 150 ms sits on top of a 31 ms flush. This is the one row where round 1 made something *slower*: before the debounce a lone keystroke published in about one flush, and it now waits out the timer first. It is the bargain debouncing is, and the last row is why it is worth taking. - **A real editor never waits for that timer.** VS Code asks for semantic tokens, inlay hints and highlights the moment a buffer changes, and a request flushes the dirty buffer before it is answered. That is the last row, and it is the number a user actually feels: **4.35 ms at 1 000 lines, 37.6 ms at 10 000**. - The first two rows are single round trips, not means, and each is bounded below by a 150 ms timer; treat their differences as noise. The last row is the mean of ten and is the one worth comparing across rounds. ## Before / after — round 1 (2026-08-28) **A1 — didChange debounce.** Not measured directly *at the time*, and the arithmetic below is what round 1 recorded: every `didChange` used to run one `Document::new` plus lint and publish; a burst of *k* changes now runs one. At 10k lines that is 31 ms saved per keystroke after the first in a burst, at 1k lines 2.6 ms. What the debounce cannot save is the first change in a burst and the last flush, so the floor is one `Document::new` per burst per buffer. **Round 2 measured it.** A burst of ten keystrokes over the wire produces **one** `publishDiagnostics`, counted rather than reasoned about — the count comes from draining the session at `shutdown`, which every pending flush is guaranteed to have arrived before. The arithmetic above was right. The burst is deliberately the **last** thing that measurement does, which is QA finding IL-1 closed: every `wait_for` in the test accepts *any* publish, so while the burst ran before the other rows, a second publish it had produced would have been absorbed by the next row's wait and never counted. With nothing between the burst and the drain, the count is the count. **A2 — one disk read per hover.** Measured, two ways. - The QA round that filed the finding measured a hover into a 31.7 MB closed file at **34 ms** (`docs/features/hover-declarations/README.md`, finding 4, 2026-08-27, same machine, same test, release build). The same test after the `disk_view` memo: **16.3 ms**. That is the expected shape — two reads and two `LineIndex` builds became one — but the two numbers come from different runs on different days, so read it as "about half", not as 2.08×. - The `perf::hovering_across_files_into_a_closed_file` measurement takes the after-number and the cost of one read + `LineIndex` side by side, in the same run: 142 µs of a 160 µs hover at 10k lines. Removing a second read of that same file is therefore removing roughly the whole remaining cost of the request, which is the before/after in the form that does not depend on an old number. **A3 — name-keyed symbol lookup.** Measured directly, on the lookup alone: 8.41 µs → 133 ns at 10k lines (5 001 symbols), 763 ns → 106 ns at 1k. The lookup is only part of what was saved: in `hover::live_symbol` every symbol the scan used to reach was also an `indexed_def_for_symbol` call — two index lookups apiece — which the name-sorted lookup never makes. That larger saving is **not measured here**; the 8.41 µs → 133 ns figure is a floor on it, not the whole of it. **B — port detail spans, and comment attachment.** Not measured. The work added is a second `fenced_sections` call over lines already in memory, and two integers per declaration in the index and its facts records. No measurement was taken and none is claimed. ## Before / after — round 2 (2026-08-28) **C1 — six token lookups stopped sweeping the buffer.** Measured directly, both halves in the same run against the same token stream (`perf::finding_one_token_against_sweeping_the_stream`), with the cursor three quarters of the way down the file — a sweep costs the distance from token zero, so measuring at either end would flatter it. | | 1k lines | 10k lines | |---|---|---| | the token at the cursor: sweep → binary search | 6.58 µs → 15 ns | 77.5 µs → 20 ns | | `balanced`'s window: sweep → binary search | 8.63 µs → 27 ns | 130 µs → 36 ns | Six call sites across `selection.rs`, `on_type.rs`, `completion.rs` and `inline.rs`; which ones, what each of them wanted, and the per-site argument that the search returns exactly what the sweep returned are in `docs/features/interactive-latency/README.md`. Every one is pinned by a test that walks **every byte** of a buffer full of touching tokens and compares the two. None of them changed an answer, and the tests are what says so rather than the claim. Three sweeps were left alone with the reason written down: `signature.rs`'s two paren-depth walks (a depth is a function of every bracket before the cursor), `pairs.rs::analyze` (not a position query at all), and `completion.rs`'s comment gate (comments are trivia attached to whichever token happens to be next, so no search of the stream answers "is this byte inside a comment"). That last one is the remaining per-keystroke whole-buffer walk on the completion path. **C2 — the round trip, and nothing else changed.** No stage of a flush was touched this round, so the flush table above is a baseline rather than an after. The reasons each candidate was refused, with its number, are in the next section. ## What it would take to go faster Ranked by what the measurements above say is there, with what each would cost. None of these were attempted; each is a future round. 1. **Incremental lexing and parsing — 24.5 ms of a 31 ms flush at 10 000 lines (79 %).** This is the only large lever left. It is also the one change that runs straight into `CLAUDE.md`'s hard invariants 1 and 2: the parser is what the "never corrupt code" and idempotency guarantees rest on, and an incremental parser that is right on 99 % of edits is worse than none. The change debounce already removed most of the *repetition* per keystroke, which is why this was not attempted here. The number that would justify it: **37.6 ms**, the felt latency of one keystroke in a 10 000-line buffer, of which roughly 30 ms is lex + parse. 2. **A cheaply-clonable `Token` — 2.40 ms, 7.7 % of a flush.** `Document.tokens` must be a separate copy because `parse_tolerant` takes ownership and steals `leading`/`trailing` out of the tokens it parses; the copy is 94 961 `String` allocations. Interning the text, or an `Rc`, would remove most of it — but `Token` is `vlog_parser`'s public type and the formatter reads it everywhere, so this is a cross-crate change for under 8 %. 3. **One tree walk for the whole lint pass — 1.40 ms, about 4 % on top of a flush.** `vlog_linter::lint` is `for rule in enabled { rule.check(ctx) }`, one walk per rule, and the same engine serves `lucisv check`. Fusing them is a `vlog_linter` refactor, and the editor and CI must not disagree about what a violation is. 4. **`completion.rs`'s comment gate**, the last whole-buffer walk on a per-keystroke request path. Not measured on its own; it is one pass over the token vector plus its trivia, so the sweep figures above (77.5 µs at 10 000 lines) are the right order of magnitude for it. Doing it properly means giving the lexer a way to answer "which comment covers this byte", which is a `vlog_parser` question and not a `completion.rs` one. 5. **`CHANGE_DEBOUNCE` itself** is not a code problem: 150 ms of a lone keystroke's 164 ms round trip is the timer. It only bites clients that ask for nothing after a change, and it was set deliberately. Now that it is measured, a future round has the number to argue about. ## Status / TODO - [x] `crates/lucisv_server/src/perf.rs` — eight `#[ignore]`d measurements, std only, each with the command to run it. - [x] `analyze`, `Document::new`, hover (local + closed cross-file), `WorkspaceIndex::update_file`, at 1k and 10k lines. - [x] Before/after where it was cheap: A2 (two ways), A3 and C1. - [x] Machine, command and date recorded above. - [x] **Round 1's first open TODO, resolved (round 2, 2026-08-28):** end-to-end request latency over the wire, measured against the shipped binary — a single `didChange`, a burst of ten (with the publish count that turns A1's arithmetic into a measurement), and the change-then-request shape a real editor produces. `tests/e2e.rs::a_keystroke_round_trip_over_the_wire`. - [x] **Added in round 2:** one flush stage by stage, including the `diagnostics::all` pass that runs on every publish and that round 1's table did not account for. `perf::breaking_down_one_flushed_keystroke`. - [ ] **Round 1's second open TODO, restated and still open:** memory. Nothing in the repo measures resident size. Round 2 added no data structure — two free functions and no state — so the figure it would report has not moved, and round 1's name index is still four bytes per symbol. Worth doing before anything interns tokens, which is item 2 of *What it would take to go faster* and is a memory trade as much as a time one. - [ ] Not measured: the per-request handlers themselves (hover aside). Completion, signature help and semantic tokens each have a whole-buffer pass somewhere; only their token lookups were measured this round, not the requests end to end. ## Work log **2026-08-28 — dev.** Wrote `perf.rs` and took the numbers above (part A4 of the LSP performance round). Chose one module of six tests over a test per crate so that a single command produces the whole table. The generated-module generator lives beside the tests rather than in a shared helper — it has one user. Renamed `hover.rs`'s QA measurement `hovering_into_a_large_closed_file_reads_it_twice` → `..._reads_it_once`, since the doubled read it was named for is what A2 removed. The QA log in `docs/features/hover-declarations/README.md` still names the old spelling; that section belongs to QA and was left alone. **2026-08-28 — dev (QA fixes).** No number above moved, and none was re-measured, because nothing on a measured path changed shape: - The hover fixes (PDS-1, PDS-2, PDS-4) are all inside `attached_lines`, which part **B** already records as unmeasured. The added work is a `strip_suffix` per composed line and a below-comment walk that now also runs when a comment sits above — a handful of `line_range` lookups against the 142 µs *file read* that dominates the "hover closed" figure. - CD-1 only *removes* work (a closing buffer's `Document::new` and publish), and the debounce's own A1 numbers are arithmetic over `Document::new`, which is untouched. - PDS-3 widens one `bind_port_decl` branch from ports to task/function arguments: one hash lookup per declarator of a body port declaration inside a task or function. `perf.rs`'s generated modules contain no tasks, so the `analyze` / `Document::new` / `update_file` figures cannot have moved at all. **2026-08-28 — dev (round 2).** Added `perf::breaking_down_one_flushed_keystroke`, `perf::finding_one_token_against_sweeping_the_stream` and `tests/e2e.rs::a_keystroke_round_trip_over_the_wire`, and **re-measured every figure in this document** on the same machine from the commands written beside them — the tables above are one run of each, not a mix of runs across days. The old figures are gone rather than kept alongside; every pair was within the noise this document already budgets for, and carrying two columns of the same measurement would invite reading a difference into scheduling. Round 1's figures are not lost — the QA log below quotes all of them in its own table, dated, and that section belongs to QA and was left alone. The round's changes are in `docs/features/interactive-latency/README.md`. Only C1 touched shipped code, and it touched no stage of a flush, so the flush table is a baseline for the next round rather than an after. Two things are recorded here that a future round should not have to rediscover. First, the e2e measurement reuses `e2e.rs`'s existing `Live` session helper; the only shared code that changed is that `Live::finish` now returns the publish count it saw on the way out, which is what makes the debounce's coalescing countable. Second, `perf::generated_module` now has a copy in `tests/e2e.rs` — a binary crate's modules are not reachable from an integration test — and the two must stay the same shape or the in-crate table and the over-the-wire table stop being readable against one another. ## QA log **2026-08-28 — QA.** No findings — every number reproduces within reason. - **Machine match confirmed.** `sysctl -n machdep.cpu.brand_string` → `Apple M1 Max`; `sw_vers` → macOS 27.0, build `26A5421a` — exactly the machine named in the README's own table, on the same checkout. - **Re-ran the exact reproduce command**, `cargo test --release -p lucisv_server perf:: -- --ignored --nocapture --test-threads=1`, and compared every figure against the table: | what | table | re-measured | |---|---|---| | `analyze`, 1k / 10k | 274 µs / 3.79 ms | 287.6 µs / 3.806 ms | | `Document::new`, 1k / 10k | 2.60 ms / 31.1 ms | 2.600 ms / 31.72 ms | | hover local, 1k / 10k | 1.79 µs / 0.97 µs | 3.35 µs / 1.01 µs | | hover closed, 1k / 10k | 35.2 µs / 160 µs | 34.4 µs / 172 µs | | — one read of it, 1k / 10k | 26.9 µs / 142 µs | 26.7 µs / 147.6 µs | | `update_file`, 1k / 10k | 2.73 ms / 31.4 ms | 2.695 ms / 32.45 ms | | — the clone, 1k / 10k | 60 µs / 528 µs | 60.7 µs / 516.7 µs | | linear scan, 1k / 10k | 763 ns / 8.41 µs | 793 ns / 8.21 µs | | name-sorted, 1k / 10k | 106 ns / 133 ns | 93 ns / 134 ns | Every pair is within the noise the README itself budgets for — including the hover-local 1k figure, which the README already calls out by name as "that measurement's noise floor, not a result" (mine is higher still, 3.35 µs vs. 1.79 µs, and still a handful of pointer chases either way). Nothing here is scaled, remembered, or overclaimed. - **Re-ran the older, separately-commanded measurement** too: `cargo test --release -p lucisv_server large_closed -- --ignored --nocapture --test-threads=1` → **17.0 ms** against the documented "34 ms → 16.3 ms." Matches the README's own honesty caveat exactly ("read it as 'about half', not as 2.08×" — different runs, different days). - **`perf.rs` measures what its name says** — spot-checked `hovering_across_files_into_a_closed_file` and `looking_up_one_name_against_scanning_every_symbol` against `hover.rs`'s and `lib.rs`'s actual entry points (`hover::hover`, `Semantics:: symbols_named` vs. a hand-rolled linear scan over `symbols()`) — both call the real, shipped functions, not a stand-in. - **A1's "before/after" is correctly labeled "not measured directly"** — it is arithmetic over the already-verified `Document::new` figures, and the README says so; no separate claim to check. - **B's "not measured, none is claimed"** — confirmed true: no perf test exercises `fenced_sections`/`attached_lines`, and the README does not say one does. **2026-08-28 — QA (round 2).** Findings and the full gate output are recorded in `docs/features/interactive-latency/README.md`'s QA log, which is this round's owning feature; summarized here since this document is where the numbers live. - Every figure in *Over the wire* and *One flush, stage by stage* reproduced within the noise this document budgets for, from a fresh `cargo test --release -p lucisv_server perf:: -- --ignored --nocapture --test-threads=1` and `cargo test --release -p lucisv_server --test e2e a_keystroke -- --ignored --nocapture --test-threads=1` on this session's run of this machine. - **C1's identity claim** (six sweeps → binary searches, none changed an answer) held under a hand-traced check of all six call sites, including the empty-token and large-file-fast-path edges, and under an independent check of the `state.rs` monotonicity precondition against `vlog_parser`'s own much wider corpus/fuzz tests (`tests/spans.rs`, `tests/error_recovery.rs`) — no gap found. - **One methodology finding, IL-1 (medium)**, on the e2e round trip's burst measurement: the "1 publish(es)" figure for a burst of ten is not the order-independent count its own doc comment claims — a stray extra publish in the window between the burst and `Live::finish`'s drain could be silently absorbed by an unrelated `wait_for` later in the same test and never counted. Full detail and repro in `docs/features/interactive-latency/README.md`. Not a claim about a real double-publish (the debounce is understood to coalesce to one), and not fixed — closing it is a test-restructuring decision for the dev. - The two `generated_module` copies (this crate's `perf.rs` and `e2e.rs`'s) differ by one trivia line, not the byte-identical shape their own comment implies; verified harmless (comments are not tokens, and the tables stay readable against each other). Detail in the same QA log. - **Verdict: SHIP WITH FIXES noted, none applied.** ## Review **2026-08-28 — lead.** Accepted as the round's record. The discipline here is what makes the numbers usable later: one command, one machine block, means rather than single runs, `--test-threads=1` justified by a contaminated first attempt that reported a cross-file hover as cheaper than the file read it performs. Refusing criterion was the right call against `CLAUDE.md`'s dependency rule, and the `#[ignore]`d-measurement pattern was already this repo's idiom rather than an invention for this round. I re-ran every measurement independently and the figures reproduce. Two claims were softened at review rather than left as written: the wire-latency rows are re-measured after the IL-1 methodology fix (164/202 ms lone, 4.35/37.6 ms with a request following), and the lone-keystroke row now says plainly that round 1 made that case *slower* — 150 ms of it is the debounce timer, and a client that asks for nothing after a change waits it out. A perf document that only records wins is not a perf document. What I want the next round to inherit: the felt-latency number is **37.6 ms** for one keystroke in a 10 000-line buffer, roughly 30 ms of it lex and parse. That is the figure any incremental-parsing proposal has to be argued against, and it now exists.