Repository navigation
perf: share a parsed template's lists instead of copying them on each read - #527
Merged
Merged
Conversation
… read PyO3 copies a field each time Python reads it, and a template's child lists held everything below them. Each node also holds the variable uses and comments of its whole subtree, so one read near the root copied about depth² values, and every `citry check` walker that stepped down the tree paid depth³ (#384). At the nesting limits one component took up to a minute to check. The child lists and the per-node lists of variables and comments are now `Shared<Vec<_>>`, an `Arc` that copies of the tree share. A read copies one node; the lists below it stay shared. Python sees no change: the same classes, fields, `repr`, `==` and constructors. - `Shared` converts to and from Python as its value does, compares and prints the value, and has no `DerefMut`. Writes go through `Shared::make_mut`, which copies only a list something else still holds. While the parser builds a tree, only `Node::from_start_and_end_tags` does that, as before. - `Template`'s `Clone` now shares its four lists, so it needs no work list. `Drop` still frees the tree from a work list, and skips any list another copy still holds; that copy frees it. The used-variables pass takes each list out of its `Shared` without a copy. - New tests check that a cloned template and node share their lists, that compiling leaves the Python-held template unchanged, and that a deep body held after its root is dropped still frees on a small stack. - The nesting-limit test now runs `citry check` on all four shapes, and the frame-budget test on four of its 14 fixtures instead of two. The others were written for rendering: their findings change with depth, or their innermost level holds no name the check reports. Together they add about 1.3 seconds on a debug build. Debug build, `check_project` on one component. Before: one run on main at 82a2b94. After: median of 5 runs, rebased on d6e2212. template depths before after <c-if><div #c-ignore> 128 -> 256 6.73 -> 48.2 s 0.107 -> 0.206 s <c-if> 128 -> 256 1.90 -> 20.5 s 0.105 -> 0.239 s <div> 256 -> 512 1.82 -> 7.88 s 0.114 -> 0.283 s <c-if>..<c-elif> chain 128 -> 256 6.69 -> 27.4 s 0.248 -> 0.665 s The c-elif row grows 2.7 times per doubling, above the plan's 2.5 limit. A profile puts most of the excess in the Vue compile that `citry check` runs (#501), and a little in `line_break_count`. On a release build, parsing, compiling and checking all of citry_ui is 2 to 3% faster. Fixes #384. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This was referenced Oct 28, 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 does
Fixes #384.
citry checkslowed down with roughly the cube of a template's nesting depth: at the nesting limits, checking one component took up to a minute. It now takes less than a second.Why it was slow. The parsed template is a tree of Rust structs that are also the Python classes. PyO3 copies a field each time Python reads it, so reading a node's
bodyor a template'selementscopied everything below it. Each node also holds the variable uses and comments of its whole subtree, so one read near the root copied about depth² values. Everycitry checkwalker that stepped down the tree paid depth³.The change. The child lists, and the per-node lists of variables and comments, are now
Shared<Vec<_>>, anArcthat copies of the tree share. A read copies one node, and the lists below it stay shared. Python sees no change: the same classes, fields,repr,==and constructors, and_rust.pyiis unchanged. This is the design Juro approved in the plan for #384.Sharedbehaves. It converts to and from Python exactly as its value does, compares and prints the value, and has noDerefMut. Every write goes throughShared::make_mut, which copies a list only when something else still holds it, so any copy is visible in the code. While the parser builds a tree, onlyNode::from_start_and_end_tagscopies that way, as it did before.Template'sClonenow shares its four lists and no longer walks the tree.Dropstill frees the tree from a work list, and skips any list that another copy still holds; that copy frees it.Sharedwithout copying, because the parser holds the only reference.Measurements
Depth doubling (the plan's acceptance check). These are debug builds, timing
check_projecton one component. The before figures are one run each, on main at 82a2b94. The after figures are the median of 5 runs, after rebasing onto d6e2212 (#520).<c-if cond="flag"><div #c-ignore><c-if cond="flag"><div><c-if>…<c-elif>chainThe c-elif row misses the plan's 2.5 limit. The plan says a ratio between 2.5 and 4 must be profiled and reported before merging. Here is the profile:
citry checkruns once per component (_rust.vue._compile_vue, from_vue_compile_lint.py) goes from 0.109 s to 0.365 s, which is 3.3 times as long.line_break_count(_source_lines.py:30), which is called once per node and goes from 0.006 s to 0.022 s.<div>row only just passes, at 2.48, for the same reason: its Vue compile also grows about 3 times per doubling.Ordinary templates were measured on a release build, on a quiet machine with all other work paused (1-minute load 1.9 to 2.8). The benchmark ran main and this branch in alternating order over 10 rounds, across all 146 citry_ui templates.
check_projecton all of citry_ui, including librariesMain's own two halves differ by 1.0% and 1.3%, so the plan's limit is max(3%, 2 × noise) = 3% slower. The branch is faster.
Not measured. The plan also asked for a profile showing that the
getattrtime is gone. I didn't record one separately. The depth profiles above show where the time goes now, and the copying no longer appears in them.Tests
tests/shared_children.rschecks that a cloned template and a cloned node share their lists.test_compiling_leaves_the_parsed_template_unchangedcompiles one parsed template twice with each compiler entry point, and checks that itsreprand the output stay the same. The template covers the places where the compiler changes a tree.citry checkcoverage, now that a check at the limit is fast:test_nesting_limits.pyrunscitry checkon all four shapes, up from two._frame_budget.pychecks four of its 14 fixtures, up from two. The other 10 were written for rendering: their check findings change with depth, or they hold no name at the innermost level (two don't nest at all). So comparing their findings at two depths wouldn't show that the check reached the innermost level. The comment says so.What else had to change (cross-language check)
ast.rs:Sharedand the field types.parser.rsandcompiler.rs: mechanical edits.src/lang/*.rs(LangImpl): no change. The formatter: no change, because it only reads the tree.citry_core_py): only a comment intemplate_parser.rs._rust.pyiand the Python wrappers are unchanged.AGENTS.md(a gotcha about shared lists) anddocs/agent/INDEX.md.CHANGELOG.md(citry checkis faster) and one inpackages/py/citry_core/CHANGELOG.md(reading a field copies one node).Follow-ups
citry check, andline_break_count, both still grow faster than the depth.Local evidence
All on macOS arm64, CPython 3.14.9, debug build, rebased on d6e2212:
cargo fmt,cargo clippy -D warningsandcargo teston every first-party crate passed (1,325 tests). After the review fixes, I reran the parser, formatter and core-binding crates (710 tests passed).citry_coretests, the deep and nesting tests, and the LSP tests (1,782 passed). Ruff, format andscripts/validate.pyare clean.test_citry_ui_type_check.py::test_citry_ui_components_have_no_typescript_or_ty_findings. It reportstsc(7.0.2) DOM-type errors in five citry_ui components' embedded JS, such ashostonNode. Its findings come from type-checking component JS, not from the parsed template's fields. I couldn't run it on main's build to confirm, so CI is the check.test_benchmark_django*modules need the optionalbenchmarkgroup.An independent reviewer read the change against the plan and the merge with #520. It found no correctness problem in
Shared, copy-on-write orDrop. Its other findings are folded in: the stale comments, the missing tests,Dropallocating when it had nothing to free, the wider check coverage, and the wording.🤖 Generated with Claude Code