Skip to content

perf: read simple="vue" rows and hash call-run ids in Rust (native render, level A) - #265

Merged
JuroOravec merged 6 commits into
mainfrom
perf/native-render-rows
Oct 23, 2026
Merged

JuroOravec merged 6 commits into
mainfrom
perf/native-render-rows

Conversation

@JuroOravec

Copy link
Copy Markdown
Member

What this changes

On standard CPython, Citry now does part of each simple = "vue" row's work in Rust:

  • Rows. After a row's data callbacks run, Rust checks the values its template reads, copies them, evaluates the template and writes the row's manifest data as JSON. Serialize writes that JSON into the page's manifest without building the row's Python copy of its values. Any input Rust cannot reproduce exactly goes back to the Python evaluator, which renders it or raises the error Python raises for it.
  • Call-run ids. Rust computes the two SHA-256 ids of every row of a c-for over a component (ordinary components too), from the same text Python hashes.

Output bytes and the order of user callbacks and hooks stay the same on both paths. The callback and hook order tests from b5e9783 (test_call_run_order.py) now run on both.

When the Rust path runs

citry/_vue/native_render.py decides once, at import. The Rust path runs on CPython with the GIL, outside Pyodide, when citry_core._rust.native_render has every name. PyPy, Pyodide and free-threaded builds take the Python path with no warning. CPython with a missing or stale module takes it with one RuntimeWarning. CITRY_NATIVE_RENDER=0 turns the Rust path off; it is the only new user-facing name.

Two cases the maintainer asked to call out:

  • Development mode. A Citry(mode="development") page checks every element id it writes, so the serializer needs every row's HTML. This includes rows in child components, which the plan had assumed stay on the Rust path. The row read therefore skips Rust for a Citry instance in development mode, so development pages evaluate every row in Python as before; only the call-run ids still come from Rust, which costs no extra work.
  • Pages whose body the serializer cannot leave out (a root template that is not a full <html> document, any CSP setting or nonce, a JavaScript or script-integrity policy, deps_position other than "smart", a custom on_serialize, and a few rarer body contents; see docs/design/native_render.md section 3), plus Events updates and pages with a custom tag callback. These still read rows in Rust, then rebuild every row in Python. The measurement below times two such pages; neither is slower than main.

Measured

Release wheels of origin/main (736071308) against this branch, Python 3.12, 5 alternating fresh-process pairs of 11 renders of the benchmark board; mean per process, garbage-collection pauses included.

Page Rows main This PR Change Pairs faster
Hydrated (the default) 1,400 99.3 ms 71.4 ms -28.1% 5 of 5
Hydrated 140 14.1 ms 12.0 ms -15.0% 5 of 5
Fragment (every row rebuilt in Python) 1,400 108.2 ms 97.3 ms -10.1% 5 of 5
Fragment 140 15.3 ms 14.3 ms -6.4% 5 of 5
Strict CSP with a nonce 1,400 848.5 ms 832.2 ms -1.9%
Strict CSP with a nonce 140 69.1 ms 67.8 ms -1.9% 3 of 5

The plan's bar for this level was at least 20% at 1,400 rows and no slowdown at 140 rows. The strict CSP page is about 8.5 times slower than the hydrated page on main too; that cost is in the CSP path and is outside this PR.

Notable implementation choices

  • Native rows reach serialize as a field of _PreparedResult (native_rows), not through context.extra, so fix(deps): run on_dependencies() once per instance on interactive pages #260's SERIALIZATION_NOTE_KEYS is unaffected. _prepare_initial_result now always returns a _PreparedResult. The five tests that stub it are updated.
  • transform_component and the shorter per-row path for Rust rows (register_native_member) share one start, start_occurrence, so every row is checked against the registry exactly as before. Only the component tag lookup runs once per class per assembly; the precompile path accepts only the built-in tag callback.
  • The manifest writer puts the occurrence list between the other sorted entries (_manifest_json), instead of replacing a placeholder string, so no user value can collide with a placeholder.
  • _RowPlan defines __traverse__, so a dropped template's plan is collected (a test proves it). _NativeRow holds only Rust data and strings, so the collector does not track it.
  • Rust copies no Python limit: the root c-bind layout cache size and the JavaScript int limit arrive with the plan. The fixed text of the manifest format and the value rules are locked by fixtures generated from Python.
  • The render parity recorder (scripts/vue_render_parity/collect.py) writes the manifest with the Rust rows' data. Otherwise it would have recorded pages without their rows' data.

Tests

  • tests/_render_paths.py adds render_both. It runs a case on both paths in one process, compares bytes or errors, and checks how many rows Rust read and how many rows Python rebuilt, so a case cannot pass because both runs used Python.
  • tests/conftest.py adds the render_path and python_render_path fixtures and the --python-render-path option.
  • tests/test_native_render_rows.py (79 cases, about 4 s on one worker; the one 1,400-row case is marked qualification) covers:
    • every output kind, with rows in child components and in the root template;
    • an Events chain of four updates;
    • 18 faults, plus three ComponentLike registrations (before and after the render), each in its own interpreter;
    • the record copy, the flag switching between render and serialize, and the decision table;
    • the encoder's refusals, run-id fallbacks and garbage collection;
    • a check that the cargo fixtures match Python's output.
  • Tests that now ask for python_render_path: three tests in test_simple_vue_row_caches.py (18 ids) look inside the Python evaluator's own caches, so they run on the Python path explicitly.
  • test_simple_vue_mode.py, test_simple_vue_row_html.py and test_simple_vue_child_calls.py run on both paths (about 4 s more). The order tests also check that their Rust case really read rows.
  • Rust: crates/citry_native_render/tests/ checks fixtures that scripts/native_render_fixtures.py writes from Python (--check mode, run by a test). The fixtures total about 220 KB; the sdist leaves the crate's tests/ out.
  • CI: the ubuntu / Python 3.10 cell runs the whole suite with --python-render-path. Its check name does not change.

Cross-binding list

New crate crates/citry_native_render; crates/citry_core_py/src/native_render/; lib.rs registers the submodule with #[pymodule(gil_used = true)]; _rust.pyi (class native_render); scripts/validators/bindings.py accepts #[pymodule(...)] with options; scripts/smoke_citry_core.py runs a row through the reader, the occurrence writer and the run ids on every wheel; scripts/verify_citry_core_distribution.py requires the new crate's manifest; Cargo.lock. No grammar, AST, compiler or LangImpl change. No changelog entry and no version bump (the release PR owns them).

Stacked on #263

This PR is based on #263 (the callback-order tests). Merge #263 first.

Not in this PR

The later levels of the plan (one Rust object per loop in serialize, one render task per loop, the loop walk in Rust, and Rust JSON for ordinary leaf data).

🤖 Generated with Claude Code

JuroOravec and others added 6 commits October 23, 2026 11:44
…nder level A)

Work in progress: the Rust crate citry_native_render, the
citry_core._rust.native_render submodule, the decision module
citry/_vue/native_render.py, the Python integration, and the tests
that compare both render paths. Docs follow.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…and tests

Development pages write every row's HTML for the id check, so their
rows stay on the Python path. Adds docs/design/native_render.md, the
crate's AGENTS.md, the user-facing performance and compatibility notes,
both-path runs of the simple='vue' test files, and the remaining
level A tests.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Lists every page whose body the serializer keeps (they rebuild each row
in Python), renames the hand-back to a rebuild, defines the design
doc's terms, and adds coverage tests for the plan guards.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Rust receives the JavaScript int limit from Python instead of copying
it, the run-id methods turn panics into RuntimeError, the occurrence
writer reports an id with no UTF-8 form as ValueError, and the record's
native fields leave equality. The smoke script now runs a row through the
reader and the occurrence writer on every wheel; the order tests prove
the Rust case read rows; a ComponentLike registration after render is
tested; the fixture generator follows nested c-bind paths and the
fixtures shrink to about 220 KB.

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