Skip to content

Cache Fragment._component_parent() lookups - #187

Merged
berendkleinhaneveld merged 5 commits into
masterfrom
perf/component-parent-cache
Jul 7, 2026
Merged

Cache Fragment._component_parent() lookups#187
berendkleinhaneveld merged 5 commits into
masterfrom
perf/component-parent-cache

Conversation

@berendkleinhaneveld

Copy link
Copy Markdown
Collaborator

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_attr use it to call component.updated()) and on every ref (un)registration, so update cost scaled with fragment depth.

  • The result is now cached per fragment (including a cached None), with a sentinel to distinguish 'not computed'.
  • Invalidation happens in the parent property setter, on unmount(destroy=True), and — via a recursive helper over children/slot contents/active fragments — after DynamicFragment._create_fragment_for_tag(), the one place a live, mounted subtree is reparented by writing _parent directly (a <component :is> tag switch changes the owning component of every transferred descendant).
  • Lifetime note: the cache holds a strong ref from a descendant fragment to an ancestor 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 :is switches.
  • test_dynamic_component_tag_switch_moves_string_ref (new vs the superseded Add benchmark suite and fix core rendering hot-path inefficiencies #183): a string ref on 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)

benchmark before (mean) after (mean)
update_attribute_at_depth[10] 5.02 µs 4.23 µs
update_attribute_at_depth[50] 7.14 µs 4.22 µs
update_attribute_at_depth[200] 15.41 µs 4.27 µs

Attribute-update cost no longer scales with fragment depth. No regressions in other groups.

Test plan

  • uv run pytest tests -q — 352 passed, 1 skipped
  • uv run ruff check . / uv run ruff format --check . — clean
  • Regression tests verified to fail without the invalidation hook
  • uv run pytest bench --benchmark-only --benchmark-compare — table above

🤖 Generated with Claude Code

berendkleinhaneveld and others added 4 commits July 6, 2026 23:20
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
berendkleinhaneveld force-pushed the perf/component-parent-cache branch from 8349e12 to 8a15408 Compare July 6, 2026 22:29
Base automatically changed from perf/mount-path to master July 7, 2026 18:16
@berendkleinhaneveld
berendkleinhaneveld merged commit 2b6a634 into master Jul 7, 2026
9 of 11 checks passed
@berendkleinhaneveld
berendkleinhaneveld deleted the perf/component-parent-cache branch July 7, 2026 19:19
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>
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