0009: Syntax analysis limits and keystroke budget
- Status: Proposed (implemented in the MVP)
- Date: 2026-12-05
Context
Measurements (performance) showed that Tree-sitter trees cost ~40 bytes per source byte (~84 MiB for a 2 MB Rust file) and that incremental reparses of very large flat files or broken code take 10–50 ms, exceeding a 60 Hz frame on the UI thread.
Decision
- Parse only documents up to 1 MiB; larger files open as plain text and the status bar says why.
- Give each keystroke a 6 ms reparse budget. On overrun, keep the previous tree (already shifted by the edit) so highlighting stays usable, mark it stale, and finish the parse on the next frame without input.
- Bound the initial parse at 500 ms.
Alternatives considered
- No limit: one generated file could cost hundreds of MiB and seconds of freezes.
- Debounce all parsing: hides cost but makes highlighting lag even for small files, where parsing takes ~0.2 ms.
- Background parsing now: the right long-term answer (backlog P0-3) but needs snapshot and stale-result handling; the budget delivers most of the benefit with little code.
Consequences
- Typing frame CPU p95 dropped from ~24 ms to ~8.5 ms on a 100 KB file with broken code; 900 KB files stay under 13.3 ms max.
- Files over 1 MiB lack highlighting until background or viewport-limited parsing lands.
- Opening a file near the limit still blocks for ~0.1 s.
Validation
Unit tests for over-budget parses keeping the old tree; session tests for large files and deferred parses; axiom-perf and axiom --perf-typing measurements.