Skip to content

perf: batch cold-cache pre-warm in mergeCollectionWithPatches via mul…#5

Closed
elirangoshen wants to merge 3 commits into
elirangoshen/fix/90634-mergeCollectionWithPatches-cache-firstfrom
elirangoshen/perf/mergeCollection-multiGet-prewarm
Closed

perf: batch cold-cache pre-warm in mergeCollectionWithPatches via mul…#5
elirangoshen wants to merge 3 commits into
elirangoshen/fix/90634-mergeCollectionWithPatches-cache-firstfrom
elirangoshen/perf/mergeCollection-multiGet-prewarm

Conversation

@elirangoshen
Copy link
Copy Markdown

@elirangoshen elirangoshen commented May 21, 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 Expensify#787 review.

Related Issues

Expensify/App#90634

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 Expensify#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 Expensify#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 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.
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 21, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 0bb8be7d-c286-4dd5-a237-28aafe15a489

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch elirangoshen/perf/mergeCollection-multiGet-prewarm

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Comment thread lib/OnyxUtils.ts
// writes the storage values back to cache before resolving.
const hasColdExistingKey = existingKeys.some((key) => !cache.hasCacheForKey(key));
const prewarmPromise = hasColdExistingKey ? multiGet(existingKeys) : Promise.resolve();
return prewarmPromise.then(() => {
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@fabioh8010 in this one , your suggested code calls multiGet unconditionally while in this one bypasses it when there's nothing to fetch, all test passes.

We can also fix the failing tests and call multiGet unconditionally, just that I go warning from claude that it will introduces a real user-visible behavior change: subscribers can now see an undefined initial callback before the merged data, where they previously saw only the merged value.

elirangoshen and others added 2 commits May 25, 2026 10:39
Adds 5 tests to the mergeCollection pre-warm describe block:
- Fast path: warm cache skips Storage.multiGet/getItem entirely.
- Slow path: cold existing keys batch into one Storage.multiGet with no
  individual getItem calls.
- Slow path correctness: cold-cache merge layers delta on top of persisted
  storage value (no field drops).
- Onyx.update timing: warm cache broadcasts a single merged value
  (no transient undefined) — guards the promise-chain depth invariant.
- Equivalence: warm and cold paths converge on identical cache state.

Helper notes:
- OnyxCache.drop removes the key from storageKeys; re-register via addKey
  so getAllKeys still sees the key as persisted when other keys remain in
  cache (the slow-path scenario).
- Capture pristine StorageMock methods at file load and restore in
  beforeEach because the retryOperation describe block mutates setItem
  and never restores it.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
…-for-each

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Copy link
Copy Markdown
Collaborator

@fabioh8010 fabioh8010 left a comment

Choose a reason for hiding this comment

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

I also got this comment from Claude:

Spotted a subtle inconsistency: the outer check uses cache.hasCacheForKey, but multiGet internally uses if (cacheValue) (truthy check). For a cached falsy value like 0, '', or false, the outer says "cached" (correct), but multiGet treats it as missing — re-fetches from storage and cache.merge(temp) ends up overwriting the cached value.

Concrete case: Onyx.set('coll_1', 0) updates cache to 0 but the storage write is still in flight. Then a mergeCollection('coll_', ...) with a cold key hits the slow path → multiGet re-fetches coll_1 and overwrites the cached 0 with whatever stale value was in storage.

Rare for collection members (usually objects), but the inconsistency is real. Could align them in this PR (!cache.get(key) in the outer check), or fix multiGet properly later (touches other callers). Not a blocker — just worth deciding.

I think it's better to check multiGet

});
});

describe('mergeCollection pre-warm', () => {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

All the changes above this part don't seem necessary

@elirangoshen
Copy link
Copy Markdown
Author

Closing — this PR was opened against callstack-internal/react-native-onyx by mistake. Reopened against the upstream Expensify repo with the same 3 commits rebased onto Expensify/react-native-onyx:main (post-Expensify#787 merge):

➡️ Expensify#793

Diff and body carried over verbatim. 456/456 unit tests pass on the rebased branch.

elirangoshen added a commit that referenced this pull request May 26, 2026
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>
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.

2 participants