Repository navigation
perf: walk templates on a first render without one Python call per level - #360
Merged
Merged
Conversation
`precompute_const_parts`, which runs on a component's first render and again for each new set of Const values, walked the template through `_precompute_into`, `_precompute_item`, `_precompute_branch_bodies` and `_try_unroll_for`, which called each other once per level, about three Python frames per level of control flow. They are now generators run by the walk driver (`citry._walk.run`): each place that called one of them yields `Call(...)` instead, and the driver keeps the running generators on a list, so the walk's results, errors and order stay the same. `_statically_precomputable`, a pure yes-or-no shape check, walks nested `<c-if>` branch bodies on its own stack. A first render of a template nested 64 levels deep no longer has constness on its deepest stack. The whole citry suite passes unchanged. Part of #272. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`compile_leaf_program` runs on a leaf's first render. Its compiler's `compile_body`, `compile_item`, `_compile_if` and `_compile_for` called each other once per level of control flow. They are now generators run by the walk driver (`citry._walk.run`), so the compiled program, the point where a body is found unsupported, and the order of every side effect on the compiler stay the same. The whole citry suite passes unchanged. Part of #272. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`_SimpleJsonCodegen._evaluate_function`, `_write_evaluate_function` and `_evaluate_lines` called each other once per branch or loop body while writing the evaluator's source. They are now walk generators run by `citry._walk.run`, so the generated source, its names and its nesting count stay the same. Part of #272. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…r level The i18n extension's `_transform_body` and the leaf program's `_evaluation_only_operations` called themselves once per nested body. They are now walk generators run by `citry._walk.run`. Each nested body is still transformed completely where it is reached, so i18n binding ordinals are numbered in the same order. Part of #272. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`_simple_json_read_set`'s `visit` and `freeze`, `_simple_json_loop_targets` and `_simple_json_nested_operations` called themselves once per nested body. They are now walk generators: the first three run with `citry._walk.run`, and the nested-operations iterator is driven by `citry._walk.walk`, which yields the same operations in the same order without a frame per level of its former `yield from` chain. Part of #272. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The events extension's `_transform_compiled_body` called itself once per nested component, slot, fill or control-flow body. It is now a walk generator run by `citry._walk.run`, so bodies are transformed in the same order and every error is raised at the same point. Part of #272. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…call per level `_resolve_foreign_body` and `_compile_simple_json_operations` called themselves once per nested body on a component's first render. Each now runs a walk generator with `citry._walk.run`, keeping its signature for its callers. Child bodies are still resolved completely, in order, before their parent is handed to its owner. Compiling the leaf operations no longer needs Python stack in proportion to depth, so it cannot raise RecursionError outside the try that catches its other compile errors. Part of #272. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`_compute_static_instance_free_template_supported`'s `supported` check called itself for each branch and loop body. It is a pure yes-or-no check, so it now walks those bodies on its own stack. Part of #272. Co-Authored-By: Claude Opus 5.5 <[email protected]>
`_native_row_operations`, `_native_row_read_node`, the read-set preflight writer's `emit` and `_materialized_row_plan`'s walk called themselves once per nested body or read-set level on a leaf's first render. They are now walk generators run by `citry._walk.run`, with the same results and the same order. The row HTML writer's generator already stops at 16 nested bodies, so it is left as it is. Part of #272. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…n stack The same nine shapes on both render paths, measured on a component's first render, which also precomputes constant parts, runs the i18n and events transforms and compiles the Vue leaf program. All 18 cases fail with only the E1 changes and pass with these. Part of #272. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The first-render frame test now also measures templates that reach the walkers the plain shapes leave shallow: <c-if> tags with Const conditions, a Const loop with a dynamic body, chained loops that build a nested read set, and a simple='vue' leaf, whose generated evaluator's own frames are left out of the count. Each measurement first compiles an unrelated component, so one-time set-up work does not land in the shallow peak. All eight new cases fail with only E1's changes. Comments now describe the code as it is. Part of #272. Co-Authored-By: Claude Opus 5.5 <[email protected]>
JuroOravec
force-pushed
the
perf/first-render-walkers
branch
from
October 24, 2026 23:42
be4c5a3 to
bc69f18
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 #272. This is PR E2 from the plan for #272: the code that walks a template on a component's first render no longer calls itself once per level of nesting. So the Python stack that Citry's own first-render code needs stays the same at any template depth, apart from the cases under Known limits.
What was wrong
A first render runs several passes over the whole template:
simple='vue'leaves, building the row plans that let the Rust renderer write their loop rows.Each pass called itself once per level of nesting, adding one to three Python frames per level. A first render of a template nested 64 levels deep peaked at 206 frames.
What changes
Each of these walkers is now a generator that
citry._walk(from #355) runs. Where it called itself, it yieldsCall(child, ...)and gets the child's result back from theyield. The driver keeps the running generators on a list, and throws a child's error into its caller at theyield. Each converted function keeps its signature for outside callers, which run the walk withrun(...). The walkers:constness.py:_precompute_into,_precompute_item,_precompute_branch_bodiesand_try_unroll_for._statically_precomputable, a pure yes-or-no check, keeps a list of bodies still to check, without calling itself.compile_body,compile_item,_compile_ifand_compile_for._evaluate_function,_write_evaluate_functionand_evaluate_lines. The generated source is byte-identical.leaf_program.py's first-render helpers:_evaluation_only_operationsand_compile_simple_json_operations;visitandfreeze, which build the read set (every variable path the template reads), and_simple_json_loop_targets;_simple_json_nested_operations, whichwalk()now drives as one generator, so it still yields operations one at a time;_native_row_operations,_native_row_read_node, andemit, which writes the code that checks the read set's data types before a render;_materialized_row_plan's walk;supported, which keeps a list of bodies still to check, without calling itself.extension.py:_resolve_foreign_body._transform_bodyand the events_transform_compiled_body. Counters the walk shares, such as the numbers i18n gives each binding, still advance in source order.The row HTML writer's code generator is left as it is: it already stops at 16 nested bodies, and deeper rows take the per-row path.
Evidence
A first render at depth 64 now peaks at 35 frames, the same as at depth 8.
test_render_depth_frames.pymeasures first renders at depths 32 and 128, and fails if depth 128 uses more than 20 frames more than depth 32. Each test first renders an unrelated component once, so one-time set-up work stays out of the depth-32 measurement. The shapes:<c-if>withConstconditions, aConstloop with a dynamic body, chained loops that nest the read set, and asimple='vue'leaf, where the test counts only Citry's own frames and skips the generated evaluator's (see Known limits).All 26 cases fail with only E1's changes and pass here.
All 9,096 citry tests pass with no expected output changed. mypy (also
--platform linux) and ruff are clean.An independent review compared every converted walker with E1's branch, and found the results identical:
__context__;None of the converted functions runs on every render: each is guarded by a flag that runs it once per template node, by the cache of precomputed constant parts, or by the compile step. The review checked this too.
Timing
The plan's threshold for E2 is that first renders get at most 5% slower. I ran 15 fresh processes per side, alternating sides, and in each recorded the CPU time of one first render of the large benchmark scenario. The base side was E1's Python code, and both sides used the same release build of the Rust extension. Positive means E2 is slower.
The load average was 10 to 16, and each sample is a single first render. All four numbers are within 5%, but the noise is about as large as the threshold, so this rules out a large slowdown without proving the 5% bound. The stronger evidence is the driver's cost: about 300 ns per
Call, roughly one call per template item, which comes to about 1 ms of a 240 ms first render. This doesn't cover the docs-site components the plan names, or aConstcache miss, which reruns the precompute pass and leaf compile; a rerun on a quiet machine can add those.Known limits
_simple_json_expr_from_aststill recurses once per node of one expression, so a very long subscript chain in a single expression can still reach Python's recursion limit. Template depth alone no longer can.simple='vue'leaf's generated evaluator keeps one Python frame per level on every render. It has no Python evaluator to fall back on, as described in perf: render nested control flow without one Python call per level #356. The plan's frame-budget test (test 5, in a later PR, E4) checks whether that matters at the deepest nesting Citry allows.🤖 Generated with Claude Code