generate: quadratic memory blowup in auxiliary-symbol tracking

Author: nzinfoCreated Sep 18, 2026Updated Sep 30, 2026
Labelsparser-generationperffix-available

Problem

tree-sitter generate has a quadratic memory blowup in auxiliary-symbol tracking (AuxiliarySymbolSequence in build_parse_table.rs), present unchanged on current master (5b951ef).

On a large real-world grammar (SysML v2, ~320 rules, ~450 keyword-like tokens, start-state closure of 114,088 items), stock v0.27.0 generate peaks at 42.4 GB RSS / 37min46s. Two compounding causes:

  1. In add_actions, for every item whose next symbol is an auxiliary variable, the entire (heavily duplicated) parent_symbols: Vec<Symbol> is cloned onto the sequence — ~1.5 billion Symbols cloned while processing the single start state.
  2. The accumulated sequence is cloned wholesale into every successor state's queue entry, making the blowup persistent across ~59k states.

Fix (ready on branch)

Branch: nzinfo:fix-aux-symbol-memory-master (on master 5b951ef)

  • Vec<AuxiliarySymbolInfo> → FxHashMap<Symbol, AuxiliarySymbolInfo> (last-write-wins; the sequence is only ever queried by "most recent entry for symbol X" in handle_conflict)
  • parent_symbols deduped at construction, stored as Rc<[Symbol]>
  • Includes regression test test_generate_with_dense_inlined_repeats (stock: 1.03 GB peak; fixed: 11.3 MB, byte-identical output)

Measured on SysML v2: 42.4 GB → 726 MB, 37min46s → 15min03s, and parser.c/grammar.json/node-types.json byte-identical to stock. Java/Rust/C++ outputs also byte-identical (v0.27.0 and master); corpus suites unchanged; cargo fmt/clippy clean; generate tests 85/85.

Bonus: the profiling tool used to find this

Branch: nzinfo:generate-mem-prof (independent, on master) adds opt-in TREE_SITTER_GENERATE_PROFILE=1 phase-level time/RSS reporting + throttled structural census of the parse-table builder — zero-overhead when disabled, documented in docs/src/cli/generate.md. Byte-identical outputs; 84/84 tests.

I can't open PRs (repo limits PR creation to collaborators) — happy to open both if given the ability, or feel free to review/cherry-pick the branches directly.

Disclosure per the AI policy: the investigation and patches were developed with AI-assistant tooling under my direction; I reviewed the analysis, verified all measurements and byte-identical outputs myself, and take responsibility for the changes.