Repository navigation
perf: remove four parse costs that grew with the square of template length - #323
Merged
Merged
Conversation
…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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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: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.finalize_nodecopied the tag's fullused_variableslist 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.finalize_nodebuilt 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 samecontains_fillsvalue, computed from its children's flags.validate_fill_exclusivitysearched 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.
<br><br><c-if>blocksDebugoutput 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.contains_fillsassertion.test_positions_after_crlf_non_ascii_and_in_nested_templateslocks positions after\r\n, after a lone\r, after multibyte characters, and inside a nested template attribute.test_contains_fills_reaches_through_nested_control_flowlocks the flags two control-flow levels deep.scripts/check.py --profile fastpassed at 40914ad10 (767 s), with citry_core built from that commit right before the run. The final commit changes only the crate'sdocs/agent/INDEX.mdwording.mypy --platform linuxpasses.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.
ast.rsfrom_start_and_end_tags,parser.rsfinalize_node).used_variableslist.validate_template_bindingscopies the active loop and fill bindings for every node.#c-ignoreelements 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