-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
perf(queriesObserver): fix O(n²) performance issue in batch updates #9467
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
joseph0926
wants to merge
1
commit into
TanStack:main
Choose a base branch
from
joseph0926:perf/queries-observer-batch-updates
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
perf(queriesObserver): fix O(n²) performance issue in batch updates #9467
joseph0926
wants to merge
1
commit into
TanStack:main
from
joseph0926:perf/queries-observer-batch-updates
Conversation
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
…1) observer lookup
View your CI Pipeline Execution ↗ for commit 5f51b4a
☁️ Nx Cloud last updated this comment at |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #9467 +/- ##
===========================================
+ Coverage 45.30% 59.60% +14.29%
===========================================
Files 208 137 -71
Lines 8283 5525 -2758
Branches 1869 1477 -392
===========================================
- Hits 3753 3293 -460
+ Misses 4085 1930 -2155
+ Partials 445 302 -143 🚀 New features to boost your workflow:
|
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.
This is an additional workaround attempt for an issue I've been challenged to solve before.
Previous issue
First Resolution Attempt PR
Background
This PR addresses a long-standing performance issue first reported in #8295, where using
useQueries
with batch data fetching patterns (like DataLoader) causes severe performance degradation due to O(n²) complexity.The Problem
When multiple queries resolve simultaneously (common with DataLoader pattern), the performance degrades quadratically:
The issue occurs because:
extra notes
i found two remaining pain‑points:
If 100 observers change only 1 member, we still perform
100 × 100 = 10 000
comparisons.Missing early‑return / direct access
Concrete mental model
Real-world impact
Previous Improvements
Over the past months, several optimizations have been made:
1. Caching
findMatchingObservers
results (#8304)I previously added
observerMatches
to cache results instead of recalculating:2. Optimizing
difference
function (O(n²) → O(n))3. Optimizing
findMatchingObservers
with MapThe Remaining Issue
Despite these improvements, one critical O(n) operation remains in
#onUpdate
:With batch updates, this creates:
This PR's Solution
This PR introduces a
WeakMap
to track observer indices, eliminating the O(n) search:Why WeakMap?
I chose
WeakMap
over regularMap
to address the concern raised in #8686 about storing observers in both a Map and an Array:WeakMap
solves this elegantly:This approach maintains the simplicity of the original design while achieving O(1) performance.
Performance Results
With this change, batch updates now scale linearly:
Summary
This completes the optimization journey for
QueriesObserver
:findMatchingObservers
caching (my previous PR)difference
function: O(n²) → O(n)findMatchingObservers
: O(n) → O(1) with Map#onUpdate
: O(n) → O(1) with WeakMap (this PR)The combination of these improvements transforms what was once a quadratic bottleneck into efficient linear scaling, making
useQueries
viable for large-scale batch operations.