Perf: shared mutation router for subtree watchers (#150 Tier 2)#155
Merged
Conversation
Collapse one MutationObserver per `createSubtreeWatcher` into one shared observer per observed root, with per-subscriber fan-out. The 24 rules that all watch `document.body` previously installed 24 observers, 24 visibilitychange listeners, and 24 route-change subscriptions, each running the same nodeType/IGNORE_TAGS/isConnected filter; now they share one of each, with the cheap shared filter running once per added node regardless of subscriber count. Public API (`createSubtreeWatcher` / `SubtreeWatcher`) is unchanged. Per-subscriber `throttleMs`, `skipPlaceholderSubtrees`, and burst-flush behavior stay independent. Last subscriber to stop() tears the router down (disconnect, remove listeners, cancel rAF, delete from registry). Adds `__resetSubtreeWatcherForTesting` mirroring the existing `__resetRouteChangeForTesting` so test isolation does not depend on every `stop()` running. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
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
createSubtreeWatcherMutationObserverwith one shared observer per observed root that fans out to subscribers (uBO'ssafeObserverHandlermodel — issue Perf: speed up rule application during fast infinite scroll and SPA route transitions #150 Tier 2 idea build(deps): Bump actions/deploy-pages from 4 to 5 #10).document.body, this collapses 24 observer callbacks, 24visibilitychangelisteners, and 24subscribeRouteChangecallbacks into one each; the sharednodeType/IGNORE_TAGS/isConnectedfilter runs once per added node regardless of subscriber count.throttleMs,skipPlaceholderSubtrees, burst-flush,pending) stays independent —createSubtreeWatcher/SubtreeWatcherAPI is unchanged, so no call site updates are needed.Mechanics
Map<Node, Router>keyed by observed root. First subscriber on a target creates the router + observer + listeners; last subscriber tostop()tears it all down and deletes the map entry.Documentanddocument.bodycollapse to the same router viaresolveTarget.document.head(used bymeta-injection-strip) gets its own router.onSubtrees([document.body])— preserves existing sweep semantics.__resetSubtreeWatcherForTestingmirroring__resetRouteChangeForTestingso test isolation doesn't depend on everystop()running.Test plan
bun run test src/lib/__tests__/subtree-watcher.test.ts— 45/45 pass (35 existing + 10 new covering fan-out, single-observer-construction via constructor spy, independentskipPlaceholderSubtrees/throttleMs, partial-stop keeping observer alive, full teardown on last stop,Document↔bodyrouting, body vs. head router isolation, route-change and visibility fan-out).bun run test— 1200/1200 pass across all 63 suites (covers all 24 rule consumers transitively).bun run typecheckclean.bun run check(Biome + ESLint) clean.🤖 Generated with Claude Code