Skip to content

fix: walk render output with the same Python frames at any template depth - #364

Merged
JuroOravec merged 1 commit into
mainfrom
fix/walkers-render-output
Oct 25, 2026
Merged

JuroOravec merged 1 commit into
mainfrom
fix/walkers-render-output

Conversation

@JuroOravec

@JuroOravec JuroOravec commented Oct 25, 2026 •

Copy link
Copy Markdown
Member

Part of #299 (deep templates, #272): the walkers that read render output.

What was wrong

Several functions that read what a render produced called themselves once per nested layer of output: each <c-if> or <c-for> body, slot, or transparent component adds a layer. So a deep template cost a Python frame per level in each of them. A chain of 300 components that each nest three <c-if> levels raises RecursionError on main, in the Vue document shell walk.

The fix

The walkers now keep their own stack, so the number of Python frames they use stays the same however deep the template nests. The output is unchanged: the whole suite passes with no change to an expected value.

  • Hand-written stacks, for walkers that run on every render:
    • the Vue document shell (_vue/document.py: contains_document and visit);
    • #c-ignore contents (_ignored_contents_html in _vue/direct_capture.py);
    • the checks of v-pre contents for authored markup and Vue syntax (_authored_markup_only and _authored_vue_syntax in _vue/direct_capture.py, from fix: let an on_render hook pass its output to another component on a Vue page #362);
    • the capture and replay of pure components (_capture_pure_part and _replay_pure_body in component_render.py);
    • the checks a PreparedView runs on its slots and calls (_vue/prepared.py).
  • The generator walk from feat: add a driver that runs recursive walks without a call per level #355 (each walker is a generator, and one loop, the driver, runs them one after another, so a nested call adds no Python frame), for the Vue assembler (transform_parts, transform_component and run_members in _vue/direct_capture.py):
    • A driver call costs about 0.7 microseconds against 0.1 for yield from, and an ordinary page makes about a thousand nested calls. Handing every call to the driver made the large benchmark scenario about 2% slower.
    • So each component's body runs up to 8 nested levels directly with yield from, through a small nested helper, inlined at the two busiest calls. Each child component starts that count again, and all direct walks together, call runs included, stop at about 128 Python frames. Past either limit, a level goes through the driver, which starts the counts again at 0.
  • The generator walk, for walkers that run less often:
    • the cache extension's artifact export (collect_instances and detach) and replay (build);
    • the functions in _vue/compiler.py that write each definition's Vue template (render_nodes and definition_compile_input).

Left recursive on purpose: _export_typed_leaf, _apply_typed_replacements and _replay_typed_leaf in ext/cache/replay.py, and _part_to_wire / _part_from_wire in ext/cache/artifact.py. They recurse only into the typed parts of a leaf program, and leaf programs cannot nest there: the export checks every part with the decoder, which rejects a nested leaf with CacheArtifactError, and the decoder turns a RecursionError into the same error.

Tests

tests/test_deep_render_output_walkers.py:

  • Depth 8 against depth 128. Each fixture renders nested 8 and 128 levels deep and counts how many frames of these walkers are on the Python stack at once. The deep render may use at most 4 more. The fixtures are:
    • <c-if> bodies around child components;
    • call runs;
    • a document shell;
    • #c-ignore contents;
    • v-pre contents inside a table, under nested <c-provide> bodies;
    • pure components;
    • a cache export and replay.
  • A 300-component chain, of plain calls and of call runs, stays under 160 walker frames (132 measured).
  • Hand-built prepared views with 8 and 128 nested slots.

On main, 11 of the 13 cases fail (only the two 10-component chains pass): the peak grows from 10 to 14 frames at depth 8 to 130 to 134 at depth 128, and both 300-component chains raise RecursionError.

The reviewer also compared the HTML with main, with random ids masked, and found it identical. That covered 13 fixture kinds at depths 8, 128 and 512, and 12 chain shapes:

  • chains of 50 components 128 and 512 levels deep;
  • chains of 300 to 1,500 components;
  • call-run, fill-forwarding and transparent chains.

An assembly error raised deep inside a chain gives the same message as on main.

Speed gate

The plan's gate is each benchmarks/ row's median of three runs, at most 2% slower. This PR, like #356, used the minimum CPU time over alternating fresh processes, because on this loaded machine the run-to-run noise was larger than 2%. The run used the large benchmark scenario with a release build of citry-core, both sides on the same extension, and 15 alternating fresh processes per side of 30 renders each.

Run (load average) Native on Native off
1 (9 to 10) +0.39% +0.64%
2 (6 to 11) +0.71% +0.77%

Runs during load spikes were inconclusive, swinging from -5% to +4% with medians within 1.4%. Timed on its own, the assembler is up to about 0.4 ms (about 2%) slower than main on an 18 ms assembly. That measure renders once in-process, then takes the minimum of 60 runs of assemble_typed_render with the same arguments. My 12 alternating process pairs on this revision gave +0.06 ms best against best, and the reviewer's 6 pairs on an earlier revision gave +0.3 to +0.4 ms.

Not in this PR

  • The simple='vue' generated evaluator functions and the slot hop (about 10 frames per forwarded fill) still grow a frame per level. Both belong to perf: render nested control flow without one Python call per level #356 by design.
  • The constant-precompute walkers in constness.py belong to the first-render group, and a single template 512 levels deep already fails there under the default recursion limit, as on main.
  • main has an unrelated failure: test_render_body_snapshots.py::test_an_error_three_levels_deep_matches_the_snapshot records an absolute checkout path, so it fails in every other checkout, CI included. It is reported to the owner of perf: render nested control flow without one Python call per level #356.

No changelog entry: the entry for deep templates comes with the rest of #299.

Checks: ruff, mypy (macOS and --platform linux), the citry suite (9085 passed, apart from the 2 failures on main above), and the citry_ui suite (3115 passed).

🤖 Generated with Claude Code

…epth

Several functions that read a render's output called themselves once per
nested layer: each `<c-if>` or `<c-for>` body, slot or transparent
component adds one (issue #299, part of #272). A deep template therefore
cost a Python frame per level in each. They now walk the same output
with a stack:

- On every render, the Vue document shell (`contains_document`, `visit`),
  `#c-ignore` contents (`_ignored_contents_html`), the checks of `v-pre`
  contents (`_authored_markup_only`, `_authored_vue_syntax`), pure components'
  capture and replay, and the prepared-view checks in `_vue/prepared.py`
  use hand-written stacks of iterators.
- The Vue assembler's `transform_parts`, `transform_component` and
  `run_members` become generator walkers run by `citry._walk.run`. A
  driver call costs about 0.7 microseconds against 0.1 for `yield from`,
  and an ordinary page makes about a thousand nested calls, so handing
  every one to the driver made the large benchmark scenario about 2%
  slower. So each component's body runs up to 8 nested levels directly
  with `yield from` (the `nested` helper, inlined at the two busiest
  calls), each child component starts that count again, and all direct
  walks together, call runs included, stop at about 128 Python frames. Past either bound a
  level goes through the driver. A chain of 300 components that each nest
  3 `<c-if>` levels now uses 130 walker frames; on `main` it raises
  `RecursionError`.
- The cache extension's artifact export (`collect_instances`, `detach`)
  and replay (`build`), and the functions in `_vue/compiler.py` that write
  each definition's Vue template (`render_nodes`,
  `definition_compile_input`), use the generator walk.

The output is unchanged: the whole suite passes with no change to an
expected value. A new test renders each fixture nested 8 and 128 levels
deep (`<c-if>` bodies around child components, call runs, a document
shell, `#c-ignore` contents, `v-pre` contents in a table, pure
components, and a cache export and replay) and checks that the deep render puts at most 4 more of these
walkers' frames on the stack at once. On `main` the peak grows from 10 to
14 frames at depth 8 to 130 to 134 at depth 128. Another test checks
that a 300-component chain, of calls or of call runs, stays under 160
walker frames (132 measured).

Speed, on the large benchmark scenario with a release build of
citry-core, by the minimum CPU time over 15 alternating fresh processes
of 30 renders each (load average 6 to 15): +0.4% and +0.7% with the
native path on, +0.6% and +0.8% with it off. Runs during load spikes
were inconclusive, swinging from -5% to +4% with medians within 1.4%.
The assembler alone, timed in-process, is up to about 0.4 ms (about 2%)
slower than `main` on an 18 ms assembly.

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