Skip to content

refactor(parser): walk the template tree with a work list, not per-level recursion - #520

Merged
JuroOravec merged 5 commits into
mainfrom
refactor/explicit-stack-template-walks
Oct 28, 2026
Merged

JuroOravec merged 5 commits into
mainfrom
refactor/explicit-stack-template-walks

Conversation

@JuroOravec

Copy link
Copy Markdown
Member

Part of #300. Two kinds of walks over a parsed template now keep their own list of what is left to visit, so their stack use does not grow with nesting depth: the parser's passes over the finished tree, and copying, comparing and freeing a Template. Until now, each nesting level called the same function again and went through the stack guard (crate::stack::with_enough_stack).

The results stay the same, and so does the order. A pass that visited parents before children still does. A pass that needs its children's results first still visits children first.

What changed

  • Parser passes (parser.rs): find_ignored_content_problem, _contains_only_fills_and_control_flow, validate_fill_group_content, recompute_template_used_variables, collect_template_binding_tokens, validate_template_bindings and extract_fill_nodes. The two near-identical branches of recompute_node_used_variables are merged into one.
  • Template (ast.rs): Clone, PartialEq and Drop keep their own stack.
    • A body with no nodes, the usual case when Python reads a node's body, skips the list.
    • Python's == now goes through the same PartialEq.
    • Debug keeps the guard, because printing the derived format without calling itself would mean writing that format by hand.
  • What still uses the guard, and why:

Out of scope:

Tests

  • A new test module, walks_without_recursion (in parser.rs), builds a tree of 3,000 nested <c-if> by hand. The parser itself stops control flow at 256. On a 64 KiB thread, the module then:

    • runs every converted pass;
    • clones, compares and drops the tree;
    • checks that a change at the innermost level makes the comparison fail.

    Putting back a recursive drop or a recursive comparison makes the thread overflow.

  • cargo test -p citry_template_parser (all 34 test binaries), cargo clippy --all-targets --no-deps -D warnings, cargo fmt --check.

  • Python, on a release build of this branch: the citry_core tests pass (543), and so does the citry suite (10,360). The one failure is a test that also fails on main; test: name the component for a link in the div that closes its paragraph #518 fixes it.

No changelog entry: nothing a user of the package can observe changes.

🤖 Generated with Claude Code

JuroOravec and others added 5 commits October 28, 2026 20:19
…er passes

The parser passes that visit every nested body (finding ignored content,
checking fill groups, recomputing used variables, collecting and checking
template bindings, and extracting fills) now keep their own list of what is
left to visit, so their native stack use no longer grows with the template's
depth. The results are unchanged. A test runs every pass over a tree 3,000
levels deep on a 64 KiB thread.

Part of #300.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…elf per level

`Template`'s `Clone`, `PartialEq` and `Drop` keep their own stack of the
bodies still to visit instead of running through the stack guard, so they
work on a small thread at any depth. `Debug` stays on the guard, because
printing the derived format without calling itself would mean writing that
format by hand. A test clones, compares and drops a tree 3,000 levels deep
on a 64 KiB thread, and checks that a change at the innermost level is seen.

Part of #300.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The stack guard module, the crate's AGENTS.md and the deep nesting test now
describe what is left on the guard: the compiler, the parse of a template
written in an attribute value, and `Template`'s `Debug`. The grammar keeps
its stack reservation because Pest's generated parser enters each rule by
calling a function, and the attribute-template parse keeps its guard
because it is a whole parse called from inside another.

Part of #300.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Bring the remaining comments that described the old per-level recursion in
line with the code (the deep nesting tests, `check_nesting_depth`, the
compile entry in the Python bindings, and the expression red zone). Let
`Template`'s clone and comparison skip the work list for a body without
nodes, which is the common case PyO3 copies on every `body` read, and name
every field in the comparison so a new one cannot be missed.

Part of #300.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
`Template.__eq__` now returns `self == other`, so Python's `==` uses the
same comparison as Rust, field list included. Also correct a few comments:
the grammar's brace rules call themselves per `{` (#300 tracks whether
they can loop), the order the binding passes visit, and the depth of the
test tree against the control-flow limit.

Part of #300.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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