Skip to content

perf: batch cold-cache pre-warm in mergeCollectionWithPatches via multiGet#793

Open
elirangoshen wants to merge 4 commits into
Expensify:mainfrom
callstack-internal:elirangoshen/perf/mergeCollection-multiGet-prewarm-v2
Open

perf: batch cold-cache pre-warm in mergeCollectionWithPatches via multiGet#793
elirangoshen wants to merge 4 commits into
Expensify:mainfrom
callstack-internal:elirangoshen/perf/mergeCollection-multiGet-prewarm-v2

Conversation

@elirangoshen
Copy link
Copy Markdown
Contributor

@elirangoshen elirangoshen commented May 26, 2026

Details

Hybrid pre-warm strategy for mergeCollectionWithPatches that replaces the unconditional Promise.all(existingKeys.map((key) => get(key))):

  • Fast path (every existingKey is already warm in cache): use a sync-resolved Promise.resolve(). No extra microtask hops, preserving the original promise-chain depth and the subscriber-callback timing that dependent tests rely on (Onyx.update batch tests broadcast a single merged callback rather than undefined followed by the merged value).

  • Slow path (at least one existingKey is a cache miss): use multiGet, which batches the missing keys into a single Storage.multiGet round-trip instead of N parallel get() invocations and writes the storage values back to cache before resolving.

Net result: same correctness as before, fewer storage operations on cold-cache merges, identical broadcast timing for warm-cache merges.

Addresses follow-up from #787 review.

Related Issues

Expensify/App#90634

Linked E/App PR

Expensify/App#91585

Automated Tests

Added a new describe('mergeCollection pre-warm', ...) block in tests/unit/onyxUtilsTest.ts with 5 tests:

  1. fast path: skips storage reads entirely when every existing key is warm in cache — Seeds two members via Onyx.set, spies on StorageMock.multiGet and StorageMock.getItem, then runs Onyx.mergeCollection. Asserts both spies are called 0 times. Confirms the diff's Promise.resolve() shortcut.

  2. slow path: batches cold existing keys into a single Storage.multiGet, with no individual getItem calls — Seeds three members, evicts two from cache (cold), leaves one warm. Asserts Storage.multiGet is called exactly once with only the two cold keys (the warm key is filtered out by OnyxUtils.multiGet), and Storage.getItem is never called during pre-warm.

  3. slow path: cold-cache merge layers the new delta on top of existing storage data (no field drops) — Seeds a key with {a:1, b:2} then evicts from cache. Merges {c:3} and asserts the cache holds {a:1, b:2, c:3}. Without the pre-warm reading from storage, cache.merge would start from undefined and drop {a:1, b:2} — this guards the correctness invariant the in-code comment specifically calls out.

  4. warm cache: subscriber receives a single merged broadcast for an Onyx.update batch (no transient undefined) — Subscribes to a warm collection key, fires an Onyx.update with a MERGE_COLLECTION op. Asserts subscriber was called exactly once with the final merged value (NOT undefined then merged on a later microtask). Guards the promise-chain-depth invariant.

  5. equivalence: warm-path and cold-path produce the same final cache state for the same merge — Runs the same effective merge against a warm cache and against a cold cache; asserts the post-merge collection state is identical in both runs.

Suite-pollution fix: the retryOperation describe block earlier in the file mutates StorageMock.setItem (and other methods) without restoring them. This block captures pristine references at file-load time and restores them in beforeEach so seeding via Onyx.set actually persists.

Helper note: OnyxCache.drop removes the key from storageKeys, which makes getAllKeys() miss it when other keys remain in cache. The evictFromCache helper calls OnyxCache.addKey(key) after drop so the key stays "tracked but unloaded" — exactly the cold-but-persisted state the slow path is meant to handle.

Local results:

  • npx jest450/450 pass across 16 suites.
  • npx tsc --noEmit — clean.

Manual Tests

End-to-end verification against Expensify/App via the companion PR Expensify/App#91585, which pins react-native-onyx's package.json to this branch's head SHA.

Setup

  1. In the App repo, check out the companion branch that pins react-native-onyx to this PR's head SHA.
  2. npm install under Node 20.20.0, then npm run web.
  3. Open https://dev.new.expensify.com:8082/ in Chrome with DevTools open.
  4. Sign in.

Functional smoke (same flows as #787, expect no regression)

For each: open the screen, perform the action, verify UI updates immediately, persist after reload.

  1. Initial hydration after login — LHN populates correctly.
  2. Send a chat message — appears immediately, confirms via Pusher, persists after reload.
  3. Mark-all-as-read — badges clear and stay cleared.
  4. Search filter — results populate and update live.
  5. Hold / unhold an expense — badge toggles and persists.
  6. Submit expense via FAB — appears in report immediately, persists.
  7. Switch workspaces — LHN filters to new workspace.

Cold-cache merge correctness

  1. Sign in and perform an action that writes collection data.
  2. Reload (clears cache, keeps storage).
  3. Trigger a MERGE_COLLECTION against one of those keys before the LHN hydrates it.
  4. Expected: merged value retains all fields from storage; the new delta is layered on top — no silent drops. (Slow-path correctness.)

Storage-failure regression (carry-over from #787)

  1. With App running and authenticated, Application → IndexedDB → right-click → delete OnyxDB. Do not reload.
  2. Immediately trigger a MERGE_COLLECTION action.
  3. Expected: UI updates correctly; new state is visible to subscribers even though the IDB write fails. Console shows storage errors, but no white screen / no stale UI / no data loss within the session.

Author Checklist

  • I linked the correct issue in the ### Related Issues section above
  • I linked the corresponding Expensify/App PR in the ### Linked E/App PR section above, and verified this change against it (E/App CI passed and manual testing completed)
  • I wrote clear testing steps that cover the changes made in this PR
    • I added steps for local testing in the Tests section
    • I tested this PR with a High Traffic account against the staging or production API to ensure there are no regressions (e.g. long loading states that impact usability).
  • I included screenshots or videos for tests on all platforms
  • I ran the tests on all platforms & verified they passed on:
    • Android / native
    • Android / Chrome
    • iOS / native
    • iOS / Safari
    • MacOS / Chrome / Safari
  • I verified there are no console errors (if there's a console error not related to the PR, report it or open an issue for it to be fixed)
  • I followed proper code patterns (see Reviewing the code)
    • I verified that any callback methods that were added or modified are named for what the method does and never what callback they handle (i.e. toggleReport and not onIconClick)
    • I verified that the left part of a conditional rendering a React component is a boolean and NOT a string, e.g. myBool && <MyComponent />.
    • I verified that comments were added to code that is not self explanatory
    • I verified that any new or modified comments were clear, correct English, and explained "why" the code was doing something instead of only explaining "what" the code was doing.
    • I verified proper file naming conventions were followed for any new files or renamed files. All non-platform specific files are named after what they export and are not named "index.js". All platform-specific files are named for the platform the code supports as outlined in the README.
    • I verified the JSDocs style guidelines (in STYLE.md) were followed
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I followed the guidelines as stated in the Review Guidelines
  • I tested other components that can be impacted by my changes (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar are working as expected)
  • I verified all code is DRY (the PR doesn't include any logic written more than once, with the exception of tests)
  • I verified any variables that can be defined as constants (ie. in CONST.js or at the top of the file that uses the constant) are defined as such
  • I verified that if a function's arguments changed that all usages have also been updated correctly
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately and each prop has a /** comment above it */
    • The file is named correctly
    • The component has a clear name that is non-ambiguous and the purpose of the component can be inferred from the name alone
    • The only data being stored in the state is data necessary for rendering and nothing else
    • If we are not using the full Onyx data that we loaded, I've added the proper selector in order to ensure the component only re-renders when the data it is using changes
    • For Class Components, any internal methods passed to components event handlers are bound to this properly so there are no scoping issues (i.e. for onClick={this.submit} the method this.submit should be bound to this in the constructor)
    • Any internal methods bound to this are necessary to be bound (i.e. avoid this.submit = this.submit.bind(this); if this.submit is never passed to a component event handler like onClick)
    • All JSX used for rendering exists in the render method
    • The component has the minimum amount of code necessary for its purpose, and it is broken down into smaller components in order to separate concerns and functions
  • If any new file was added I verified that:
    • The file has a description of what it does and/or why is needed at the top of the file if the code is not self explanatory
  • If the PR modifies a generic component, I tested and verified that those changes do not break usages of that component in the rest of the App (i.e. if a shared library or component like Avatar is modified, I verified that Avatar is working as expected in all cases)
  • If the main branch was merged into this PR after a review, I tested again and verified the outcome was still expected according to the Test steps.
  • I have checked off every checkbox in the PR author checklist, including those that don't apply to this PR.

Screenshots/Videos

Android: Native
Android: mWeb Chrome
iOS: Native
iOS: mWeb Safari
MacOS: Chrome / Safari
Screen.Recording.2026-05-25.at.12.15.01.mov
Screen.Recording.2026-05-25.at.12.15.33.mov
Screen.Recording.2026-05-25.at.12.21.34.mov
Screen.Recording.2026-05-25.at.12.25.17.mov
Screenshot 2026-05-25 at 12 25 01

…tiGet

Replaces the unconditional Promise.all(existingKeys.map(get)) pre-warm
with a hybrid:

- Fast path (every existingKey is already in cache): use a sync-
  resolved Promise — no extra microtask hops, preserving the original
  promise-chain depth and subscriber-callback timing that dependent
  tests rely on (Onyx.update batch tests broadcast a single merged
  callback rather than an `undefined` initial followed by the merged
  result).

- Slow path (at least one cache-miss existingKey): use multiGet —
  one Storage.multiGet round-trip for the missing keys instead of N
  parallel get() invocations.

Net result: same correctness as before, fewer storage operations on
cold-cache merges, identical broadcast timing for warm-cache merges.

Addresses follow-up from Expensify#787 review.
elirangoshen and others added 2 commits May 26, 2026 10:26
Adds a new describe('mergeCollection pre-warm', ...) block with 5 tests:

1. Fast path: skips storage reads entirely when every existing key is
   warm in cache (spies on StorageMock.multiGet and StorageMock.getItem,
   asserts both are called 0 times).
2. Slow path: batches cold existing keys into a single Storage.multiGet,
   with no individual getItem calls.
3. Slow path: cold-cache merge layers the new delta on top of existing
   storage data (no field drops) — guards the correctness invariant the
   in-code comment specifically calls out.
4. Warm cache: subscriber receives a single merged broadcast for an
   Onyx.update batch (no transient undefined) — guards promise-chain
   depth.
5. Equivalence: warm-path and cold-path produce the same final cache
   state for the same merge.

Includes a suite-pollution fix (capture pristine StorageMock refs and
restore them in beforeEach so seeding via Onyx.set isn't intercepted by
mocks leaking from the earlier retryOperation describe block), and an
evictFromCache helper that uses for…of to satisfy
unicorn/no-array-for-each.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Inside OnyxUtils.multiGet, the cached-value path was guarded by a truthy
check (`if (cacheValue)`), which treats cached falsy values (0, '',
false, null) as cache misses. Those keys fell through to
missingKeys → Storage.multiGet → cache.merge(temp), which would
overwrite the warm cached value with whatever stale value sat in
storage.

This mattered most after the new perf hybrid pre-warm in
mergeCollectionWithPatches started routing cold-cache merges through
multiGet — the outer check (`!cache.hasCacheForKey(key)`) and the inner
check disagreed for falsy values. Concrete case: `Onyx.set('coll_1', 0)`
updates cache to `0` but the storage write is still in flight; a
mergeCollection over the same collection key would re-fetch `coll_1`
from storage and clobber the cached `0`.

Aligns the inner check with hasCacheForKey so both sides agree, and
adds a regression test that seeds cache with `0` and asserts multiGet
returns it without touching Storage.multiGet / Storage.getItem.

Addresses fabioh8010's review on PR #5
(#5 (review)).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@elirangoshen elirangoshen force-pushed the elirangoshen/perf/mergeCollection-multiGet-prewarm-v2 branch from 73416e8 to d66152a Compare May 26, 2026 08:28
@elirangoshen
Copy link
Copy Markdown
Contributor Author

Addressing the review on the predecessor PR (callstack-internal/react-native-onyx#5) — both points handled in this branch:

1. multiGet falsy-value cache hit bug — fixed. Aligned the inner check (if (cacheValue)) with the outer (!cache.hasCacheForKey(key)) so cached falsy values (0, '', false, null) are no longer re-fetched from storage and clobbered via cache.merge(temp). New commit: d66152a3 fix: align multiGet cache hit check with cache.hasCacheForKey.

Added a regression test under describe('multiGet cache hit consistency', ...) that seeds cache with 0 and asserts Storage.multiGet / Storage.getItem are never called. Confirmed it fails on the pre-fix code (multiGetSpy is called once with ["test"]).

2. Unrelated reformat hunks in tests/unit/onyxUtilsTest.ts — dropped. All 8 prettier-drift hunks above the new mergeCollection pre-warm describe block were noise from a different prettier version. Reset the file to origin/main and re-applied only the new describe block. Final diff vs main is now a single +171-line hunk for the new tests (the existing tests in the file are byte-identical to main).

Resulting 3-commit history vs origin/main:

d66152a3 fix: align multiGet cache hit check with cache.hasCacheForKey
d02382fd test: cover mergeCollectionWithPatches multiGet pre-warm fast/slow paths
32956378 perf: batch cold-cache pre-warm in mergeCollectionWithPatches via multiGet

Total: lib +28/−6, tests +209/−0. 457/457 unit tests green, typecheck clean, prettier clean.

@elirangoshen elirangoshen marked this pull request as ready for review May 26, 2026 08:41
@elirangoshen elirangoshen requested a review from a team as a code owner May 26, 2026 08:41
@melvin-bot melvin-bot Bot requested review from lakchote and removed request for a team May 26, 2026 08:41
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d66152a3be

ℹ️ 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".

Comment thread lib/OnyxUtils.ts Outdated
The new hybrid pre-warm in mergeCollectionWithPatches replaced
`Promise.all(existingKeys.map(get))` with `multiGet(existingKeys)` on
the slow path. The two have different failure semantics:

- get() catches storage read errors per-key and resolves with undefined
  (see Logger.logInfo catch at the bottom of get()).
- multiGet() has no .catch, so Storage.multiGet rejections propagate
  all the way up.

In the cold-key path this meant a transient IndexedDB read error
rejected before cache.merge() and keysChanged() ran — subscribers
missed the in-memory merge and the outer Onyx.mergeCollection / Onyx.update
promise rejected, regressing the cache-first invariant established
in PR Expensify#787 (Expensify#787).

Adds a .catch at the call site that swallows the rejection (logging
via Logger.logInfo for visibility) so cache.merge + keysChanged still
fire when pre-warm reads fail. Doesn't modify multiGet itself —
other callers may legitimately depend on rejection visibility.

Adds a regression test that mocks Storage.multiGet to reject on the
pre-warm read and asserts:
1. Onyx.mergeCollection resolves (doesn't reject up to caller).
2. The waitForCollectionCallback subscriber sees the merged delta.

Confirmed: test fails on pre-fix code with "Received: [Error:
Transient IndexedDB read error]" on `expect(outerRejected).toBeNull()`.

Addresses chatgpt-codex-connector review on PR Expensify#793
(Expensify#793 (comment)).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant