Skip to content

perf: record each call run of Rust-read rows with one Rust object (native render, level B1) - #266

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

JuroOravec merged 2 commits into
mainfrom
perf/native-render-runs

Conversation

@JuroOravec

@JuroOravec JuroOravec commented Oct 23, 2026 •

Copy link
Copy Markdown
Member

What this changes

Level A (#265) has Rust read each simple = "vue" row. Assembly still records every row of a c-for over such a component in a Python loop, one row at a time. That loop computes the row's ids, checks them against what assembly has already seen, writes the row into about a dozen sets and maps, and records one occurrence.

With this PR, when Rust read every row of a call run (the rows one c-for places at one component call site), one Rust object, _NativeRun, records the whole run during serialize:

  1. Class checks, once per run. take_native_run makes the checks that depend only on the class: the registry lookup, the component tag, and that the class is the one registered for this engine and not the built-in mark component.
  2. Read and name the rows. _NativeRun.try_new reads every row's record and checks that each row qualifies: the same class, class id, template fragment and call site as the first row, an exact str key and render id with a UTF-8 form, and no js_data or root markers. It computes both ids of every row and changes nothing in assembly.
  3. Check, then write. commit checks every row against assembly's live state, and only then writes all rows into the maps, in row order.
  4. Final checks. check_invariants makes the end-of-assembly checks for the run's rows.
  5. The view and the manifest. The view lists one _NativeOccurrence per row, in the same place as before. The manifest's occurrence list holds the run object once, and _encode_occurrences writes all its rows' items from Rust.

If try_new declines or commit refuses, the per-row loop records the run exactly as before and raises any error itself. Output bytes, error types and messages, and the order of user callbacks are unchanged on both paths.

This level runs only on the precompile path. It does not run for Events updates, for pages with a custom tag callback or a browser hook, or in development mode (which reads no rows in Rust).

Measured

Release wheels of #265 against this branch, Python 3.12, 5 alternating fresh-process pairs of 11 renders of the benchmark board; mean per process.

Comparison Rows Before After Change Pairs faster
#265 -> this PR 1,400 71.3 ms 64.1 ms -10.0% 5 of 5
#265 -> this PR 140 12.0 ms 11.0 ms -8.1% 5 of 5
main -> this PR (A and B1) 1,400 100.1 ms 63.9 ms -36.2% 5 of 5
main -> this PR (A and B1) 140 14.1 ms 11.1 ms -21.0% 5 of 5

The bar was at least 3 ms on the collection-free median at 1,400 rows; it measured 4.8 ms (9.0 ms on the mean).

Notable implementation choices

  • Definition order. native_definition is split into native_artifact (a cache keyed by the fragment) and register_artifact. Assembly registers a run's definition only after commit succeeds, because the order of first registration is the order the manifest lists definitions in.
  • The per-row loop is now a function. It moved, unchanged, into the nested function run_members. take_native_run runs before it.
  • Shared final-check messages. The two final-check messages are module constants (_RUN_ROW_CHECK_MESSAGES), so the run path and the per-row path raise the same text.
  • Manifest ids. _PreparedResult gains run_definition_ids, the logical-to-compiled definition ids, set only when the manifest holds a run. _manifest_json passes them to the encoder.
  • Readers of the view and the manifest. prepared_manifest checks each run's definition once for opaque HTML. The Events preparation skips run objects in its opaque HTML and renderId loops; an Events instance matching a run row would raise AssertionError, which cannot happen. Serialize reads a run's type_key for its component tags. The Vue render parity recorder reads definition ids back from the written JSON. AssembledView.occurrences and PreparedViewMetadata.occurrences are now typed PreparedOccurrence | _NativeOccurrence, so mypy checks every reader against the attributes a _NativeOccurrence has.
  • Class handles. native_render.NativeRun and native_render.NativeOccurrence hold the Rust classes for the type(x) is checks. Where the module is not used, they hold a class that no object belongs to.
  • Run ids. RunIds in the pure crate (run.rs) now computes the two ids for both levels; _RunDigests uses it too.
  • Errors while reading a row. try_new declines when reading a row raises an ordinary exception, so the per-row loop meets it at its own row. KeyboardInterrupt and SystemExit propagate.
  • Not tracked by the garbage collector. Neither new class defines __traverse__, so the cyclic garbage collector does not track them; a test checks this.

Accepted differences (none changes output)

  • The registry lookup for a run's class runs once per run instead of once per row. If another thread replaces the class while serialize runs, the later check that the class is still registered fails instead, with a different message.
  • When the built-in metadata has no tag for a run's class, serialize asks the tag callback once per run instead of once per row.

Tests

  • tests/_render_paths.py: render_both gains runs=(built, declined, in manifests). A stand-in for the Rust module counts what try_new builds and declines, and a spy on prepared_manifest counts the run objects the manifests hold. This counts each manifest once even when the Vue render parity recorder writes it a second time.
  • tests/test_native_render_runs.py (56 cases, about 1.5 s on four workers):
    • 14 page cases (the 13 from the design, plus one where the row class is unregistered between render and serialize). Each runs through render_both with its run counts. A repeated key in one run makes try_new decline, and a key repeated across two runs makes the second run's commit refuse.
    • B1 really runs: the test board at 140 rows in five phases of 28 rows gives 5 runs built and written, none declined, no row rebuilt. The benchmark app itself needs Django, so the test uses the suite's board in the benchmark's shape.
    • A 30-row assembly compared field by field with the per-row loop's assembly: occurrences, definitions in order, compile inputs and both render maps. Also the encoder's run items against the per-row items.
    • Direct binding cases with synthetic maps:
      • 26 try_new declines;
      • 5 commit refusals, each leaving every map unchanged, and a clean commit in row order;
      • 4 check_invariants cases;
      • holds_any;
      • GC tracking and read-only row data;
      • the encoder's refusals;
      • the adjacency assertion;
      • the interrupt case.
  • test_native_render_rows.py: the 18 fault cases now also state their run counts (FAULT_RUNS), and the decision table gains a module that has level A's names but not level B1's.
  • Rust: crates/citry_native_render/tests/run_items.rs checks ids, repeats and item bytes against run_items.json. scripts/native_render_fixtures.py writes that fixture from Python, and its --check mode covers it.

Gates

  • Fast profile (scripts/check.py --profile fast --reporter agent): "status": "PASSED", every phase passed.
  • mypy: clean, also with --platform linux.
  • Python-path run (pytest -m "not e2e" -n 4 --python-render-path): 12,793 passed, 145 skipped, 1 xfailed.
  • Coverage (the full profile's pytest command with --cov, run locally on Python 3.14): 89.52%, against a ratchet of 88.5. The coverage settings leave out assemble_typed_render (a line of its signature matches the ... exclusion), so B1's assembly code in direct_capture.py is not counted. Of the counted B1 lines, only the Events-instance AssertionError is uncovered, and no page can reach it. A separate focused run without that exclusion shows four uncovered B1 lines in assembly, all defensive: a first row with no call metadata, a registry class from another engine or the mark component, a failed check_invariants (it needs a digest collision), and an unfinished definition in the view build.

Cross-binding list

  • New: crates/citry_native_render/src/run.rs and crates/citry_core_py/src/native_render/runs.rs.
  • crates/citry_native_render/src/lib.rs exports RunIds, first_repeat, write_occurrence_item and OccurrenceItem.
  • rows.rs: _encode_occurrences gains definition_ids and writes run items, and _RunDigests now uses RunIds.
  • mod.rs registers the two classes.
  • _rust.pyi has the two classes and the new definition_ids parameter.
  • native_render._REQUIRED_NAMES gains _NativeRun and _NativeOccurrence, so an older module warns and falls back.
  • Python wrapper: citry/_vue/native_render.py gains the NativeRun and NativeOccurrence class handles.
  • No grammar, AST, compiler or LangImpl change, and no change to crates/citry_core_py/src/lib.rs.
  • No changelog entry and no version bump: a changelog edit starts the release-candidate workflow, so the release PR updates the existing Performance line with the measured figure for the whole stack.

Base

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

🤖 Generated with Claude Code

JuroOravec and others added 2 commits October 23, 2026 12:09
…tive render level B1)

When Rust read every row of a call run (the rows one c-for places at one
component call site), assembly on the precompile path now records the
whole run with one Rust object, _NativeRun, instead of one Python record
per row, and the manifest writer writes the run's rows from it. Any row or
assembly state the run object cannot reproduce exactly sends the run back
to the per-row loop, which raises its own errors, so output bytes, errors
and callback order stay the same on both paths.

- crates/citry_native_render/src/run.rs: RunIds (shared with level A's
  _RunDigests), first_repeat, write_occurrence_item; fixture test
  run_items.rs with run_items.json written by scripts/native_render_fixtures.py.
- crates/citry_core_py/src/native_render/runs.rs: _NativeRun (try_new,
  commit, check_invariants, member_ids, occurrences, holds_any) and
  _NativeOccurrence; _encode_occurrences writes run items given
  definition_ids. try_new declines on an ordinary exception while reading
  a row and lets KeyboardInterrupt and SystemExit through.
- direct_capture.py: the per-row loop moves into run_members;
  take_native_run runs first; definition registration splits into
  native_artifact and register_artifact so a declined run cannot change
  definition order; the final check calls check_invariants; the view
  expands each run in place.
- protocol.py, events.py, serialization.py, the parity recorder: handle
  run objects in the view and the manifest; _PreparedResult carries
  run_definition_ids.
- Tests: test_native_render_runs.py (page cases with run counts, the
  140-row spy, the assembly comparison, binding refusals), run counts on
  the level A fault cases, render_both gains runs=(built, declined, held).
- Docs: native_render.md section 2 and error modes, AGENTS.md files, a
  ledger placeholder row for the timing.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@JuroOravec
JuroOravec force-pushed the perf/native-render-runs branch from dc1b647 to 8ecdf56 Compare October 23, 2026 10:09
@JuroOravec
JuroOravec merged commit 6324df1 into main Oct 23, 2026
28 of 29 checks passed
@JuroOravec
JuroOravec deleted the perf/native-render-runs branch October 23, 2026 10:36
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