Repository navigation
perf: render nested slot fallbacks without a Python call per level - #397
Merged
Merged
Conversation
…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]>
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.
What this changes
A
<c-slot>that gets no fill renders its own body, its fallback. Beforethis change, each nested fallback added 10 Python frames, because the
fallback rendered through a
Slotcall that started the body renderer(
_render_body, the loop that renders a template body) again. About 90nested fallbacks raised
RecursionErrorunder Python's default limit,well below the 256 levels of Citry logic tags that the planned nesting
limit in #272 will allow.
SlotNode._render_stepsnow returns the fallback body to the bodyrenderer that is already running, the way
IfNodeandForNodehavehanded over their bodies since #356. Nested fallbacks now render in about
the same number of frames at any depth:
A passed fill still renders through a
Slotcall (_render_direct), asbefore. 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
CitryRenderclass, which_render_valuewould then treat differently;citry_render.pymoves thatcheck into
_render_passes_throughso both places use it.How the output stays the same
The generator that
_render_stepsreturns for a fallback does the samework as
_render_direct, in the same order. It makes the sameSlot,starts the same Vue execution, and holds the same scopes while the body
renders: the execution, then
add_slot_to_error_message(the scope thatadds the slot to an error's component path), then the execution again. It
wraps the result the same way and runs
on_slot_renderedat the samepoint.
BodySteps, the tuple a node returns to tell the body renderer whichbodies to render, gains two optional fields for this:
parts: the list the body's parts go into, so the slot can wrap themwhile its scopes are still open.
wraps_errors: an error leaving the fallback is thrown into the slot'sgenerator where it waits at its
yield, so thewithblocks there seeit and the slot is still added to the component path
(
Root > Root(slot:s0) > Root(slot:s1)).IfNodeandForNodeleave both at their defaults. AStopIterationfrom template code or the slot hook is carried out of the generator, which
Python would otherwise turn into a
RuntimeError, and raised with the sametype, message, component path and
__context__as before.Tests
test_render_body_snapshots.pygains fallback fixtures: 1, 3 and 8deep, next to a filled slot, shown through a fill's
fallbackvariable,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_renderedsees, a cached component's replay,development mode, and errors raised inside three fallbacks (an
expression error, a
StopIteration, aKeyboardInterrupt) or by thehook. 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.
holding its scopes. When a test leaves one paused on purpose, all 14
error tests fail.
test_render_depth_frames.pygains a template of nested fallbacks,comparing the deepest stack at depth 32 and at depth 128, also with an
on_slot_renderedhook and in development mode. All 8 cases fail if_render_bodystops passingSlotNodeto_render_steps._throw_into(which throws an error into a pausedfallback 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.<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 renderswhen 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):
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
NamedTuplethere and measured +2.2% to+2.5%.
Checks run
citrytest suite with a release build of this branch: everythingpasses. 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_flowframe 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, andmypyon thecitrypackage (also--platform linux).scripts/check.pyphase that rebuilds or compares a committedfile:
uv lock --check, protocol Python copies, the docs-playgroundbuild check, Citry UI assets, and the validators.
version, and one of this text. Their findings are folded in.
The root
CHANGELOG.mdgets one entry under Fixes.Part of #272.
🤖 Generated with Claude Code