Skip to content

perf: remove four parse costs that grew with the square of template length - #323

Merged
JuroOravec merged 1 commit into
mainfrom
fix/linear-parse
Oct 24, 2026
Merged

JuroOravec merged 1 commit into
mainfrom
fix/linear-parse

Conversation

@JuroOravec

Copy link
Copy Markdown
Member

Part of #272. This PR removes four parse costs that grew with the square of the template's length. The nesting limits for #272 come in a later PR.

What changes

Parsing a large template took time that grew with the square of its length. Four causes, each fixed in citry_template_parser:

  • Line and column. Every token asked pest for its line and column (pest::Position::line_col), which walks the source from the start each time. A line index (src/utils/line_index.rs) is now built once from the whole template string, and shared with the parses of nested template attribute values. Each lookup is a binary search. It gives the same answers as pest: lines end after each \n, and columns count characters.
  • Used variables. When a tag closed, finalize_node copied the tag's full used_variables list into its parent. The recompute pass (recompute_template_used_variables), which runs once the tree is complete, rebuilds those lists anyway. Nothing reads them before it, so the copy is gone.
  • The fill list. finalize_node built the full <c-fill> list for every closed tag, walking every nested control-flow tag again at each level. Only component tags check their fills, so only they build the list now. Every node still gets the same contains_fills value, computed from its children's flags.
  • The sibling search. validate_fill_exclusivity searched all earlier siblings for a <c-fill> each time a tag closed. The parser now keeps a flag for each open body that says whether it already holds a fill.

The AST does not change.

Evidence

Release build, macOS arm64, parse only, median of 11 samples. "Flat" means sibling tags with no nesting.

Input Before After
4,000 sibling <br> 74 ms 1.9 ms
8,000 sibling <br> 292 ms 3.9 ms
4,000 sibling <c-if> blocks 1.5 s 11 ms
92 KB flat template 1.3 s 10 ms
184 KB flat template 5.2 s 21 ms
368 KB flat template not run 44 ms
128 docs-site and benchmark templates, total 88 ms 6.1 ms
  • Speed per template. I compared the minimum of three interleaved runs on each side for the 128 templates. Only one reads more than 5% slower: an 84-byte template at 1.066. In six more alternating runs it took 6.4 to 7.3 µs after the change against 6.6 to 7.5 µs before, so that reading is noise.
  • Same AST. For those templates, two synthetic files, and a sample with fills inside control flow, comments, slots, CRLF, a lone CR and non-ASCII text, the Debug output of the parsed AST is byte-identical before and after. This includes parse errors. I ran this check before the sibling-flag change, which touches only validation.
  • Tests.
    • The 542 crate tests pass, including every locked position and contains_fills assertion.
    • New: a test checks the line index against pest at every offset of 300 random strings (fixed seed), at checkpoint spacings from 1 to 256 bytes, plus long lines.
    • New: test_positions_after_crlf_non_ascii_and_in_nested_templates locks positions after \r\n, after a lone \r, after multibyte characters, and inside a nested template attribute.
    • New: test_contains_fills_reaches_through_nested_control_flow locks the flags two control-flow levels deep.
  • Fast gate. scripts/check.py --profile fast passed at 40914ad10 (767 s), with citry_core built from that commit right before the run. The final commit changes only the crate's docs/agent/INDEX.md wording. mypy --platform linux passes.

Costs that still grow faster than the length

The AST contract requires these, and they grow with nesting depth, not with length alone. A nesting limit is planned for #272 to bound them.

  • Each node and body stores every comment and slot in its subtree, so those lists are copied up at each close (ast.rs from_start_and_end_tags, parser.rs finalize_node).
  • The recompute pass writes each node's full used_variables list.
  • validate_template_bindings copies the active loop and fill bindings for every node.
  • Nested #c-ignore elements re-walk their subtree (find_ignored_content_problem), and so does a chain of control-flow tags mixed with fills (_contains_only_fills_and_control_flow).

🤖 Generated with Claude Code

…ength

Parsing a large template took time that grew with the square of its
length: 4,000 sibling <br> tags took 74 ms, and a 92 KB template of
sibling tags with no nesting took 1.3 s (184 KB: 5.2 s). Four causes,
each fixed:

- Every token asked pest for its line and column, and pest walks the
  source from the start each time. A line index, built once from the
  whole template string and shared with nested template parses, now
  answers each lookup with a binary search. A test checks it against
  pest at every offset of 300 random strings (fixed seed) that mix
  multibyte characters and every kind of line break, at several
  checkpoint spacings, plus long lines.
- `finalize_node` copied each closed node's full `used_variables` list
  into its parent. The recompute pass (`recompute_template_used_variables`)
  rebuilds those lists once the tree exists, and nothing reads them
  before it, so the copy is gone.
- `finalize_node` built the full fill list for every closed tag, walking
  every nested control-flow tag again at each level. Only component tags
  check fills, so only they build it now. Every node still gets the same
  `contains_fills` value, computed from its children's flags.
- `validate_fill_exclusivity` searched all earlier siblings for a fill
  each time a tag closed. The parser now keeps a flag per open body that
  says whether it already holds a fill.

After (release build): 4,000 <br> parse in 1.9 ms, 92 KB in about
10 ms, 184 KB in 21 ms, 368 KB in 44 ms, and 4,000 sibling <c-if> in
11 ms instead of 1.5 s. The parsed AST for every docs-site and benchmark
template is byte-identical to before, and none parses more than 5%
slower.

Part of #272.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@JuroOravec
JuroOravec merged commit 4c91470 into main Oct 24, 2026
30 checks passed
@JuroOravec
JuroOravec deleted the fix/linear-parse branch October 24, 2026 15:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant