App icon indicating copy to clipboard operation
App copied to clipboard

[Due for payment 2026-11-26] [$175] [Performance] Onyx: skip unchanged cached objects when removing nested nulls on set

Open melvin-bot[bot] opened this issue 1 month ago • 29 comments

Problem

Every Onyx.set runs removeNestedNullValues over the whole value before caching and storing it. The App rewrites some large values repeatedly, especially derived values with one entry per report. On heavy accounts, each Pusher update takes ~350 ms on iOS and Android, and part of that cost scales with the stored value's size rather than the update's.

Solution (react-native-onyx)

  1. Keep the cache free of nulls. This restores the rule from Expensify/react-native-onyx#554 by fixing 4 paths that put nulls in the cache today: batched remove-then-replace merges, lazy SQLite reads, web cross-tab sync, and Onyx.clear().
  2. Pass the cached value to removeNestedNullValues. It then skips any nested object that is the same object as in the cache. This relies on the shared references kept since Expensify/react-native-onyx#765, and subscribers receive the same values.
  3. Add a dev-only alert if App code mutates a cached object in place.

Behavior change

On those 4 paths, subscribers get a missing key instead of null. Testing on web and native found this only for account.errors, report.errorFields.export, and report.errorFields.notFound. All three treat null and missing the same way.

Results (heavy account, median before → after)

  • Each Pusher update: iOS 347→331 ms (-4.6%), Android 349→336 ms (-3.7%), Web CPU x4 326→310 ms (-4.9%)
  • Incremental reconnect: iOS ~700→~660 ms, Web CPU x4 602→564 ms (-6.6%)
  • Setting a large object: iOS ~290→~265 ms, Android 293→282 ms
  • Tested on top of the pending OnyxDerived no-op-write skip, the gain holds: Pusher update 341→316 ms (-7.2%), reconnect 580→532 ms (-5.5%), 5/5 runs faster.
  • Most of the gain comes from visibleReportActions, sortedReportActions, and outstandingReportsByPolicyID. reportAttributes doesn't benefit, because its undefined fields break sharing after a full recompute. Fixing that on the App side is a possible follow-up.

Verify in production

In Sentry, StartupData.Apply should get shorter for ReconnectApp on heavy accounts (~40 ms on iOS).

Links

  • Onyx PR: https://github.com/Expensify/react-native-onyx/pull/856
  • Background: https://github.com/Expensify/App/issues/71205
  • Related: https://github.com/Expensify/App/pull/98956

Reported in Slack.

Issue OwnerCurrent Issue Owner: @mallenexpensify
Upwork Automation - Do Not Edit

melvin-bot[bot] avatar Oct 09 '26 15:10 melvin-bot[bot]

Let us know when the PR is ready

mountiny avatar Oct 12 '26 10:10 mountiny

Triggered auto assignment to Contributor-plus team member for initial proposal review - @Krishna2323 (External)

melvin-bot[bot] avatar Oct 12 '26 10:10 melvin-bot[bot]

Job added to Upwork: https://www.upwork.com/jobs/~022109596033799181646

melvin-bot[bot] avatar Oct 12 '26 10:10 melvin-bot[bot]

Proposal

What is the root cause of that problem?

The cost is in react-native-onyx, not in App code. Onyx.set calls removeNestedNullValues on the entire new value and walks every nested object, even though most of those objects are the same references already held in the cache (shared since react-native-onyx#765). The walk cannot skip them because the cache cannot be trusted to be null-free: four paths still put nulls into it (batched remove-then-replace merges, lazy SQLite reads, web cross-tab sync, Onyx.clear()), which breaks the rule from react-native-onyx#554. App only consumes the library through the pinned dependency at https://github.com/Expensify/App/blob/37e2b8b8fb247f003796221219ebb979d78cff94/package.json#L204. I did not have the library source in this checkout, so the library-side description below follows the issue and the linked Onyx PR #856.

What changes do you think we should make in order to solve the problem?

  • In react-native-onyx, strip nulls on the four paths above (batched remove-then-replace merge, lazy SQLite read, web cross-tab sync, Onyx.clear()) so the cache never holds nulls.
  • Give removeNestedNullValues an optional cached-value argument and return early for any nested object that is === to the cached one, since a null-free cached object needs no scan.
  • Have Onyx.set pass the current cached value into that call.
  • Add a dev-only check that warns when App code mutates a cached object in place, since the skip depends on cached objects staying unchanged.
  • Add Onyx unit tests: a nested object that is the same reference as the cached one is not traversed, and each of the four paths leaves no nulls in the cache.
  • Release react-native-onyx and bump the version in App's package.json (currently 3.0.116).
  • In App, check that the only subscriber-visible change is null becoming missing for account.errors, report.errorFields.export and report.errorFields.notFound. The issue says all three treat the two the same.
  • Optional follow-up in App: stop reportAttributes from emitting undefined fields so its shared references survive a full recompute.

What alternative solutions did you explore? (Optional)

  • Skip only the OnyxDerived no-op writes in App: this is a separate fix that already exists, and it does not help when a value changes slightly while most of its nested objects stay the same.
  • Keep scanning but cache a "null-free" flag per object: this needs an extra WeakSet lookup per object and is more complex than comparing against the cached reference.

haiquywork avatar Oct 12 '26 10:10 haiquywork

Not looking for proposals, only wanted to get C+ assigned

mountiny avatar Oct 12 '26 10:10 mountiny

https://github.com/Expensify/react-native-onyx/pull/856 - Its ready for review now

arekm213 avatar Oct 15 '26 14:10 arekm213

I'm OOO from tomorrow until the end of next week. Since I have a lot of context on this, I'd prefer to continue it myself when I'm back, including the review on react-native-onyx#856. @mountiny let me know if that's fine with you. If not, someone from Software Mansion will take it over in the meantime, thanks!

arekm213 avatar Oct 15 '26 14:10 arekm213

@arekm213 Huh... This is 4 days overdue. Who can take care of this?

melvin-bot[bot] avatar Oct 20 '26 21:10 melvin-bot[bot]

@arekm213 Still overdue 6 days?! Let's take care of this!

melvin-bot[bot] avatar Oct 22 '26 21:10 melvin-bot[bot]

I will continue working on this issue on Monday

arekm213 avatar Oct 23 '26 06:10 arekm213

@arekm213 Whoops! This issue is 2 days overdue. Let's get this updated quick!

melvin-bot[bot] avatar Oct 26 '26 21:10 melvin-bot[bot]

Im currently focusing on this issue which seems higher priority for me at the moment, i will try to get back to this issue soon, hopefully this week

arekm213 avatar Oct 27 '26 12:10 arekm213

@arekm213 Uh oh! This issue is overdue by 2 days. Don't forget to update your issues!

melvin-bot[bot] avatar Oct 30 '26 21:10 melvin-bot[bot]

Onyx PR is merged and released in 3.0.144, the App bump PR is on hold until the App catches up with NitroSQLite 10 and the Onyx.disconnect removal

arekm213 avatar Nov 02 '26 11:11 arekm213

@arekm213 Uh oh! This issue is overdue by 2 days. Don't forget to update your issues!

melvin-bot[bot] avatar Nov 05 '26 21:11 melvin-bot[bot]

Proposal

Please re-state the problem that we are trying to solve in this issue.

Every Onyx.set call recursively traverses the entire value via removeNestedNullValues before updating the in-memory cache and persistent storage. For large derived or collection-like Onyx values that are frequently updated via Onyx.set (sharing most nested object references with the previously cached value), each update takes ~350 ms on iOS and Android because removeNestedNullValues re-walks unchanged nested objects on every call.

What is the root cause of that problem?

In react-native-onyx (lib/Onyx.ts and lib/OnyxUtils.ts):

  1. removeNestedNullValues(value) currently accepts only the incoming value and recursively clones/walks every nested object and array, even when a nested property is the exact same object reference (===) as the corresponding property already stored in cache.get(key).
  2. removeNestedNullValues could not safely skip reference-equal cached subtrees (value[k] === cachedValue[k]) because 4 code paths in react-native-onyx were still able to leave null values inside the in-memory OnyxCache:
    • Batched remove-then-replace Onyx.merge operations
    • Lazy SQLite storage reads hydrating into cache
    • Web localStorage cross-tab synchronization (onStorageEvent)
    • Onyx.clear() with default key states

What changes do you think we should make in order to solve the problem?

In Expensify/react-native-onyx (and bumping the react-native-onyx dependency in Expensify/App/package.json):

  1. Ensure the in-memory OnyxCache is strictly null-free across all 4 remaining paths (batched merge changes, lazy SQLite reads, web cross-tab storage sync, and Onyx.clear()) by stripping nested null values before inserting into cache.
  2. Update removeNestedNullValues(value, cachedValue) in OnyxUtils.ts to accept the current cachedValue from cache.get(key). When walking nested properties, if nestedValue === cachedNestedValue, skip recursing into that subtree and reuse cachedNestedValue directly (and in __DEV__, assert that the skipped cached subtree contains no null values to catch in-place mutations of cached Onyx objects).
  3. In Expensify/App, bump react-native-onyx in package.json once the react-native-onyx PR (#856) is released, and verify that callers reading account.errors, report.errorFields.export, and report.errorFields.notFound handle omitted keys identically to null.

What alternative solutions did you explore? (Optional)

We explored refactoring individual Onyx.set callers in Expensify/App (such as derived Onyx configs) to use Onyx.merge, but derived keys hold computed snapshots of entire collections and naturally share unchanged sub-object references; skipping reference-equal cached subtrees in removeNestedNullValues solves the O(N) traversal bottleneck for all Onyx.set callers at once.

ghost avatar Nov 08 '26 16:11 ghost

📣 @jeff-graham-ai! 📣 Hey, it seems we don’t have your contributor details yet! You'll only have to do this once, and this is how we'll hire you on Upwork. Please follow these steps:

  1. Make sure you've read and understood the contributing guidelines.
  2. Get the email address used to login to your Expensify account. If you don't already have an Expensify account, create one here. If you have multiple accounts (e.g. one for testing), please use your main account email.
  3. Get the link to your Upwork profile. It's necessary because we only pay via Upwork. You can access it by logging in, and then clicking on your name. It'll look like this. If you don't already have an account, sign up for one here.
  4. Copy the format below and paste it in a comment on this issue. Replace the placeholder text with your actual details. Screen Shot 2022-11-16 at 4 42 54 PM Format:
Contributor details
Your Expensify account email: <REPLACE EMAIL HERE>
Upwork Profile Link: <REPLACE LINK HERE>

melvin-bot[bot] avatar Nov 08 '26 16:11 melvin-bot[bot]

@arekm213 Still overdue 6 days?! Let's take care of this!

melvin-bot[bot] avatar Nov 09 '26 21:11 melvin-bot[bot]

@arekm213 8 days overdue is a lot. Should this be a Weekly issue? If so, feel free to change it!

melvin-bot[bot] avatar Nov 11 '26 21:11 melvin-bot[bot]

We are still waiting for onyx update in mobile app

arekm213 avatar Nov 12 '26 17:11 arekm213