Repository navigation
perf: Reuse one policy list across selected reports in the change-workspace check #108471
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
BartekObudzinski
wants to merge
3
commits into
Expensify:main
Choose a base branch
from
callstack-internal:perf/change-workspace-cached-policy-list
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.
+141
−5
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
e31b9e2
perf: Reuse one policy list across selected reports in the change-wor…
BartekObudzinski a896966
chore: Shorten comments in getPolicyList and its tests
BartekObudzinski 5fb4537
test: Check the policy list cache against useOnyx and rename the help…
BartekObudzinski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| /** Lists the workspaces of a policy collection once per Onyx snapshot, so per-item loops don't copy the collection each time. */ | ||
| import type {Policy} from '@src/types/onyx'; | ||
|
|
||
| import type {OnyxCollection} from 'react-native-onyx'; | ||
|
|
||
| const policyListByCollection = new WeakMap<NonNullable<OnyxCollection<Policy>>, readonly Policy[]>(); | ||
|
|
||
| /** Every caller of the same snapshot gets the same array, so it is read-only. */ | ||
| function getPolicyList(policies: OnyxCollection<Policy>): readonly Policy[] { | ||
| if (!policies) { | ||
| return []; | ||
| } | ||
|
|
||
| const cachedPolicyList = policyListByCollection.get(policies); | ||
| if (cachedPolicyList) { | ||
| return cachedPolicyList; | ||
| } | ||
|
|
||
| const policyList = Object.values(policies).filter((policy): policy is Policy => !!policy); | ||
|
|
||
| // Onyx freezes each snapshot and replaces it on change, so only a frozen collection is safe to cache. | ||
| if (Object.isFrozen(policies)) { | ||
| policyListByCollection.set(policies, policyList); | ||
| } | ||
| return policyList; | ||
| } | ||
|
|
||
| // eslint-disable-next-line import/prefer-default-export -- PolicyUtils/index.ts re-exports its modules with `export *`, which skips default exports. | ||
| export {getPolicyList}; | ||
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| import {act, renderHook} from '@testing-library/react-native'; | ||
|
|
||
| import useOnyx from '@hooks/useOnyx'; | ||
|
|
||
| import {getPolicyList} from '@libs/PolicyUtils'; | ||
|
|
||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
| import type {Policy} from '@src/types/onyx'; | ||
|
|
||
| import type {OnyxCollection} from 'react-native-onyx'; | ||
|
|
||
| import Onyx from 'react-native-onyx'; | ||
|
|
||
| import createRandomPolicy from '../utils/collections/policies'; | ||
| import waitForBatchedUpdatesWithAct from '../utils/waitForBatchedUpdatesWithAct'; | ||
|
|
||
| describe('getPolicyList', () => { | ||
| it('returns the workspaces of a collection and skips empty entries', () => { | ||
| // Given an empty entry, which the OnyxCollection type allows even though Onyx drops removed members from snapshots | ||
| const firstPolicy = createRandomPolicy(1); | ||
| const secondPolicy = createRandomPolicy(2); | ||
| const policies: OnyxCollection<Policy> = { | ||
| [`${ONYXKEYS.COLLECTION.POLICY}1`]: firstPolicy, | ||
| [`${ONYXKEYS.COLLECTION.POLICY}2`]: secondPolicy, | ||
| [`${ONYXKEYS.COLLECTION.POLICY}3`]: undefined, | ||
| }; | ||
|
|
||
| // When the workspaces are listed | ||
| const policyList = getPolicyList(policies); | ||
|
|
||
| // Then only real workspaces come back, so callers skip their own empty checks | ||
| expect(policyList).toEqual([firstPolicy, secondPolicy]); | ||
| }); | ||
|
|
||
| it('returns an empty list when there is no collection', () => { | ||
| // Given no policy collection yet, as before Onyx loads it | ||
| // When the workspaces are listed | ||
| // Then callers can still loop over the result | ||
| expect(getPolicyList(undefined)).toEqual([]); | ||
| }); | ||
|
|
||
| it('reuses the list for the same frozen collection', () => { | ||
| // Given a frozen collection, which is how Onyx hands out each collection snapshot | ||
| const policies: OnyxCollection<Policy> = Object.freeze({[`${ONYXKEYS.COLLECTION.POLICY}1`]: createRandomPolicy(1), [`${ONYXKEYS.COLLECTION.POLICY}2`]: createRandomPolicy(2)}); | ||
|
|
||
| // When the workspaces are listed twice, like once per selected report | ||
| const firstList = getPolicyList(policies); | ||
| const secondList = getPolicyList(policies); | ||
|
|
||
| // Then the second call returns the same array instead of copying the collection again | ||
| expect(secondList).toBe(firstList); | ||
| }); | ||
|
|
||
| it('lists a new collection snapshot fresh', () => { | ||
| // Given a frozen snapshot that was already listed, and a newer snapshot after a workspace changed | ||
| const firstPolicy = createRandomPolicy(1); | ||
| const oldPolicies: OnyxCollection<Policy> = Object.freeze({[`${ONYXKEYS.COLLECTION.POLICY}1`]: firstPolicy}); | ||
| const oldList = getPolicyList(oldPolicies); | ||
| const changedPolicy = {...firstPolicy, name: 'Renamed workspace'}; | ||
| const newPolicies: OnyxCollection<Policy> = Object.freeze({[`${ONYXKEYS.COLLECTION.POLICY}1`]: changedPolicy}); | ||
|
|
||
| // When the newer snapshot is listed | ||
| const newList = getPolicyList(newPolicies); | ||
|
|
||
| // Then it reflects the change, because the cache is tied to the collection object | ||
| expect(newList).not.toBe(oldList); | ||
| expect(newList).toEqual([changedPolicy]); | ||
| }); | ||
|
|
||
| it('does not cache a collection that is not frozen', () => { | ||
| // Given a plain object that its owner can still change after the first call | ||
| const policies: Record<string, Policy | undefined> = {[`${ONYXKEYS.COLLECTION.POLICY}1`]: createRandomPolicy(1)}; | ||
| const firstList = getPolicyList(policies); | ||
|
|
||
| // When a workspace is added to the same object and it is listed again | ||
| const addedPolicy = createRandomPolicy(2); | ||
| policies[`${ONYXKEYS.COLLECTION.POLICY}2`] = addedPolicy; | ||
| const secondList = getPolicyList(policies); | ||
|
|
||
| // Then the new workspace shows up, because only frozen collections are safe to cache | ||
| expect(secondList).toContain(addedPolicy); | ||
| expect(secondList).not.toBe(firstList); | ||
| }); | ||
|
|
||
| it('reuses the list for the collection useOnyx returns', async () => { | ||
| // Given workspaces stored in Onyx and read with useOnyx, the way the Spend bulk actions read them | ||
| Onyx.init({keys: ONYXKEYS}); | ||
| await act(async () => { | ||
| await Onyx.clear(); | ||
| await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}1`, createRandomPolicy(1)); | ||
| await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}2`, createRandomPolicy(2)); | ||
| }); | ||
| const {result} = renderHook(() => useOnyx(ONYXKEYS.COLLECTION.POLICY)); | ||
| await waitForBatchedUpdatesWithAct(); | ||
| const policies = result.current[0]; | ||
|
|
||
| // When the workspaces are listed twice, like once per selected report | ||
| const firstList = getPolicyList(policies); | ||
| const secondList = getPolicyList(policies); | ||
|
|
||
| // Then the same array comes back, which only holds while Onyx keeps freezing collection snapshots | ||
| expect(Object.keys(policies ?? {})).toHaveLength(2); | ||
| expect(Object.isFrozen(policies)).toBe(true); | ||
| expect(secondList).toBe(firstList); | ||
| }); | ||
| }); |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CONSISTENCY-9in.claude/skills/app-coding-standards/rules/consistency-9-file-naming.mdrequires newly added source files to be named after their primary export. This module exports onlygetPolicyList, sopolicyList.tsviolates that convention and does not qualify for the multi-export utilities exception. Rename it togetPolicyList.tsand update the barrel re-export so the helper is discoverable under its exported name.AGENTS.md reference: AGENTS.md:L7-L9
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Renamed to
getPolicyList.ts.