Skip to content

perf: render a loop's simple="vue" rows in one render-queue task (native render, level C1) - #267

Merged
JuroOravec merged 2 commits into
mainfrom
perf/native-render-run-tasks
Oct 23, 2026
Merged

JuroOravec merged 2 commits into
mainfrom
perf/native-render-run-tasks

Conversation

@JuroOravec

@JuroOravec JuroOravec commented Oct 23, 2026 •

Copy link
Copy Markdown
Member

What this changes

Today a <c-for> loop over a simple = "vue" component gives the render queue one task per row. While the parent's template runs, the loop builds a CitryElement and a DeferredComponent for every row. The queue then pops each row's task, checks the depth, sets the row's execution scope, makes the row's admission checks, runs its callbacks and puts its record in place.

With this PR, the queue renders such a loop's rows in one task (_RunTask), in the same order:

  1. Before the loop. ForNode._call_run_class checks that the loop is a call run of simple = "vue" rows: the checks ForNode.render already makes after the loop, plus three more: the class is simple = "vue", the inputs are plain, and the call's metadata does not target an element.
  2. The loop walk. ForNode._render_call_run makes each row's template steps in their usual order, but records the row's kwargs and call metadata in one _DeferredRun instead of building its element. Each row's position in the parts list gets its own small _DeferredRunRow, so the list still has one object per row.
  3. The scan. Rows of one run that sit next to each other in a parts list get one run task. Their own tasks would sit next to each other on the stack, so one task that renders them in list order does the same work in the same order.
  4. The run task. _run_rows renders the rows in order: for each row the depth check, the execution scope, the admission checks that can change between rows, then the same steps as _render_simple_vue_leaf. Both now call one shared function, _simple_vue_record, which also makes level A's Rust row read when the Rust path is on.
  5. Rows that stop the task. A row that takes the ordinary component path, or calls child components, ends the task: the queue renders that row's work, with a new run task for the remaining rows under it, so the row's subtree finishes before the next row starts. A row that is too deep, or whose position is gone from the parts list, ends the render with the error its own task raises.

This level is Python only. It runs on every interpreter, on both the Rust path and the Python path, so PyPy, Pyodide and free-threaded builds use it too and Citry keeps one render queue. Output bytes and the order of every user callback and extension hook stay the same: the order records from #263 (test_call_run_order.py) pass unchanged on both paths.

Measured

Release wheels, Python 3.12, 10 alternating fresh-process pairs of 11 renders of the benchmark board; mean per process.

Comparison Rows Before After Change Pairs faster
#266 -> this PR, Rust path 1,400 62.5 ms 61.5 ms -1.7% 8 of 10
#266 -> this PR, Rust path 140 10.9 ms 10.9 ms -0.3% 6 of 10
#266 -> this PR, Python path (CITRY_NATIVE_RENDER=0 on both) 1,400 99.6 ms 97.1 ms -2.5% 6 of 10
#266 -> this PR, Python path 140 13.9 ms 14.1 ms +1.2% 2 of 10
main -> this PR, Python path (the whole stack so far) 1,400 98.2 ms 97.3 ms -0.8% 4 of 10
main -> this PR, Python path 140 13.9 ms 14.0 ms +0.9% 3 of 10

On the Rust path C1 is a small gain at 1,400 rows and level at 140. On the Python path, which PyPy, Pyodide and free-threaded builds run, it gains at 1,400 rows and costs about 0.17 ms (1.2%) at 140 rows, most likely the one small object per row added by the review fix. The plan's bar for the Python path was "not slower than main"; at 140 rows it misses that by about 1%, so this is flagged for the maintainer's decision. C1 ships mainly as the base that level C2 needs.

Notable implementation choices

  • One object per row position. The research put the one run object at every row's position. The review showed that a public hook (on_slot_rendered) that reorders or drops the unplaced rows, or a slot render printed again after some rows were placed, then renders different rows, or the same rows in a different order. With one _DeferredRunRow per position, the queue finds each row by identity, as it finds a DeferredComponent, and both cases match one task per row exactly.
  • One shared row function. The research copied the end of _render_simple_vue_leaf into the run task. Here both call _simple_vue_record, so the steps after the admission checks exist once. _render_simple_vue_leaf gains one function call per row it renders.
  • ComponentNode.render split. It now ends in _finish_render, so a row that leaves its run finishes as an ordinary call without resolving its inputs or looking up its class twice. This adds one method call to every component call.
  • Shared rejection messages. The three rejection reasons the run task can give are module constants used by both paths.
  • A shared step for placing a child. The settle loop's "place an ordinary child and queue its finalize" step is a nested function, place_child, used by the per-row task and the run task's ordinary-path exit.
  • A row's deferred component is built once. A row the queue meets twice on the ordinary path keeps one element, as one deferred component per row would.
  • Not shipped from the research: the Rust run task (row_runs.rs, _RowRunPlan) and the Python helper table it read, the CITRY_RESEARCH_RUN_TASKS switch and the counters.

Accepted differences (none changes output)

  • A row in a run shows _run_rows and _run_row in its traceback, where a row with its own task shows _render_simple_vue_leaf. The innermost frame, where user code raised, is the same.
  • Reads of class attributes and of Citry's own frozen records differ in count, and some in timing: the call's shape is checked once for the loop instead of once per row, and _call_run_class reads the class's simple, transparent and selector flags before the loop runs. Only a user metaclass with a read hook could see this.
  • CitryRender.parts can hold a _DeferredRunRow at a row's position where it held a DeferredComponent, until the queue places the rows. Neither class is exported from citry. Three public routes can hand user code such a render: calling a template-defined slot, the on_slot_rendered hook, and a nested template passed as a kwarg. In each, the list has one object per row and str() raises the error an unplaced deferred component raises; a test covers each route. on_render and on_component_rendered see a render only after its rows are placed.

Tests

  • tests/test_call_run_tasks.py (31 cases, under a second on four workers). Every case but the last runs on both paths:
    • C1 really runs: the test board at 140 rows (five phases of 28, and once with all rows in the page's own template) gives one run task per loop that places every row, with no ordinary-path exit and no row given its own task;
    • both ways a row's position can be gone (before its record is placed, and after an ordinary-path render): the same RuntimeError leaves the render past the on_render boundary, after the same callbacks;
    • cases compared with one task per row in the same process. Citry has no switch, so the second render makes no loop qualify for a run. They compare the output or error and every callback: an on_slot_rendered hook that reverses, drops or repeats the unplaced rows; a rows list printed again after its first row; a row that makes the class fail admission; an ordinary-path row that raises under a swallowing boundary; a class that becomes a dynamic selector between rows; and a parts list edited before its rows are placed;
    • the three public routes above;
    • a leftover row in a prepared render gets the same "unresolved runtime part" error as a leftover deferred component.
  • tests/test_call_run_order.py (91 cases) passes unchanged on both paths. It covers the faults, KeyboardInterrupt, the depth limit with and without a swallowing boundary, rows that change engine or class state for later rows, child calls, slot fills, the nested template printed twice, tracing and two threads.

Review

An independent reviewer (high effort) checked callback order, exception paths, re-entrancy and threads against a snapshot of #266. It found two major problems, both from the research design of one shared object at every row position: a hook that edits the unplaced rows, and a list scanned again after some rows were placed (a different callback order). Both are fixed by one object per row and locked by tests that compare with one task per row. It also found four inaccurate statements in the design doc, all corrected, and branches no test reached; tests now cover the ordinary-path error under a boundary and the selector flip. Four guards stay without a test because no page reaches them: simple changing between rows (the class's metaclass refuses that change), the registry identity check in a run, the root-call check, and a row seeing a different execution frame during the walk. A separate prose pass found about 30 items (four of them wrong statements); all were applied except shortening two existing headings this PR did not write.

The same reviewer then re-checked the fix against the #266 snapshot: 88 cases per path in which an on_slot_rendered hook edits the unplaced rows in 11 ways (reverse, delete, repeat, rotate, an empty string between rows, a row moved to another list, two runs interleaved, and others), each with one or two loops, ordinary-path rows and rows with a child. Bytes and callback order matched in every case. It found no blocker or major problem; its one test nit is applied. A second prose pass found one accidentally dropped paragraph (the level B1 test bullet in native_render.md, restored), two missing rows in the C1 error table (added) and smaller wording items, all applied.

Gates

  • Fast profile (scripts/check.py --profile fast --reporter agent, on the commit): "status": "PASSED", every phase passed.
  • mypy: clean, also with --platform linux (593 source files).
  • Python-path run (pytest -m "not e2e" -n 4 --python-render-path): 12,824 passed, 145 skipped, 1 xfailed.
  • Coverage (the full profile's pytest command with --cov, run locally on Python 3.14): 89.53%, against a ratchet of 88.5 (89.52% on perf: record each call run of Rust-read rows with one Rust object (native render, level B1) #266). The new lines no test reaches are guards no page can reach: the simple check and the registry and root-call checks in _run_row, the AssertionError after the position-gone raise, a row seeing a different execution frame during the walk, and three early returns in _call_run_class that the template parser or the plain-input check already rules out.

Cross-binding list

  • Python only: citry_render.py, nodes/__init__.py, component_render.py, _vue/capture.py, and two comments in serialize.py.
  • No Rust, PyO3, stub, grammar, AST, compiler or LangImpl change, and no new required name in native_render._REQUIRED_NAMES.
  • Docs: docs/design/native_render.md (new section 3, C1's error modes and accepted differences, code map, tests), docs/design/component_rendering.md, component_rendering_defer.md and component_on_render.md (the run task in the render queue), vue_optimization_rollout.md (prior art), docs/agent/INDEX.md, and a ledger row in performance_architecture_experiments.md marked "timing pending".
  • No changelog entry and no version bump, as for the rest of the stack.

Base

Rebased onto main after #263, #265 and #266 merged; the content is unchanged.

🤖 Generated with Claude Code

JuroOravec and others added 2 commits October 23, 2026 12:46
…ive render level C1)

A c-for loop over a simple='vue' component now records its rows in one
_DeferredRun, with one _DeferredRunRow at each row's position in the
parts list, instead of building a CitryElement and a DeferredComponent
per row. The render queue gives rows of one run that sit next to each
other one task (_RunTask), which renders them in the order their own
tasks would. A row that calls child components or takes the ordinary
component path stops the task, and the queue renders that row's work
with a task for the remaining rows under it; a row that is too deep or
whose position is gone raises the error its own task raises. Output
bytes and the order of every callback and hook are unchanged; the order
records from #263 pass unchanged on both render paths.

This level is Python only and runs on every interpreter, on both the
Rust and the Python path.

- citry_render.py: _DeferredRun and _DeferredRunRow (one object per row
  position, so the queue finds rows by identity when a hook reorders,
  drops or repeats them; the row's DeferredComponent is built once).
- nodes/__init__.py: ComponentNode.render ends in _finish_render;
  _render_run_row; ForNode._call_run_class and _render_call_run.
- component_render.py: _simple_vue_record, shared by
  _render_simple_vue_leaf and the run task's _run_row; _run_rows,
  _place_run_part, _RunTask and the settle loop's run branch with its
  four exits; place_child shared with the per-row task; the scan groups
  adjacent rows; _contains_deferred and _nesting_depth_error_for.
- _vue/capture.py: a leftover row is an unresolved part.
- Tests: test_call_run_tasks.py (the board spy on both paths, both
  position-gone exits, hook edits and a re-printed list compared with one
  task per row in the same process, rows that change later rows, the
  three public routes that see unplaced rows).
- Docs: native_render.md section 3 and C1's error modes;
  component_rendering.md, component_rendering_defer.md,
  component_on_render.md, vue_optimization_rollout.md, agent INDEX, and a
  ledger row with the timing pending.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@JuroOravec
JuroOravec force-pushed the perf/native-render-run-tasks branch from ef6c9cf to 1851f97 Compare October 23, 2026 10:46
@JuroOravec
JuroOravec merged commit 205fd8b into main Oct 23, 2026
6 of 7 checks passed
@JuroOravec
JuroOravec deleted the perf/native-render-run-tasks branch October 23, 2026 10:47
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