Repository navigation
perf: render nested control flow without one Python call per level - #356
Merged
Merged
Conversation
…stack renderer Snapshots observed on main (1b64f71) for the plan's test 10, on both render paths: the serialized HTML, the nested render structure, and the error text for an expression that fails three levels deep. Part of #272. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`_render_body` rendered a `<c-if>` or `<c-for>` by calling the node's `render`, which called `_render_body` again for its body, so each level of control flow cost two Python frames. A template nested a few hundred levels deep raised RecursionError while rendering. The body renderer now keeps a stack of frames. When it reaches an `IfNode` or `ForNode`, the node's `_render_steps` hands back the bodies it would render and the step that wraps their parts, and the renderer renders those bodies on its own stack. The output is unchanged: each node's part is wrapped exactly as its `render` wraps it, `ForNode` still reads its iterations one at a time, and each frame sets and resets the value context as before. An error passes out through the frames innermost first, closing each loop and adding each node's template position in the same order as the nested calls did. A second render of a template nested 200 levels deep used 412 Python frames before and 15 now, at every depth. The recorded snapshots for the plan's test 10 match, and the whole citry suite passes unchanged. Part of #272. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Two more walkers called themselves once per level of control flow: - `collect_fills_from_body` asked each `<c-if>` and `<c-for>` to collect its own fills, which called it again for the body. It now walks those bodies on its own stack. A loop's next iteration is read only after the previous body is finished, as before, and an error closes every open loop innermost first. - `_materialize`, which writes a Vue leaf program's output, called itself for each branch and loop row. It now walks them on its own stack, with the same type checks in the same order. The whole citry suite passes unchanged. Part of #272. Co-Authored-By: Claude Opus 5.5 <[email protected]>
A template made of HTML, expressions and control flow renders through a Vue leaf program. Its walkers called themselves once per branch or loop level, and its generated evaluator and row writer emitted one Python function per level that called the next, so a deep template cost a frame per level on every render. - `_evaluate_inner`, the Python evaluator, walks branch and loop bodies on a stack of frames. Each body frame sets the value context as each `_evaluate` call did, and an error passes out innermost first: each loop frame closes its loop and adds its `<c-for>`'s position, as the loop's `try` did. - `_materialize_typed`, which writes the typed output, does the same, and wraps a loop kept together as a call run once all its rows are written. - `_simple_json_read_node_is_plain` checks a read plan's children and list items on its own stack. - The generated evaluator and row writer emit at most 16 nested bodies. A deeper template raises ValueError while generating, which both already handle by using the Python evaluator and the general row path. A second render of a 150-level leaf template now peaks at 19 Python frames at every depth, on both render paths. The whole citry suite passes unchanged. Part of #272. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Second renders at depth 8 and 128 for nested <c-if>, <c-for> and c-if=, through the body renderer and the Vue leaf program, plus nested components, simple components and fills under control flow, on both render paths. The peak may grow by at most 20 frames. On main, the six control-flow shapes fail on both paths (300 frames at depth 128). Part of #272. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The explicit-stack renderer added every part through a helper, one extra call per part (about 3,500 per render of the large benchmark scenario). The loop now does it inline, as the recursive renderer did, and the helper handles only finished <c-if>/<c-for> parts. The hot loop also no longer calls typing.cast(). Part of #272. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…dings - A `simple='vue'` component renders without an instance, through the generated evaluator only. The 16-level cap made the evaluator refuse a template 16 or more levels deep, so such a component failed to render. The evaluator is generated at any depth again and records how deeply its functions nest; only the ordinary leaf render, which has the Python evaluator to fall back on, skips a program past 16 levels. The row HTML writer keeps its cap, because its rows fall back to the per-row path. - Test 10's error case now registers the child components, so it records the expression failing three levels deep instead of an unregistered component, and it records the nodes that add their template position on the way out. Recorded again on 1b64f71. - The depth test compares depths 32 and 128, both past the generated code, so both use the same evaluator, and checks simple='vue' components at 15, 16 and 20 levels. - IfNode keeps its @Final marker, the root body frame sets the value context last, and comments describe the code as it is. Part of #272. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This was referenced Oct 24, 2026
Merged
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 is PR E1 from the plan for #272: the code that runs on every render no longer calls itself once per level of
<c-if>,<c-for>orc-if=, so a second render of a deeply nested template needs about the same number of Python frames at any depth.Depends on #343 (C1). The new tests nest templates 128 levels deep. Parsing and compiling those is safe on every CI platform only once C1 lands; it stops the Rust parser and compiler from overflowing the stack on deep input. Merge #343 first.
What was wrong
Rendering a
<c-if>called itsrender, which called the body renderer again for the branch, so each level of control flow cost two Python frames. A template made of HTML, expressions and control flow renders through a Vue leaf program, and it cost one frame per level in the evaluator, and its generated code emitted one Python function per level. A second render of a template nested 200 levels deep used 412 Python frames.What changes
_render_bodyincomponent_render.py, which renders a list of template nodes).IfNodeorForNode, the node's new_render_stepshands back the bodies it would render, plus the function that wraps their rendered output. The renderer then renders those bodies itself.renderwraps it, andForNodestill reads one iteration at a time.IfNode.renderandForNode.renderstill work for callers outside the loop.<c-fill>tags (collect_fills_from_body) walks<c-if>and<c-for>bodies on its own stack._vue/leaf_program.py):_evaluate_inner), the typed and HTML output writers (_materialize_typed,_materialize) and the check that a value read from the template data is plain JSON (_simple_json_read_node_is_plain) walk branches and loop rows on their own stacks.<c-if>/<c-for>levels. Deeper templates run on the Python evaluator.simple='vue'component renders without a component instance and has no other evaluator, so it still uses the generated one at any depth.Evidence
test_render_body_snapshots.py) was recorded on 1b64f71 before any code change, then locked. On both the Rust and the Python render paths, for 18 fixtures (control flow, components, slots and fills, a simple component's slot outlet, loop rows written as one call run (consecutive component calls rendered together), a template in an attribute,<c-component>/<c-element>,<c-provide>), it compares:PreparedExprNode,ForNode,IfNode).test_render_depth_frames.py) measures the deepest stack of a second render at depths 32 and 128, for nine shapes on both paths, allowing at most 20 frames of growth. On main, the six control-flow shapes fail: 300 frames at depth 128.simple='vue'components at 15, 16 and 20 levels.--platform linux) is clean.simple='vue'past 16 levels. It's fixed and has a test.Speed gate
The plan requires that no Citry row of the benchmark (the large scenario, first and subsequent renders, Rust render path on and off) gets more than 2% slower, comparing the median of three runs per side. Both sides used the same release build of the Rust extension; only the Python code differed.
A rerun on a quiet machine before release is left to the maintainer.
Not in this PR
RecursionErroron deep templates in the walkers the plan gives to E2, such as the pass that precomputes a template's constant parts. The changelog entry for deep templates comes with the rest of PR E.🤖 Generated with Claude Code