Repository navigation
Description
Coming from a proposal https://expensify.slack.com/archives/C08CZDJFJ77/p1783094934084619?thread_ts=1782801937.464879&cid=C08CZDJFJ77
Proposal: Coalesce OnyxDerived recomputes into one macrotask flush
Background
OnyxDerived values (e.g. reportAttributes) are precomputed values stored in Onyx so that expensive computations shared across the app run once instead of in every consumer. Each derived value declares a list of Onyx dependencies and opens one connection per dependency; whenever a dependency broadcasts a change, the connection callback runs the derived value's compute function. reportAttributes alone declares 13 dependencies (reports, transactions, violations, report actions, NVPs, policies, personal details, etc.) and is among the heaviest computes in the app. Critically, Onyx delivers a single logical write (such as a server response that touches reports, transactions, and violations together) as several separate broadcasts spread across microtasks, and each derived dependency is connected independently.
Problem
When a single logical Onyx update touches several dependencies of a derived value, each dependency's connection callback fires its own synchronous recompute, so one change recomputes the derived value many times back-to-back, blocking the JS thread and dropping frames during startup and report opens.
Solution
Coalesce the per-dependency callbacks for each derived value into a single compute, scheduled on the next macrotask, and reconstruct the changed-member delta at flush time:
- Coalescing. recomputeDerivedValue no longer computes inline. It records the triggering dependency in a pending set and, if no flush is already scheduled, schedules one via setTimeout(flushRecompute, 0). Every dependency callback from the same logical update lands in that one pending flush, so N recomputes collapse to 1.
- Flush-time delta reconstruction. Rather than depending on Onyx's per-broadcast sourceValue (which doesn't survive coalescing — you'd be merging N partial payloads — and is a mechanism being phased out), the flush reconstructs the delta itself. For each collection dependency that fired, it diffs the current snapshot against a baseline snapshot captured at the previous flush, using a new shared getCollectionDelta helper. Onyx's structural sharing keeps unchanged members reference-equal, so this is a cheap reference scan that yields exactly the changed members. Non-collection dependencies pass their whole value. The first flush has no baselines, so it computes from scratch (no sourceValues → full compute) and captures the initial baselines for subsequent diffs.
- Incremental configs are hardened to honor every coalesced trigger. Configs that update incrementally previously assumed a compute was triggered by a single dependency. Coalescing can now deliver several changed dependencies in one compute, so any config that branched on "which one changed" was updated to process all of them, namely reportAttributes, reportTransactionsAndViolations and sortedReportActions.
With recomputes coalesced, one logical update produces exactly one compute of each derived value instead of one-per-dependency, removing the redundant passes that block the thread. Derived values become eventually consistent within one macrotask of the originating write — consumers re-render normally through useOnyx when the single updated value lands.
Measured results (Applause heavy account)
- ManualAppStartup (inbox tab) = 2717 ms -> 2708 ms (−0.3%) (stable, no perf regressions)
- ManualSendMessage = 168 ms -> 84 ms (−50.0%)
- Derived computation after startup and ReconnectApp call
- Total time = 575 ms -> 215 ms (−62.6%)
- Dependencies changes = 62 -> 62 (expected, we want to see improvements in the recomputes)
- Recomputes = 62 -> 19 (−69.4%)
- On main, reportAttributes fired ~14 times/run, including a burst of large ones (123 + 105 + 101 + 75 ≈ 404 ms of compute).
- On the solution branch, reportAttributes fires ~4 times/run, with a single large compute (~110 ms) instead of four.
Upwork Automation - Do Not Edit
- Upwork Job URL: https://www.upwork.com/jobs/~022073092002479955520
- Upwork Job ID: 2073092002479955520
- Last Price Increase: 2026-08-06
Issue Owner
Current Issue Owner: @mallenexpensifyMetadata
Metadata
Labels
Type
Projects
- StatusShow more project fieldsDone
Activity
MelvinBot commentedon Jul 3, 2026
Analysis
This issue is a performance proposal (coalesce N per-dependency OnyxDerived recomputes into 1 macrotask flush), not a bug report — so this is a technical validation of the proposal rather than a root-cause analysis. Verdict: the proposal's description of the current architecture is accurate and the approach is sound, but there are two concrete correctness risks the implementation must handle.
Verified against code (
8f9ac4c):compute()immediately inside the Onyx callback, so one logical multi-collection write triggers N synchronous recomputes.OnyxDerived/index.ts:68-103reportAttributesdeclares 14 dependencies (proposal says "~13") and is the heaviest compute.configs/reportAttributes.ts:86-101sourceValues, so they genuinely must be updated to process all coalesced triggers:reportAttributes.ts:123-182,reportTransactionsAndViolations.ts:16-32,sortedReportActions.ts:54-87. (visibleReportActions.tsalso branches onsourceValuesand should be checked for the same assumption.)Synchronous readers get one-macrotask-stale data. Deferring recompute means any code path that writes a dependency and then synchronously reads the derived value in the same tick sees the old value.
ReportUtilscachesreportAttributesDerivedValueand reads it non-reactively in many synchronous utilities — e.g.getReportNameandgetReportPreviewMessage(ReportUtils.ts:1217-1221). ReactiveuseOnyxconsumers are safe (they re-render on the eventual write); synchronous utility reads are the exposure. Worth auditing write→sync-read sequences before merging.Tests assuming synchronous derivation will need updating.
tests/unit/OnyxDerivedTest.tsxmixes patterns — someawait waitForBatchedUpdates()(which drainssetTimeout(0), likely fine) but some write then immediatelyawait OnyxUtils.get(...)with no batched-update wait, which relies on inline recompute and will break under a deferred flush. Timer mode (fake vs real) matters for thesetTimeout(0)flush.Notes / claims not verifiable from this checkout
sourceValueis "being phased out" is a roadmap statement not evidenced in this checkout — worth confirming againstreact-native-onyx.sourceValue"doesn't survive coalescing" is subtly imprecise: within a singleOnyx.multiSet, Onyx does build a per-collection partial passed assourceValue. The claim really applies to the cross-broadcast coalescing the proposal itself introduces (the pending set spanning multiple microtask broadcasts), where no singlesourceValuecovers all triggers — which is exactly why thegetCollectionDeltabaseline-diff replacement is needed. The structural-sharing assumption behind that diff holds:keysChanged/keyChangedalready rely on reference equality for unchanged members.Confidence: High on the code claims (all verified); the two roadmap/Onyx-internal points above are flagged as unverified.
Next Steps for Contributor+ team:
To accept:
@MelvinBot implement [this](https://github.com/Expensify/App/issues/95301)to create a draft PR.To refine:
@MelvinBot <your feedback>To reject: Explain why you are rejecting Melvin's proposal.
shivang-goliyan commentedon Jul 3, 2026
aswin-s commentedon Jul 3, 2026
David-2002065 commentedon Jul 3, 2026
melvin-bot commentedon Jul 3, 2026
63 remaining items
mountiny commentedon Aug 6, 2026
Yeah that is fair!
[-][$250] Coalesce OnyxDerived recomputes into one macrotask flush[/-][+][$125] Coalesce OnyxDerived recomputes into one macrotask flush[/+]melvin-bot commentedon Aug 6, 2026
Upwork job price has been updated to $125
[-][$125] Coalesce OnyxDerived recomputes into one macrotask flush[/-][+][Due for payment 2026-08-06] [$125] Coalesce OnyxDerived recomputes into one macrotask flush[/+]melvin-bot commentedon Aug 6, 2026
Current assignee @mallenexpensify is eligible for the Awaiting Payment assigner, not assigning anyone new.
mallenexpensify commentedon Aug 8, 2026
Payment Summary
Contributor+: @shubham1206agra due $125 via NewDot
shubham1206agra commentedon Aug 8, 2026
@mallenexpensify I am going to request $125 only. Can you fix the payment summary in the meantime?
mallenexpensify commentedon Aug 8, 2026
Thx, @shubham1206agra , I missed that with my text expander input. Updated
JmillsExpensify commentedon Aug 10, 2026
$125 approved for @shubham1206agra