Skip to content

perf: render nested control flow without one Python call per level - #356

Merged
JuroOravec merged 7 commits into
mainfrom
perf/render-explicit-stack
Oct 24, 2026
Merged

JuroOravec merged 7 commits into
mainfrom
perf/render-explicit-stack

Conversation

@JuroOravec

Copy link
Copy Markdown
Member

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> or c-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 its render, 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

  • The body renderer keeps its own stack (_render_body in component_render.py, which renders a list of template nodes).
    • When it reaches an IfNode or ForNode, the node's new _render_steps hands back the bodies it would render, plus the function that wraps their rendered output. The renderer then renders those bodies itself.
    • Each node's part is wrapped exactly as its render wraps it, and ForNode still reads one iteration at a time.
    • An error passes out through the frames innermost first. Each frame resets the current template data, closes its loop and adds its node's template position, in the same order as before.
    • IfNode.render and ForNode.render still work for callers outside the loop.
  • Collecting <c-fill> tags (collect_fills_from_body) walks <c-if> and <c-for> bodies on its own stack.
  • The Vue leaf runtime (_vue/leaf_program.py):
    • The Python evaluator (_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.
    • An ordinary leaf uses the generated evaluator for up to 15 nested <c-if>/<c-for> levels. Deeper templates run on the Python evaluator.
    • The generated row writer refuses 16 or more levels, and those rows are rendered one at a time on the general path.
    • A simple='vue' component renders without a component instance and has no other evaluator, so it still uses the generated one at any depth.
  • Rendering a slot, or a template passed in an attribute, still calls the renderer again, but that adds a fixed number of frames however deep the template is.

Evidence

  • The snapshot test (test 10 in the plan, 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:
    • the serialized HTML;
    • the nested render structure;
    • the error text for an expression failing three levels deep, and the nodes that add their position on the way out (PreparedExprNode, ForNode, IfNode).
  • The frame test (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.
    • After this PR, second renders peak at 15 to 28 frames at every depth.
    • The same file checks simple='vue' components at 15, 16 and 20 levels.
  • All 9,055 citry tests pass with no expected output changed. ruff passes, and mypy (also --platform linux) is clean.
  • An independent review compared error messages, exception chains, the order loops close in, output and structure with main through probes, and found them identical in every case it tried.
    • The review found one real regression: simple='vue' past 16 levels. It's fixed and has a test.
    • It also showed that the first version of test 10's error case recorded the wrong error. That snapshot was recorded again on 1b64f71.

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.

  • Inconclusive by wall time. The machine was shared with other builds, at a load average of 16 to 26 throughout, and single runs of one row varied by up to 2.4x. In the final run, all subsequent-render rows and all native-off rows came out between -6.1% and +0.2%. The native first-render rows came out at +7.3% and +1.4%, with overlapping runs (base 272, 341 and 287 ms; branch 316, 290 and 308 ms).
  • Passed by minimum CPU time, which is much less affected by this load. I ran 7 alternating fresh processes per side, each measuring the minimum process time over 15 subsequent renders of the large scenario. The change is +0.21% with the native path on and +0.28% with it off.
  • The rework this gate caused. Before commit 7cb9d64, the same measurement showed +6.0% with the native path off. A profile traced it to a helper call per render part, about 3,500 per render. The loop now does that work inline.

A rerun on a quiet machine before release is left to the maintainer.

Not in this PR

  • First renders can still reach RecursionError on 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.
  • E3 covers the code that walks render output, and E4 the code that walks the parsed AST.

🤖 Generated with Claude Code

JuroOravec and others added 7 commits October 25, 2026 00:15
…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]>
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