Skip to content

perf: render nested slot fallbacks without a Python call per level - #397

Merged
JuroOravec merged 2 commits into
mainfrom
perf/slot-fallback-walk
Oct 25, 2026
Merged

JuroOravec merged 2 commits into
mainfrom
perf/slot-fallback-walk

Conversation

@JuroOravec

@JuroOravec JuroOravec commented Oct 25, 2026 •

Copy link
Copy Markdown
Member

What this changes

A <c-slot> that gets no fill renders its own body, its fallback. Before
this change, each nested fallback added 10 Python frames, because the
fallback rendered through a Slot call that started the body renderer
(_render_body, the loop that renders a template body) again. About 90
nested fallbacks raised RecursionError under Python's default limit,
well below the 256 levels of Citry logic tags that the planned nesting
limit in #272 will allow.

SlotNode._render_steps now returns the fallback body to the body
renderer that is already running, the way IfNode and ForNode have
handed over their bodies since #356. Nested fallbacks now render in about
the same number of frames at any depth:

Nested fallbacks 8 64 256
Deepest stack before (Python frames, no Vue) 97 657 about 2,600
Deepest stack after, no Vue 17 17 17
Deepest stack after, with Vue 22 23 23

A passed fill still renders through a Slot call (_render_direct), as
before. Its body belongs to the component that passed it, so rendering it
here would not save a frame per level. The old path also stays for a
render without a Vue capture session (every ordinary render has one) and
while code has registered or patched the CitryRender class, which
_render_value would then treat differently; citry_render.py moves that
check into _render_passes_through so both places use it.

How the output stays the same

The generator that _render_steps returns for a fallback does the same
work as _render_direct, in the same order. It makes the same Slot,
starts the same Vue execution, and holds the same scopes while the body
renders: the execution, then add_slot_to_error_message (the scope that
adds the slot to an error's component path), then the execution again. It
wraps the result the same way and runs on_slot_rendered at the same
point.

BodySteps, the tuple a node returns to tell the body renderer which
bodies to render, gains two optional fields for this:

  • parts: the list the body's parts go into, so the slot can wrap them
    while its scopes are still open.
  • wraps_errors: an error leaving the fallback is thrown into the slot's
    generator where it waits at its yield, so the with blocks there see
    it and the slot is still added to the component path
    (Root > Root(slot:s0) > Root(slot:s1)).

IfNode and ForNode leave both at their defaults. A StopIteration
from template code or the slot hook is carried out of the generator, which
Python would otherwise turn into a RuntimeError, and raised with the same
type, message, component path and __context__ as before.

Tests

  • test_render_body_snapshots.py gains fallback fixtures: 1, 3 and 8
    deep, next to a filled slot, shown through a fill's fallback variable,
    with slot data, in loops, and in pure and cached components. Each is
    recorded without Vue (HTML and the render structure) and with Vue (the
    host HTML and the browser data block, with ids numbered by first
    appearance), on both the native (Rust) and the Python render path. Also
    recorded: what on_slot_rendered sees, a cached component's replay,
    development mode, and errors raised inside three fallbacks (an
    expression error, a StopIteration, a KeyboardInterrupt) or by the
    hook. They were recorded on 05b95e7 in a separate commit before the
    change, and pass unchanged both on 2fc91b2 (this branch's base) and
    after the change.
  • Each error test also checks that no fallback generator is left paused
    holding its scopes. When a test leaves one paused on purpose, all 14
    error tests fail.
  • test_render_depth_frames.py gains a template of nested fallbacks,
    comparing the deepest stack at depth 32 and at depth 128, also with an
    on_slot_rendered hook and in development mode. All 8 cases fail if
    _render_body stops passing SlotNode to _render_steps.
  • A unit test for _throw_into (which throws an error into a paused
    fallback generator and returns the error that comes out) covers an error
    raised again, a replaced one, one the generator drops (by returning or
    by yielding again), and a KeyboardInterrupt.
  • Test 5 in 50 nested <c-if> blocks fail with "too many nested parentheses", and about 1,000 crash the process #272, which checks that a template 256 levels deep renders
    when 200 Python frames are already in use, comes next in its own PR,
    stacked on this one, and includes nested fallbacks at 256.

Speed

CPU time, release build of citry_core, the same extension on both sides,
7 alternating runs per side, on a quiet machine (load average under 3):

Benchmark Native (Rust) render path Change (min) Change (median)
Large render benchmark on +0.82% +0.60%
Large render benchmark off +0.99% +0.27%
300 components, each with one fallback on -2.96% -3.06%
300 components, each with one fallback off -1.88% -2.30%
300 components, each with one filled slot, run 1 on +1.32% +1.13%
300 components, each with one filled slot, run 1 off +1.34% +2.79%
300 components, each with one filled slot, run 2 on +1.79% +0.97%
300 components, each with one filled slot, run 2 off +1.47% +0.77%

The filled-slot benchmark ran twice, because in run 1, with the native
path off, the median (+2.79%) was far from the minimum (+1.34%). Run 2 is
under 2% everywhere.

A filled slot costs about 1.5% more because each slot now picks its fill
in a separate method that returns a tuple, so the body renderer can decide
whether to render the fallback itself. That adds one call and one tuple
per slot. A first version built a NamedTuple there and measured +2.2% to
+2.5%.

Checks run

  • The citry test suite with a release build of this branch: everything
    passes. With the extension built from the nesting-limit branch for 50 nested <c-if> blocks fail with "too many nested parentheses", and about 1,000 crash the process #272
    (not yet a PR), four fills_under_control_flow frame cases fail,
    because that branch's limit rejects the shape at depth 128. The same
    branch already halves that shape's depth in the test.
  • ruff, and mypy on the citry package (also --platform linux).
  • Every scripts/check.py phase that rebuilds or compares a committed
    file: uv lock --check, protocol Python copies, the docs-playground
    build check, Citry UI assets, and the validators.
  • Two independent code reviews, each with a prose pass, before the final
    version, and one of this text. Their findings are folded in.
  • The full local gate was not run; PR CI runs it.

The root CHANGELOG.md gets one entry under Fixes.

Part of #272.

🤖 Generated with Claude Code

JuroOravec and others added 2 commits October 25, 2026 11:44
…m over

Snapshots of fallbacks nested 1, 3 and 8 deep, a fallback next to a
filled slot, one shown through a fill's fallback variable, slot data, and
fallbacks in loops and in pure and cached components. Each is recorded
with and without Vue, together with what the on_slot_rendered hook sees, a
cached component's replay, development mode, and errors raised inside
three fallbacks: an expression error, a StopIteration, a KeyboardInterrupt,
and errors from the slot hook, including a StopIteration it raises while
handling another error. Each error check also confirms that no slot
fallback is left paused. Everything here was recorded on 05b95e7 and
passes unchanged on 2fc91b2.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A <c-slot> that gets no fill renders its own body. That body used to
render through a Slot call that started the body renderer again, so each
nested fallback added 10 Python frames, and about 90 nested fallbacks
raised RecursionError under Python's default limit. SlotNode now hands its
fallback to the running body renderer through _render_steps, as IfNode and
ForNode do. It holds the same scopes around the body in the same order, so
the HTML, the Vue browser data, the on_slot_rendered hook, cache replay and
errors stay as the previous commit recorded them on main. Nested fallbacks
now render in the same number of frames at any depth.

The body renderer gains two things for this: BodySteps can pass the list
the parts go into, and an error leaving a slot's fallback is thrown into
its steps, so the slot is still added to the error's component path. A
StopIteration from template code or the slot hook is carried out of the
steps' generator and raised with the same type, message, component path
and context as before. A unit test covers each way an error can come back
out of the steps.

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