Repository navigation
Fix O(n²) membership check in setCollection - #898
Merged
mountiny merged 1 commit intoNov 26, 2026
Merged
Conversation
45 tasks done
mountiny
approved these changes
Nov 26, 2026
mountiny
marked this pull request as ready for review
November 26, 2026 16:26
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
expensify-chore-bot
Bot
requested review from
marcochavezf
and removed request for
a team
November 26, 2026 16:27
Contributor
3 tasks
37 tasks done
38 tasks done
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.
Details
setCollectiondeletes every persisted member of the collection that isn't in the new value. To decide which ones,setCollectionWithRetryloops over all persisted keys and checks each one againstresultCollectionKeys. That was an array, so each.includes(key)scanned the whole new collection, which makes the loop O(n²) in collection size.This PR builds a
Setfrom the same keys and uses.has(key), so the loop is O(n).Production impact. On a large production account with ~280k collection members (
rules_, a ~104 MB response), applying the update withOnyx.setCollectionfroze the tab for minutes. That's about 281k × 140k ≈ 4e10 string comparisons. Timings from App:setCollectionapply phase (rules_, 281,712 members)new Set(Object.keys(...)), 281,712 keysWhy behavior doesn't change
Array.prototype.includesandSet.prototype.hasboth use SameValueZero, and every key is a string.Setis built from the sameObject.keys(resultCollection)at the same point as the array it replaces: after the skippable-member reduce, synchronously, beforegetAllKeys()..includesread the reassigned array. The firstresultCollectionKeysis still used bydoAllCollectionItemsBelongToSameParentand the skippable reduce, and is nowconst.setCollectionWithRetryagain with the originalcollection, so theSetis rebuilt on every attempt.mergeCollectionalready usespersistedKeys.has(...)and needs no change.Setper call, a few MB at 281k keys.Possible future work. What's left of the apply phase for very large collections (cache set,
Storage.multiSet, subscriber notification for every key) is linear work, not part of this bug. It may still be worth optimizing later.Related Issues
GH_LINK
Linked E/App PR
Expensify/App#108098
Automated Tests
Added
should keep, update and remove persisted members when replacing a collectiontotests/unit/onyxTest.ts. It starts with 1,000 stored members, callssetCollectionwith a set that overlaps half of them, and checks that:I didn't add a timing-based test. At 50k members the old code takes ~1.5s and the new code ~0.46s, so a timeout tight enough to catch a regression would be flaky in CI.
Manual Tests
rules_collection (or another collection with many members).GetRulesruns and its response is applied withOnyx.setCollection.Author Checklist
### Related Issuessection aboveTestssectiontoggleReportand notonIconClick)myBool && <MyComponent />.STYLE.md) were followedAvatar, I verified the components usingAvatarare working as expected)/** comment above it */thisproperly so there are no scoping issues (i.e. foronClick={this.submit}the methodthis.submitshould be bound tothisin the constructor)thisare necessary to be bound (i.e. avoidthis.submit = this.submit.bind(this);ifthis.submitis never passed to a component event handler likeonClick)Avataris modified, I verified thatAvataris working as expected in all cases)mainbranch was merged into this PR after a review, I tested again and verified the outcome was still expected according to theTeststeps.Screenshots/Videos
Android: Native
Android: mWeb Chrome
iOS: Native
iOS: mWeb Safari
MacOS: Chrome / Safari
🤖 Generated with Claude Code