Repository navigation
perf: write the JSON of ordinary components' leaf data in Rust (native render, level B2) - #269
Merged
Merged
Conversation
JuroOravec
force-pushed
the
perf/native-render-leaf-json
branch
from
October 23, 2026 09:44
1430ef5 to
9fb5eb6
Compare
JuroOravec
force-pushed
the
perf/native-render-leaf-json
branch
from
October 23, 2026 10:09
9fb5eb6 to
c2d8654
Compare
This was referenced Oct 23, 2026
JuroOravec
force-pushed
the
perf/native-render-leaf-json
branch
from
October 23, 2026 10:46
c2d8654 to
844b7c2
Compare
JuroOravec
force-pushed
the
perf/native-render-leaf-json
branch
from
October 23, 2026 11:23
844b7c2 to
dd4ca71
Compare
…e render level B2) When assembly attaches the leaf data of an ordinary (not simple='vue') component's occurrence on the precompile path, native_render._leaf_values_json writes each value's JSON as _script_json(_json_plain(value)) would, and assembly stores one shared placeholder object under each key instead of a copy. _encode_occurrences then writes the occurrence's item with Rust's JSON in place of each placeholder. Anything Rust cannot write exactly returns a reason, and Python copies the leaf as before, so every error, the citry.Secret refusal included, stays Python's. _manifest_json now writes the manifest's entries in sorted key order, so a value that cannot be written raises the error _script_json(manifest) would raise first. Rust path only. Co-Authored-By: Claude Opus 5.5 <[email protected]>
…edger Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…per row Co-Authored-By: Claude Opus 5.5 <[email protected]>
JuroOravec
force-pushed
the
perf/native-render-leaf-json
branch
from
October 23, 2026 11:27
dd4ca71 to
46c45d3
Compare
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
An ordinary component (not
simple = "vue") whose template calls no other components and has no slots compiles into one leaf program, and its render records the values that program sends to the browser (its leaf data). Asimple = "vue"row that level A did not read records such a leaf too. Today assembly copies each of those values into the occurrence's prepared data, value by value, and the manifest writer later turns the copy into JSON.With this PR, on the precompile path (when no browser hook can read or change prepared data) and on the Rust path:
attach_leaf_dataasksnative_leaf_json.take. Rust (_leaf_values_json) writes each value's JSON, byte for byte what_script_json(_json_plain(value))writes, and returns a_LeafJson. No Python code runs while it writes._LeafJsonin the dict that already carries level A's rows to the manifest writer._encode_occurrenceswrites that occurrence's item with its keys in sorted order and each placeholder's JSON in its place. Other values are written as_script_jsonwrites them.Rust writes only exact plain data:
None,bool, anintwithin 64 bits, a finitefloat, astrwith a UTF-8 form, and exactdict(with exactstrkeys),listandtuple, nested at most 200 deep, with no container inside itself. For anything else it returns a reason, and assembly copies the leaf in Python as before, so every error is Python's own, including thecitry.Secretrefusal and its message. Leaf programs have already turned their data into text, ints and bools, so the declines a page reaches are a lone surrogate in text and an int past Python's digit limit in an attribute; both then fail or succeed exactly as without Rust.Output bytes, errors and the order of user callbacks are unchanged on both paths; the callback-order tests from #263 pass unchanged. This level runs only on the Rust path.
Measured
Release wheels, Python 3.12, alternating fresh-process pairs of 11 renders of the benchmark board; mean per process.
The bar was at least 1.5 ms at 1,400 rows; it measured 3.6 ms on the mean (2.6 ms on the collection-free median).
The whole stack against
mainThe same method,
origin/main736071308against this branch (all five levels), 5 pairs:mainFirst and second page load through the real server (Server-Timing
prepare, median of 10 fresh servers per side): at 1,400 rows first load 155.3 -> 111.4 ms (0.72 ofmain) and second load 103.1 -> 54.7 ms (0.53); at 140 rows 60.0 -> 56.3 ms (0.94) and 14.1 -> 10.6 ms (0.75). Every side sent the same bytes.Notable implementation choices
takereturns the reason. It returns the_LeafJsonor the decline reason as a string, as level C2's walk does, so the tests can see which case declined. Nothing in the shipped code counts declines._LeafJsonis not tracked by the garbage collector. It holds only Rust strings and the placeholder, so it can be in no reference cycle, as with level B1's classes. The plan had proposed__traverse__; a test checks that a_LeafJsonis untracked and holds no reference to the values it wrote.None, an exactstrwith a UTF-8 form and an empty dict, and calls_script_jsononly for the rest (a non-emptyserverData, an EventseventContext). The research called_script_jsonfor 8 to 10 values per item._manifest_jsonnow writes the entries beforeoccurrences, then the occurrences, then the rest, the order_script_json(manifest)writes them in. A page with such leaves and no Rust rows now takes this writer, so a value that cannot be written raises the error that call would raise first.ValueErrorwhen a placeholder has no JSON, when a value Rust wrote has no placeholder in the item (which would drop data silently), and when an item key is not a str. Assembly never makes such an item.owner_valuesraisesAssertionErrorif one ever did.CITRY_RESEARCH_RUST_LEAF_JSONswitch, the leaf counters andcount_leaf_json_decline, and_LeafJson.value_json.Accepted differences
None changes output.
RecursionError. A test shows it.PreparedOccurrenceequality includesprepared_data, and every placeholder is the same object, so two such occurrences whose leaf values differ would compare equal. Nothing compares them on this path.Tests
tests/test_native_render_leaf_json.py(88 cases, about 0.4 s on one worker):takeshows all 4 ordinary leaves written and no decline, with 140 rows read and none rebuilt;render_both, each checking that its pages are pages and not errors: awkward text (quotes,</script>, control characters, U+2028, an astral character), three calls sharing one definition so both attach places run, the two reachable declines (a lone surrogate beside two leaves Rust writes, and an int past the digit limit in an attribute), asimple = "vue"row Rust did not read, whose leaf Rust then writes, the flag switched off after assembly, a leaf beside a run of Rust rows,js_data, aSecretinjs_data(Python's refusal and message), aSecretin text, an attribute and a spread (masked before Rust sees it),Markup, an Events component, development mode, and a browser hook (no leaf written);json.dumpsand kept bycopy,deepcopyandpickle, and a_LeafJsonuntracked by the collector with no leaked references.render_bothnow also counts the leaves Rust wrote and records each decline reason, and checks the Python path writes none.SUPPORTED/ACTIVEdecision test gains a module with level C2's names but not level B2's.Gates
scripts/check.py --profile fast --reporter agent, on the final tree):"status": "PASSED", all 20 phases passed. An earlier run failed only in the Vue render parity phase, on this PR's own test pages: a DEL character (U+007F) in hydrated page text fails that phase's parse check on both paths. DEL now appears only in the direct byte checks (see "For the maintainer" below).--platform linux(254 source files).pytest -m "not e2e" -n 4 --python-render-path): 13,038 passed, 145 skipped, 1 xfailed. It ran before the last review fixes, which add tests and an encoder check on the Rust path only.--cov, run locally on Python 3.14): 89.56%, against a ratchet of 88.5 (89.56% on perf: walk a loop of simple="vue" rows in Rust when it reads only plain paths (native render, level C2) #268).native_leaf_json.pyis fully covered. The coverage settings leave outassemble_typed_render, so B2's assembly code is not counted; its two defensiveAssertionErrors are reachable from no page.fmt --checkandclippy -D warningsclean.Cross-binding list
crates/citry_core_py/src/native_render/leaf_json.rs(new, PyO3 only; the pure crate is unchanged), registered innative_render/mod.rs;rows.rs's_encode_occurrencesgains the leaf branch and sharespython_float_repr._LeafJson,_leaf_values_jsonand the encoder'snativestype in_rust.pyi; both names added tonative_render._REQUIRED_NAMES.citry/_vue/native_leaf_json.py(new),citry/_vue/direct_capture.py(attach_leaf_data,owner_values),citry/_vue/serialization.py(_manifest_json), comments inevents.py,prepared.pyandnative_render.py.LangImplchange.docs/design/native_render.md(new section 5, the B2 error modes and accepted differences, code map, tests), a ledger row inperformance_architecture_experiments.md(with the measured figures), one paragraph on the performance page,docs/agent/INDEX.md, and theAGENTS.mdfiles ofcitry_core_pyandcitry_core.Review
An independent reviewer (high effort) compared Rust's JSON with Python's on 35 hand-picked values and 20,000 fuzzed items, probed the item writer, every reader of prepared data between assembly and the manifest writer,
citry.Secret, the flag, Events, development mode and the collector. It found no blocker and no byte or error difference. Its findings and what was done:big_int, through an attribute holding an int past Python's digit limit. Fixed the claim everywhere and added a page case.simple = "vue"row that level A declined. Fixed the wording and added a page case (leaves=(3, ())).ValueError; tested.fallbackruns" error is described as a lost key; the depth comment counts two frames per level on Python 3.10 and 3.11; every success case checks it got pages, not errors; a test switches the flag off after assembly;takechecks the two guarded keys with two lookups; the placeholder's no-cycle requirement is stated on_leaf_values_json.simple = "vue"rows does not say it either.leaf_json.rshas no Rust unit tests or fixtures (the cross-binding table planned none for B2): it lives in the PyO3 layer, and the Python byte comparisons cover it; the JSON string and float writers it uses are level A's, which have fixtures.A separate prose pass checked every changed comment, docstring, the stub, the docs, the commit message and this draft. It found three misleading statements (the performance paragraph sat under a list of cases that do not apply to it, a test comment gave the wrong reason for its outputs, a frame count) and about 30 wording items. All were applied, including the paragraph's own limits (it applies in development mode and with
js_data()).For the maintainer
Base
Rebased onto
mainafter the rest of the stack (#263, #265-#268) and the research record (#275) merged. Two small commits on top: the wheel-size note inscripts/verify_citry_distribution.pynow gives the size with B2 (1,396,226 bytes, about 12.8 KB under the 1,376 KiB cap), and one accepted-difference bullet innative_render.mdnow says correctly that Rust-read rows are still checked against the registry for every row.🤖 Generated with Claude Code