Skip to content

[Due for payment 2026-08-06] [$125] Coalesce OnyxDerived recomputes into one macrotask flush #95301

Closed

Description

@mountiny

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:

  1. 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.
  2. 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.
  3. 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
Issue OwnerCurrent Issue Owner: @mallenexpensify

Activity

  1. added
    BugSomething is broken. Auto assigns a BugZero manager.
    on Jul 3, 2026
  2. MelvinBot commented on Jul 3, 2026

    @MelvinBot
    Contributor

    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):

    • Recompute is inline/synchronous today — each dependency broadcast calls compute() immediately inside the Onyx callback, so one logical multi-collection write triggers N synchronous recomputes. OnyxDerived/index.ts:68-103
    • reportAttributes declares 14 dependencies (proposal says "~13") and is the heaviest compute. configs/reportAttributes.ts:86-101
    • No coalescing/debounce exists today — recompute is strictly 1:1 with broadcasts.
    • The three named incremental configs do branch on "which dependency changed" via sourceValues, so they genuinely must be updated to process all coalesced triggers: reportAttributes.ts:123-182, reportTransactionsAndViolations.ts:16-32, sortedReportActions.ts:54-87. (visibleReportActions.ts also branches on sourceValues and should be checked for the same assumption.)

    ⚠️ Two risks to handle in implementation

    1. 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. ReportUtils caches reportAttributesDerivedValue and reads it non-reactively in many synchronous utilities — e.g. getReportName and getReportPreviewMessage (ReportUtils.ts:1217-1221). Reactive useOnyx consumers are safe (they re-render on the eventual write); synchronous utility reads are the exposure. Worth auditing write→sync-read sequences before merging.

    2. Tests assuming synchronous derivation will need updating. tests/unit/OnyxDerivedTest.tsx mixes patterns — some await waitForBatchedUpdates() (which drains setTimeout(0), likely fine) but some write then immediately await 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 the setTimeout(0) flush.

    Notes / claims not verifiable from this checkout
    • The claim that Onyx sourceValue is "being phased out" is a roadmap statement not evidenced in this checkout — worth confirming against react-native-onyx.
    • The claim that sourceValue "doesn't survive coalescing" is subtly imprecise: within a single Onyx.multiSet, Onyx does build a per-collection partial passed as sourceValue. The claim really applies to the cross-broadcast coalescing the proposal itself introduces (the pending set spanning multiple microtask broadcasts), where no single sourceValue covers all triggers — which is exactly why the getCollectionDelta baseline-diff replacement is needed. The structural-sharing assumption behind that diff holds: keysChanged/keyChanged already rely on reference equality for unchanged members.
    • No likely-duplicate open issues found in a quick scan.

    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.

  3. added
    ExternalAdded to denote the issue can be worked on by a contributor
    on Jul 3, 2026
  4. shivang-goliyan commented on Jul 3, 2026

    @shivang-goliyan
  5. added
    Help WantedApply this label when an issue is open to proposals by contributors
    on Jul 3, 2026
  6. aswin-s commented on Jul 3, 2026

    @aswin-s
  7. David-2002065 commented on Jul 3, 2026

    @David-2002065
  8. melvin-bot commented on Jul 3, 2026

    @melvin-bot
  9. 63 remaining items

  10. mountiny commented on Aug 6, 2026

    @mountiny
    ContributorAuthor

    Yeah that is fair!

  11. changed the title [-][$250] Coalesce OnyxDerived recomputes into one macrotask flush[/-] [+][$125] Coalesce OnyxDerived recomputes into one macrotask flush[/+] on Aug 6, 2026
  12. added
    Awaiting PaymentAuto-added when associated PR is deployed to production
    on Aug 6, 2026
  13. melvin-bot commented on Aug 6, 2026

    @melvin-bot

    Upwork job price has been updated to $125

  14. changed the title [-][$125] Coalesce OnyxDerived recomputes into one macrotask flush[/-] [+][Due for payment 2026-08-06] [$125] Coalesce OnyxDerived recomputes into one macrotask flush[/+] on Aug 6, 2026
  15. melvin-bot commented on Aug 6, 2026

    @melvin-bot

    Current assignee @mallenexpensify is eligible for the Awaiting Payment assigner, not assigning anyone new.

  16. mallenexpensify commented on Aug 8, 2026

    @mallenexpensify
    Contributor

    Payment Summary

    Contributor+: @shubham1206agra due $125 via NewDot

  17. shubham1206agra commented on Aug 8, 2026

    @shubham1206agra
    Contributor

    @mallenexpensify I am going to request $125 only. Can you fix the payment summary in the meantime?

  18. mallenexpensify commented on Aug 8, 2026

    @mallenexpensify
    Contributor

    Thx, @shubham1206agra , I missed that with my text expander input. Updated

  19. JmillsExpensify commented on Aug 10, 2026

    @JmillsExpensify
    Contributor

    $125 approved for @shubham1206agra

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Awaiting PaymentAuto-added when associated PR is deployed to productionBugSomething is broken. Auto assigns a BugZero manager.ReviewingHas a PR in reviewWeeklyKSv2

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions