Cache Fragment._component_parent() lookups - #187
Merged
Conversation
Adds a bench/ suite (element creation, component mount depth, attribute update at depth, keyed/unkeyed v-for reconciliation, and unkeyed list grow/shrink) using pytest-benchmark with DictRenderer and synchronous event loop, mirroring the setup in the sibling observ repo. The new benchmark workflow runs the suite twice on every PR - once against the master version of collagraph/ and once against the PR version - and fails when mean time regresses more than 5%. The CI test job is scoped to the tests directory so the matrix does not execute benchmarks. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
The allocation burst of list reconciliation triggers collection pauses in some rounds, which inflated stddev on the grow benchmarks to ~40% of the mean - far too noisy for the 5% regression gate in CI. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
- weak(): read __code__.co_argcount instead of building a full inspect.Signature for every wrapped watcher callback; this runs for every dynamic bind during create/mount. - ComponentFragment.mount(): use the existing first() helper to resolve the component root element instead of a hand-rolled BFS with a per-mount deque import. Adds a regression test for a component whose root element sits behind a v-if wrapper. - Component.emit(): avoid creating defaultdict entries for events that have no handlers and skip the set copy when empty. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
The parent-chain walk ran on every reactive attribute update (via _set_attr/_rem_attr calling component.updated()) and on every ref (un)registration, making update cost scale with fragment depth. The result is now cached per fragment. The cache is invalidated in the parent property setter, on unmount(destroy=True), and - via a new recursive helper - after DynamicFragment._create_fragment_for_tag() transfers live children to a new active fragment on a <component :is> tag switch, which reassigns _parent directly and changes the owning component of every transferred descendant. Two regression tests cover the tag-switch reparenting: updated() attribution and string-ref registration must follow the new owner. Both fail when the invalidation hook is removed. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
berendkleinhaneveld
force-pushed
the
perf/mount-path
branch
from
July 6, 2026 22:29
d444f66 to
36125ef
Compare
berendkleinhaneveld
force-pushed
the
perf/component-parent-cache
branch
from
July 6, 2026 22:29
8349e12 to
8a15408
Compare
3 tasks
Merged
berendkleinhaneveld
added a commit
that referenced
this pull request
Jul 21, 2026
Features: - Add pure-Python view API as alternative to cgx templates (#193) - Support text elements for PySide widgets that display text (#191) Fixes & internals: - Fragment parenting overhaul (#162) - Fix PyInstaller hook for CGX files inside packages (#184) - Write compiled AST to temp file when CGX_DEBUG is set (#175) Performance: - Speed up mount path: cheap arity check, reuse first(), leaner emit (#186) - Cache Fragment._component_parent() lookups (#187) - Avoid redundant anchor lookups in Fragment.anchor() and unkeyed v-for (#188) Documentation: - Add MkDocs documentation with GitHub Pages deployment (#176) - Add internals architecture documentation page (#194) - Add docs badge and links to README (#192) Tooling & CI: - Add benchmark suite and per-PR benchmark CI workflow (#185) - Make benchmark CI guard robust against run-to-run noise (#196) - Update GitHub actions from Node 20 to Node 24 (#190) - Migrate from pre-commit to prek (#195) Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
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.
Summary
Third PR of the performance series (stacked on #186 → #185; will retarget as those merge).
_component_parent()walked the parent chain on every reactive attribute update (_set_attr/_rem_attruse it to callcomponent.updated()) and on every ref (un)registration, so update cost scaled with fragment depth.None), with a sentinel to distinguish 'not computed'.parentproperty setter, onunmount(destroy=True), and — via a recursive helper over children/slot contents/active fragments — afterDynamicFragment._create_fragment_for_tag(), the one place a live, mounted subtree is reparented by writing_parentdirectly (a<component :is>tag switch changes the owning component of every transferred descendant).Component, whose lifetime already encloses the descendant's;unmount(destroy=True)clears it, and no reference cycle results.Regression tests
Two tests cover the tag-switch reparenting edge, and both fail if the invalidation hook is removed (verified):
test_dynamic_component_tag_switch_updates_correct_owner:updated()attribution flips App → Wrapper → App across:isswitches.test_dynamic_component_tag_switch_moves_string_ref(new vs the superseded Add benchmark suite and fix core rendering hot-path inefficiencies #183): a stringrefon transferred slot content registers with the new owner after each switch. Assertions were validated against the uncached code first to guarantee semantics are unchanged.Benchmarks (local, macOS, vs base branch)
Attribute-update cost no longer scales with fragment depth. No regressions in other groups.
Test plan
uv run pytest tests -q— 352 passed, 1 skippeduv run ruff check ./uv run ruff format --check .— cleanuv run pytest bench --benchmark-only --benchmark-compare— table above🤖 Generated with Claude Code