Skip to content

perf: walk templates on a first render without one Python call per level - #360

Merged
JuroOravec merged 11 commits into
mainfrom
perf/first-render-walkers
Oct 25, 2026
Merged

JuroOravec merged 11 commits into
mainfrom
perf/first-render-walkers

Conversation

@JuroOravec

@JuroOravec JuroOravec commented Oct 24, 2026 •

Copy link
Copy Markdown
Member

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:

  • precomputing its constant parts;
  • the i18n and events transforms (both on by default);
  • handing the template parts an extension claimed (foreign spans) back to that extension to compile;
  • compiling the parts of the template that contain no components (Vue leaf programs) into Python code, and writing their generated evaluator;
  • for 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 yields Call(child, ...) and gets the child's result back from the yield. The driver keeps the running generators on a list, and throws a child's error into its caller at the yield. Each converted function keeps its signature for outside callers, which run the walk with run(...). The walkers:

  • constness.py: _precompute_into, _precompute_item, _precompute_branch_bodies and _try_unroll_for. _statically_precomputable, a pure yes-or-no check, keeps a list of bodies still to check, without calling itself.
  • The Vue leaf compiler: compile_body, compile_item, _compile_if and _compile_for.
  • The evaluator code generator: _evaluate_function, _write_evaluate_function and _evaluate_lines. The generated source is byte-identical.
  • leaf_program.py's first-render helpers:
    • _evaluation_only_operations and _compile_simple_json_operations;
    • visit and freeze, which build the read set (every variable path the template reads), and _simple_json_loop_targets;
    • _simple_json_nested_operations, which walk() now drives as one generator, so it still yields operations one at a time;
    • _native_row_operations, _native_row_read_node, and emit, 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.
  • The extensions: the i18n _transform_body and 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.py measures 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:

    • nine shapes on both the Rust and the Python render paths;
    • four more, also on both paths, that reach the remaining walkers: <c-if> with Const conditions, a Const loop with a dynamic body, chained loops that nest the read set, and a simple='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:

    • 41 generated sources and 7 native row plans, byte for byte, at several depths;
    • rendered HTML on both paths;
    • error types, messages and __context__;
    • the order of shared-state updates.
  • 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.

Rust render path By minimum By median
On +4.65% -4.72%
Off -3.28% +1.95%

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 a Const cache miss, which reruns the precompute pass and leaf compile; a rerun on a quiet machine can add those.

Known limits

  • Tracebacks from these walkers are longer: each level adds a walker frame and a driver frame. The error type, message and chain are unchanged.
  • _simple_json_expr_from_ast still 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.
  • A 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

JuroOravec and others added 11 commits October 25, 2026 01:40
`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]>
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