Skip to content

Cache computed properties with lazy dirty-checking - #819

Open
wouterlucas wants to merge 1 commit into
async-reactivity-schedulerfrom
cached-computed
Open

wouterlucas wants to merge 1 commit into
async-reactivity-schedulerfrom
cached-computed

Conversation

@wouterlucas

Copy link
Copy Markdown
Contributor

Stacked on #818 (needs its scheduler). Evaluates each computed at most once per dependency change instead of on every read, porting Vue2's lazy computed-watcher semantics.

What changed:

  • src/component/setup/computed.js: per-instance WeakMap caches (setup runs per type, getters run per instance). First read evaluates; later reads return the cache and subscribe the reader via track(entry, 'value').
  • Evaluation runs under a new withTracking helper so deps subscribe a per-entry invalidator that marks dirty and notifies downstream on change (chained computeds propagate through the same mechanism).
  • Invalidators register in the instance's symbols.effects so destroy() unsubscribes them; plain-object test doubles skip registration.
  • src/lib/reactivity/effect.js: extracted withTracking(effectFn, key, fn); effect() is a thin wrapper, no behavior change.
  • Dynamic updates test now uses reactive backing (plain writes cannot invalidate any cache — same as Vue); new tests pin single evaluation, chain invalidation, multi-dep batching (1 recompute + 1 downstream run), and per-instance isolation.

Verification: eslint clean, full suite 1546/1546 pass.

Evaluate each computed at most once per dependency change instead of
on every read, porting Vue2's lazy computed watcher semantics.

- Per-instance WeakMap caches (setup runs per type, getters run per
  instance); first read evaluates, later reads return the cache
- Evaluation runs under withTracking so deps subscribe an invalidator
  that marks dirty and notifies downstream readers on change
- Invalidators register in instance effects so destroy unsubscribes them
- effect(): extracted withTracking helper, no behavior change
@github-actions

Copy link
Copy Markdown

Benchmark comparison

Timing Change
Synchronous generation n/a
Total time to idle +1.33%

Negative means faster. Informational results; small changes can be measurement noise.

Full report · Workflow run

Head: 6e255578aab1 · Base: c9f3603cbf4b

This branch has not been deployed

No deployments
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