Repository navigation
perf: Share policy collection scans between subscribers - #108442
Conversation
Codecov Report❌ Looks like you've decreased code coverage for some files. Please write tests to increase, or at least maintain, the existing level of code coverage. See our documentation here for how to interpret this table.
|
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. |
|
@MelvinBot Review. |
|
Reviewed commit Both targeted unit suites passed: 34 tests. Browser behavior and performance gains were not independently verified. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 9287ef7bad
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
| return policiesWhereReportCanBeCreatedMemo(policies, currentUserLogin ?? '', () => | ||
| Object.values(policies).filter((policy): policy is Policy => canCreateReportOnPolicy(policy, currentUserLogin)), |
There was a problem hiding this comment.
Include environment readiness in the report-policy cache key
When restored policies are scanned before the asynchronous iOS environment check completes, this memo can retain an incorrect Teachers Unite eligibility result. canCreateReportOnPolicy() calls isTeachersUnitePolicyID(), whose environment changes asynchronously (src/libs/isTeachersUnitePolicyID.ts:7-14), so collection and login are not all of the scan's inputs. Once the environment resolves, the uncached predicate rejects the production Teachers Unite workspace, but this helper continues returning the old array to later callers and remounts until a policy write changes the collection. Consequently, the workspace picker at src/pages/DynamicNewReportWorkspaceSelectionPage.tsx:260 can keep offering an explicitly excluded workspace, particularly while offline. Include environment readiness/value in the cache key, or avoid caching this scan until it has resolved.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
@sosek108 I reproduced it with a unit test. Scanned a frozen collection before the environment resolved, then again after. The helper returned the old array including the "PROD Teachers Unite" workspace, while the uncached canCreateReportOnPolicy excluded it. Should we add the Teachers Unite policy ID, or the resolved environment, to the cache key?
There was a problem hiding this comment.
Good catch, thanks! Added the Teachers Unite policy ID to the cache key and a test that resolves the environment between two calls on the same snapshot.
Reviewer Checklist
Screenshots/VideosAndroid: HybridAppAndroid: mWeb ChromeiOS: HybridAppiOS: mWeb SafariMacOS: Chrome / SafariKapture.2026-12-01.at.23.25.30.mp4 |
|
Failing Unit test is not related to this PR. |
mountiny
left a comment
There was a problem hiding this comment.
@sosek108 @fabioh8010 I'm going to approve this, but it feels like a bit of an anti-pattern right now. I would love us to create a follow-up issue to see how we could solve this more holistically within Onyx, because it feels like we should not be creating these cached maps for individual selectors manually.
|
The checks are fixed on main, so this is just about merging main to the PR. It should be safe to merge it right now. |
|
🚧 mountiny has triggered a test Expensify/App build. You can view the workflow run here. |
|
✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release. |
|
@mountiny looks like this was merged without a test passing. These checks had not passed on 1e8d578 when it was merged:
Please add a note explaining why this was done and remove the |
|
🧪🧪 Use the links below to test this adhoc build on Android, iOS, and Web. Happy testing! 🧪🧪
|
|
This is not an emergency. The tests are confirmed to be fixed on main. |
We agree with you @mountiny. We are testing a global solution to this problem. |
Explanation of Change
On accounts with thousands of workspaces, every write to any one workspace made about 30 subscribers each go through the whole policy collection with the same few checks: active workspaces, admin workspaces, owned paid workspaces, and the Workspaces tab red dot. On an account with about 6000 workspaces, one unrelated policy write cost about 290 to 370 ms of JavaScript on Home.
The shared policy helpers now cache their result per Onyx collection snapshot. Onyx hands every subscriber the same frozen snapshot until the collection changes, so the first subscriber on a write does the scan and the rest reuse it. Collections that aren't frozen are never cached, and each answer is also keyed by the login, account ID or offline state it depends on. The cached arrays are now typed as readonly, so no caller can sort or change a shared result; the Wallet page sorted one in place and now uses
sortPoliciesByName, which copies first.The Workspaces tab red dot also remembers each policy's check results for as long as Onyx keeps that policy object, so a write only re-checks the policy it changed. The sync and HR connection checks now only look at admin workspaces that have connections, the only ones that can fail them.
Web, Home, about 6000 workspaces, one unrelated policy write: JavaScript work outside garbage collection went from 286 to 373 ms to 183 to 222 ms. The red dot went from 28 to 41 ms to 5 to 6 ms, and product training, the subscription, trial and marketing checks and the create report hook dropped by about 60 to 90 ms together.
Fixed Issues
$ #108486
PROPOSAL:
Tests
Offline tests
QA Steps
Same as tests
PR Author Checklist
### Fixed Issuessection aboveTestssectionOffline stepssectionQA stepssectionAvatar, I verified the components usingAvatarare working as expected)npm run compress-svg)Avataris modified, I verified thatAvataris working as expected in all cases)Designlabel and/or tagged@Expensify/designso the design team can review the changes.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