Repository navigation
fix: walk render output with the same Python frames at any template depth - #364
Merged
Merged
Conversation
This was referenced Oct 25, 2026
…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]>
JuroOravec
force-pushed
the
fix/walkers-render-output
branch
from
October 25, 2026 09:13
c3ef195 to
bfb2cb6
Compare
This was referenced Oct 25, 2026
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 #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 raisesRecursionErroronmain, 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.
_vue/document.py:contains_documentandvisit);#c-ignorecontents (_ignored_contents_htmlin_vue/direct_capture.py);v-precontents for authored markup and Vue syntax (_authored_markup_onlyand_authored_vue_syntaxin_vue/direct_capture.py, from fix: let an on_render hook pass its output to another component on a Vue page #362);_capture_pure_partand_replay_pure_bodyincomponent_render.py);PreparedViewruns on its slots and calls (_vue/prepared.py).transform_parts,transform_componentandrun_membersin_vue/direct_capture.py):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.yield from, through a smallnestedhelper, 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.collect_instancesanddetach) and replay (build);_vue/compiler.pythat write each definition's Vue template (render_nodesanddefinition_compile_input).Left recursive on purpose:
_export_typed_leaf,_apply_typed_replacementsand_replay_typed_leafinext/cache/replay.py, and_part_to_wire/_part_from_wireinext/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 withCacheArtifactError, and the decoder turns aRecursionErrorinto the same error.Tests
tests/test_deep_render_output_walkers.py:<c-if>bodies around child components;#c-ignorecontents;v-precontents inside a table, under nested<c-provide>bodies;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 raiseRecursionError.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: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.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
mainon an 18 ms assembly. That measure renders once in-process, then takes the minimum of 60 runs ofassemble_typed_renderwith 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
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.constness.pybelong to the first-render group, and a single template 512 levels deep already fails there under the default recursion limit, as onmain.mainhas an unrelated failure:test_render_body_snapshots.py::test_an_error_three_levels_deep_matches_the_snapshotrecords 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 onmainabove), and the citry_ui suite (3115 passed).🤖 Generated with Claude Code