Repository navigation
perf: walk a loop of simple="vue" rows in Rust when it reads only plain paths (native render, level C2) - #268
Merged
Merged
Conversation
JuroOravec
force-pushed
the
perf/native-render-walk
branch
from
October 23, 2026 09:44
88a039a to
3536dc3
Compare
JuroOravec
force-pushed
the
perf/native-render-walk
branch
from
October 23, 2026 10:09
3536dc3 to
d334e47
Compare
JuroOravec
force-pushed
the
perf/native-render-walk
branch
from
October 23, 2026 10:46
d334e47 to
de743a1
Compare
…in paths (native render level C2) When a c-for call run's iterable, call inputs and #c-key are plain paths (a name and constant subscripts), Rust walks the loop through native_render._CallRunWalkPlan: it reads every row with exact-type checks, then calls the registry's get and builds each row's kwargs dict and call metadata. Python puts them in the _DeferredRun and _DeferredRunRow objects level C1's run task renders. Anything Rust cannot read exactly as Python does declines with a reason, and Python's walk runs the loop from the start. citry/_vue/native_walk.py turns each expression into a path from both Python's parse and the sandbox's generated code, and refuses the loop when the two differ. A loop whose engine overrides Citry.get keeps Python's walk. Rust path only. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
…evels The loop walk (level C2) takes the wheel 354 bytes past the 1,360 KiB cap, and level B2 adds about 3 KB more. The new cap leaves about 12.8 KB of headroom with every level in. Co-Authored-By: Claude Opus 5.5 <[email protected]>
JuroOravec
force-pushed
the
perf/native-render-walk
branch
from
October 23, 2026 11:23
de743a1 to
b001252
Compare
This was referenced Oct 23, 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
Level C1 (#267) records a
<c-for>loop'ssimple = "vue"rows in one_DeferredRun, which one render-queue task renders. The loop itself still runs in Python: for each row, Python's walk (ForNode._render_call_run) copies the template's variables, evaluates each input with the sandboxed evaluator, looks the class up with the registry'sget, evaluates the key and builds the call metadata.With this PR, when the loop's iterable, the call's inputs and its
#c-keyare plain paths (a name and constant subscripts, as in<c-Row c-for="row in phase['outputs']" #c-key="row['id']" c-row="row" />), Rust does that walk:ForNode._build_walk_planturns the loop clause, the key and each input into paths (citry/_vue/native_walk.py) and builds a_CallRunWalkPlan. Each expression is turned into a path twice, from Python's own parse of the text and from the Python code the expression sandbox generated for it, and the loop gets no plan unless both agree. The sandbox reads a lone surrogate escape such as'\ud800'as U+FFFD while Python's parser keeps it, so the two evaluators would otherwise read different keys.ForNode._walk_call_runleaves the loop to Python when tracing is on or when the variables are not exactly Citry's_ConstMapping. It then checks, as Python's walk does, that the loop variable's name is not already in scope, and leaves the loop to Python when an input reads a scope name marked constant or when the engine'sgetis notCitry.get.dict,listortuple; a key that is an exactstror anintwithin i64). It follows a list's current length before every item, as Python's list iterator does, and keeps the objects it read, never copies.getfor each row, checks it returned the run's class, and builds the row's kwargs dict and_PreparedCallMetadata._DeferredRunwith one_DeferredRunRowper row position, exactly what Python's walk records, and level C1's run task renders the rows as before.Anything Rust cannot read exactly as Python does makes it return a reason, and Python's walk runs the loop from the start, so every error is Python's own, with its template position. Output bytes and the order of every user callback and extension hook are unchanged on both paths: the callback-order tests from #263 (
tests/test_call_run_order.py) pass unchanged. This level runs only on the Rust path; the Python path keeps C1's walk.Measured
Release wheels, Python 3.12, alternating fresh-process pairs of 11 renders of the benchmark board; mean per process.
main-> this PR, whole stack so far (5 pairs)main-> this PR, whole stack so far (5 pairs)The bar was at least 3 ms on the collection-free median at 1,400 rows; it measured 4.2 ms (4.1 ms on the mean).
Notable implementation choices
research/rust-render-stage0) refused only surrogate escapes. Comparing Python's parse with the sandbox's generated code catches any literal the two read differently, and refuses generated code of a shapenative_walk.pydoes not know. The board's five loops all get a plan, which a test checks.__eq__can change it). Rust now re-reads the length before every item, so such a loop gives what Python's walk gives without a decline._ConstMapping. The research also walked a plaindictscope. Python's walk copies a plain dict scope for each row by inserting every key again, which runs a non-strkey's__hash__once per row; Rust would not. Citry always builds_ConstMapping; only an extension that replacescontext.variableswith a plain dict reaches this decline.getkeeps Python's walk. ACitrysubclass'sgetis user code, which Python's walk calls between each row's reads and Rust would call only after all of them; such a loop declines (custom_get).MemoryErrororRecursionError) makes the walk decline, so Python's walk raises it with the row's template position.walkreturns the reasonpanic, because the walk keeps nothing it built. A panic in the plan's constructor raisesRuntimeError, which_build_walk_planturns into "no plan".SUPPORTED. A loop's plan, or the reason it has none, is kept on the node; it is never built or used on the Python path, so a Python-path render cannot leave "no plan" cached for a later Rust-path render.CITRY_RESEARCH_RUST_WALKswitch, the walk counters andcount_walk_decline, and thechangeddecline.Accepted differences
None changes output unless the named user code has side effects.
__eq__runs again after a decline, because Python's walk reads the rows again. On a walked loop, a key among the template's variables that collides with the loop variable's name or a constant name is compared fewer times; one inside a row, or colliding with another scope name, is compared as often as in Python's walk (tests count both). A colliding__eq__that rebinds a template variable is seen by the same row in Rust and only by the next row in Python's walk.getraises or returns another class at row k, thegetcalls of rows 0 to k-1 run twice (a test counts them). This is the one change in how many timesgetruns, and only another thread's registry change can reach it.component.citry.getand the execution frame are read once per walk, and the class'ssimpleand dynamic-selector flags once before the loop, not once per row.getis called. If it changes a row in place, the walk does not see the change: the rows hold the values Rust read just before.__builtins__in the variables while it evaluates the iterable; Rust writes nothing.Tests
tests/test_native_render_walk.py(127 cases, about 1.8 s on four workers):render_both, one for each decline reason a page can reach (no_plan_loop,no_plan_key,no_plan_input,tracing,scope,const_input,custom_get,iterable,missing,not_plain,marker,key_type,read_raised,get_raised,class_changed) and for each kind of value the walk reads (tuples, int and extreme int keys, list and mixed rows, a dict keyedTrueread byrow[1], a deeper iterable path, a scope input, aConstmarker inside a row, an empty loop, the sandbox off);test_call_run_order.pythrough the walk, including a row that changes the next row's dict before it renders, and two threads rendering together;getthat raises or is interrupted, a failing row build, aPanicException, the constructor's refusals, the collector seeing every held object and collecting a cycle through the plan, and no leaked references.render_bothnow also records the loops Rust walked and each decline reason, and checks the Python path never walks in Rust.crates/citry_native_render/tests/walk_rows.rsagainstwalk_rows.json(24 loops over JSON data, values from Python's sandboxed evaluator), written byscripts/native_render_fixtures.py, whose--checka Python test runs.Surrogate keys read differently by the sandbox
A related bug is real, older than this stack, and wider than
simple = "vue": the sandbox reads'\ud800'as U+FFFD while the fast leaf evaluator and the unsandboxed path keep U+D800, so even an ordinary component reads a different key depending on its template's shape and its data. The fix belongs in the sandbox's code generator, outside this PR; an issue draft with reproductions is ready to file. Level A refuses such a key already (the comment and test now say why), and level C2 refuses any path whose two readings differ.Review
An independent reviewer (high effort) attacked the equivalence of the reads (exact types, colliding
__eq__, lists changed during the walk, threads, generators,Constmarkers, constness, tracing, the sandbox on and off), the panic and decline paths, reference counts and the collector, with throwaway probes comparing both paths. It found no blocker and no case where a page without side-effecting user code renders differently. Its findings and what was done:Citry.getis user code, and Rust would call it after all the reads instead of between them. Fixed: such a loop now declines (custom_get), with a page case whosegetadds a row.__eq__that rebinds a template variable is seen one row earlier in Rust. Added to the accepted differences.__eq__compares was wrong (copying a_ConstMappingcompares nothing; the extra compares come from adding the loop variable and looking up constant names). Corrected in the doc and a test comment.RecursionError; the doc's "every value's exact type" (only containers on a path and the key are checked); aPanicExceptionalso runssys.excepthook; the collector test now checks every held object and collects a cycle through the plan; the fixture generator records aNonekey as Python's walk does; step keys are interned.A separate prose pass checked every changed doc, comment, docstring, the stub, the commit message and both drafts, and found three wrong claims (the order of the checks before Rust runs, the fixture count, "the one call-count change"), one missing accepted difference, and about 35 wording items. All were applied.
Gates
scripts/check.py --profile fast --reporter agent, on the commit):"status": "PASSED", every phase passed.--platform linux(251 source files).pytest -m "not e2e" -n 4 --python-render-path): 12,951 passed, 145 skipped, 1 xfailed. It ran before the review fixes, which change only code the Rust path runs.--cov, run locally on Python 3.14): 89.56%, against a ratchet of 88.5 (89.53% on perf: render a loop's simple="vue" rows in one render-queue task (native render, level C1) #267). The one new line no test reaches is a guard innative_walk.pyfor generated code whose loop element is not the loop variable, which the lowering's own wrapping rules out.Cross-binding list
crates/citry_native_render/src/walk.rs(pure),crates/citry_core_py/src/native_render/walk.rs(PyO3,_CallRunWalkPlan), registered innative_render/mod.rs; the_rust.pyistub;_CallRunWalkPlanadded tonative_render._REQUIRED_NAMESand to theSUPPORTED/ACTIVEdecision test intest_native_render_rows.py.citry/_vue/native_walk.py(new),citry/nodes/__init__.py(ForNode), one comment incitry/_vue/leaf_program.py.LangImplchange; the template parser is not involved.docs/design/native_render.md(new section 4, the C2 error modes and accepted differences, code map, tests), a ledger row inperformance_architecture_experiments.mdwith the measured figures,docs/agent/INDEX.md, and the crateAGENTS.mdfiles.Base
Rebased onto
mainafter #263, #265, #266 and #267 merged.Wheel size cap
This level takes the citry wheel 354 bytes past its 1,360 KiB release cap (1,392,994 bytes). The last commit raises the cap to 1,376 KiB in
scripts/verify_citry_distribution.py, as agreed; level B2 (#269) adds about 3 KB more and still leaves about 12.8 KB of headroom.🤖 Generated with Claude Code