Skip to content

Perf: use indexed derived value in useReportTransactions - #97965

Merged
mountiny merged 2 commits into
Expensify:mainfrom
callstack-internal:perf/report-transactions-indexed-lookup
Aug 7, 2026
Merged

mountiny merged 2 commits into
Expensify:mainfrom
callstack-internal:perf/report-transactions-indexed-lookup

Conversation

@TMisiukiewicz

@TMisiukiewicz TMisiukiewicz commented Aug 6, 2026 •

Copy link
Copy Markdown
Contributor

Explanation of Change

Found via CPU profiling while investigating blank rows when scrolling the Search Spends tab. No linked issue.

useReportTransactions previously connected to the full transactions Onyx collection and did an Object.values(...).filter(...) scan over every transaction in the app to find the ones belonging to a single report, for every consumer of the hook. This change makes it reuse useReportTransactionsCollection, which reads from the reportTransactionsAndViolations derived value already indexed by reportID, so lookups are O(1) per report instead of a full linear scan of the collection.

The set of transactions returned is identical — the derived value groups transactions by transaction.reportID. React Compiler memoizes the Object.values().filter(), so the returned array stays reference-stable.

Profiling evidence

Chrome DevTools trace of scrolling the Spends tab (10.4s total, 6.56s idle). Top self-time frames:

self time frame
426.1ms getTransactionsForReport (pre-existing, separate)
317.0ms selector ← BaseKYCWall → useReportTransactions
262.6ms isTransactionEntry
240.0ms getViolationsFromSearchData

Call stack: BaseKYCWall → useReportTransactions → useOnyx → updateSyncExternalStore → getSnapshot → selector.

Every Spends row renders a KYC wall via PayActionCell → SettlementButton → KYCWall, so each row scrolling into view triggered a full scan of the transactions collection. That selector accounted for ~8% of all busy JS in the trace.

Call sites that benefit

Fixed in the shared hook, so all three consumers improve:

  • src/components/KYCWall/BaseKYCWall.tsx
  • src/components/ReportActionItem/MoneyReportView.tsx
  • src/components/ReportActionItem/MoneyRequestView.tsx

Fixed Issues

$ #97977
PROPOSAL:

Tests

  1. Sign in with an account that has many reports and transactions.
  2. Go to Search → Spends and scroll a long list of rows.
    1. Verify rows render normally while scrolling.
    2. Verify the Pay button appears on the same rows it did before this change.
    3. Pay a report directly from a Spends row and verify the payment completes.
  3. Open an expense report (MoneyReportView) and verify the transaction list and totals are unchanged.
  4. Open a single expense (MoneyRequestView) and verify the fields render and the report's transactions resolve as before.
  • Verify that no errors appear in the JS console

Offline tests

N/A

QA Steps

// TODO: These must be filled out, or the issue title must include "[No QA]."

Same as tests

  • Verify that no errors appear in the JS console

PR Author Checklist

  • I linked the correct issue in the ### Fixed 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 added steps for the expected offline behavior in the Offline steps section
    • I added steps for Staging and/or Production testing in the QA steps section
    • I added steps to cover failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
    • 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: mWeb Chrome
    • iOS: Native
    • iOS: mWeb 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 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 any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is either coming verbatim from figma or has been approved by marketing (in order to get marketing approval, ask the Bug Zero team member to add the Waiting for copy label to the issue)
  • 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)
  • If a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(theme.componentBG))
  • If new assets were added or existing ones were modified, I verified that:
    • The assets are optimized and compressed (for SVG files, run npm run compress-svg)
    • The assets load correctly across all supported platforms.
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • 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 PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If the PR modifies the UI (e.g. new buttons, new UI components, changing the padding/spacing/sizing, moving components, etc) or modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label and/or tagged @Expensify/design so the design team can review the changes.
  • I added unit tests for any new feature or bug fix in this PR to help automatically prevent regressions in this user flow.
  • 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.

Screenshots/Videos

Android: Native
Android: mWeb Chrome
iOS: Native
iOS: mWeb Safari
MacOS: Chrome / Safari

@TMisiukiewicz
TMisiukiewicz force-pushed the perf/report-transactions-indexed-lookup branch from e14c097 to 943ffd8 Compare August 6, 2026 12:06
@TMisiukiewicz TMisiukiewicz changed the title Perf: use indexed derived value in useReportTransactions [WIP] Perf: use indexed derived value in useReportTransactions Aug 6, 2026
@github-actions

github-actions Bot commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

🚧 mountiny has triggered a test Expensify/App build. You can view the workflow run here.

@TMisiukiewicz TMisiukiewicz changed the title [WIP] Perf: use indexed derived value in useReportTransactions Perf: use indexed derived value in useReportTransactions Aug 6, 2026
@mountiny

mountiny commented Aug 6, 2026

Copy link
Copy Markdown
Contributor

@MelvinBot review

@mountiny
mountiny self-requested a review August 6, 2026 13:31
@mountiny
mountiny marked this pull request as ready for review August 6, 2026 13:31
@mountiny
mountiny requested review from a team as code owners August 6, 2026 13:31
@melvin-bot
melvin-bot Bot requested review from daledah and heyjennahay August 6, 2026 13:31
@melvin-bot

melvin-bot Bot commented Aug 6, 2026

Copy link
Copy Markdown

@daledah Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button]

@melvin-bot
melvin-bot Bot requested review from a team and eh2077 and removed request for a team August 6, 2026 13:31
@melvin-bot

melvin-bot Bot commented Aug 6, 2026

Copy link
Copy Markdown

@eh2077 Please copy/paste the Reviewer Checklist from here into a new comment on this PR and complete it. If you have the K2 extension, you can simply click: [this button]

@MelvinBot

Copy link
Copy Markdown
Contributor

Review: LGTM ✅

Clean, well-justified perf change. Swapping the full-collection Object.values().filter() scan for the reportID-indexed derived value is the right fix, and it's semantically equivalent. A few minor notes below, none blocking.

Correctness — the returned set is identical. The derived value groups transactions by transaction.reportID (reportTransactionsAndViolations.ts:112), which matches the old transaction.reportID === reportID filter exactly. The undefined reportID case also matches: useReportTransactionsCollection returns the empty-object singleton → Object.values({}).filter() → [], same as the old early return.

Reference stability — preserved. React Compiler is enabled repo-wide (compilationMode: infer, src/ included), so the new Object.values(...).filter(...) in useReportTransactions.ts:12 is memoized on the stable reportTransactions reference (useOnyx selector output, or CONST.EMPTY_OBJECT for the empty case). The passing React Compiler Compliance check confirms the hook compiles, so consumers keep getting a reference-stable array. Good.

Minor / optional
  • Redundant ?? {} in Object.values(reportTransactions ?? {}). useReportTransactionsCollection already returns getEmptyObject() and never returns undefined, so the fallback is dead. Could simplify to Object.values(reportTransactions) — harmless either way.
  • PR body undercounts consumers. It lists 3 call sites, but the hook has 7 (also DynamicReportChangeWorkspacePage, IOURequestEditReportCommon.tsx, useExpenseSubmission.ts, MoneyReportContentCreated.tsx). All of them benefit from the same fix — just worth noting for accuracy.
  • Timing nuance. The derived value updates one compute cycle after the TRANSACTION collection changes, whereas the old hook read the raw collection directly. There's a theoretical sub-frame window where the new hook returns empty before the derived value catches up. This is the same tradeoff the other useReportTransactionsCollection consumers already accept, so low risk — flagging only for completeness.
  • The initOnyxDerivedValues() additions to the three test files are the correct and necessary consequence of the hook now depending on the derived value; the green test suite confirms no other tests transitively render these components without it.

Testing note: I ran a smoke test of Search → Spend on web, but the test account had no seeded expenses/reports, so the row-rendering and Pay-button paths couldn't be exercised live. Navigation was clean with no console errors. Functional equivalence is otherwise well covered by the passing MoneyReportView/MoneyRequestView/MoneyReportContentCreated UI tests and the green branch-perf-tests.

The only failing check is Check independent approval, which is process-related, not a code failure.

@daledah

daledah commented Aug 6, 2026 •

Copy link
Copy Markdown
Contributor

Reviewer Checklist

  • I have verified the author checklist is complete (all boxes are checked off).
  • I verified the correct issue is linked in the ### Fixed Issues section above
  • I verified testing steps are clear and they cover the changes made in this PR
    • I verified the steps for local testing are in the Tests section
    • I verified the steps for Staging and/or Production testing are in the QA steps section
    • I verified the steps cover any possible failure scenarios (i.e. verify an input displays the correct error message if the entered data is not correct)
    • I turned off my network connection and tested it while offline to ensure it matches the expected behavior (i.e. verify the default avatar icon is displayed if app is offline)
  • I checked that screenshots or videos are included for tests on all platforms
  • I included screenshots or videos for tests on all platforms
  • I verified that the composer does not automatically focus or open the keyboard on mobile unless explicitly intended. This includes checking that returning the app from the background does not unexpectedly open the keyboard.
  • I verified tests pass on all platforms & I tested again on:
    • Android: HybridApp
    • Android: mWeb Chrome
    • iOS: HybridApp
    • iOS: mWeb Safari
    • MacOS: Chrome / Safari
  • If there are any errors in the console that are unrelated to this PR, I either fixed them (preferred) or linked to where I reported them in Slack
  • I verified proper code patterns were followed (see Reviewing the code)
    • 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 any copy / text that was added to the app is grammatically correct in English. It adheres to proper capitalization guidelines (note: only the first word of header/labels should be capitalized), and is either coming verbatim from figma or has been approved by marketing (in order to get marketing approval, ask the Bug Zero team member to add the Waiting for copy label to the issue)
  • If a new code pattern is added I verified it was agreed to be used by multiple Expensify engineers
  • I verified that this PR follows the guidelines as stated in the Review Guidelines
  • I verified other components that can be impacted by these changes have been tested, and I retested again (i.e. if the PR modifies a shared library or component like Avatar, I verified the components using Avatar have been tested & I retested again)
  • If a new component is created I verified that:
    • A similar component doesn't exist in the codebase
    • All props are defined accurately
    • 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
    • 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 a new CSS style is added I verified that:
    • A similar style doesn't already exist
    • The style can't be created with an existing StyleUtils function (i.e. StyleUtils.getBackgroundAndBorderStyle(theme.componentBG)
  • If the PR modifies code that runs when editing or sending messages, I tested and verified there is no unexpected behavior for all supported markdown - URLs, single line code, code blocks, quotes, headings, bold, strikethrough, and italic.
  • 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 PR modifies a component related to any of the existing Storybook stories, I tested and verified all stories for that component are still working as expected.
  • If the PR modifies a component or page that can be accessed by a direct deeplink, I verified that the code functions as expected when the deeplink is used - from a logged in and logged out account.
  • If the PR modifies the UI (e.g. new buttons, new UI components, changing the padding/spacing/sizing, moving components, etc) or modifies the form input styles:
    • I verified that all the inputs inside a form are aligned with each other.
    • I added Design label and/or tagged @Expensify/design so the design team can review the changes.
  • For any bug fix or new feature in this PR, I verified that sufficient unit tests are included to prevent regressions in this flow.
  • 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 reviewer checklist, including those that don't apply to this PR.

Screenshots/Videos

MacOS: Chrome / Safari
Screen.Recording.2026-08-06.at.21.55.41.mov

@daledah daledah left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@mountiny
mountiny removed the request for review from eh2077 August 6, 2026 16:59
@mountiny mountiny closed this Aug 7, 2026
@mountiny mountiny reopened this Aug 7, 2026
@mountiny
mountiny merged commit 09599b9 into Expensify:main Aug 7, 2026
46 of 49 checks passed
@melvin-bot

melvin-bot Bot commented Aug 7, 2026

Copy link
Copy Markdown

@mountiny looks like this was merged without a test passing. Please add a note explaining why this was done and remove the Emergency label if this is not an emergency.

@melvin-bot melvin-bot Bot added the Emergency label Aug 7, 2026
@OSBotify

OSBotify commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

✋ This PR was not deployed to staging yet because QA is ongoing. It will be automatically deployed to staging after the next production release.

@mountiny

Copy link
Copy Markdown
Contributor

Not emergency, seems like the action was still running though the button was green

@OSBotify

Copy link
Copy Markdown
Contributor

🚀 Deployed to staging by https://github.com/mountiny in version: 9.4.52-0 🚀

platform result
🕸 web 🕸 success ✅
🤖 android 🤖 success ✅
🍎 iOS 🍎 cancelled 🔪

@MelvinBot

Copy link
Copy Markdown
Contributor

🤖 No help site changes required.

I reviewed the changes in this PR against the help site content under App/docs/articles, and no documentation updates are needed — so I did not open a draft PR.

Why: This is a purely internal performance optimization with no user-facing behavior change. useReportTransactions is swapped from an Object.values().filter() linear scan over the full transactions Onyx collection to an O(1) indexed lookup via useReportTransactionsCollection (the reportTransactionsAndViolations derived value). As the PR description notes, the set of transactions returned is identical; the remaining changes are test setup (initOnyxDerivedValues()). Nothing about features, tabs, settings, buttons, pricing, billing, workspaces, or any documented workflow changes — the help site describes product behavior, and that behavior is unchanged here.

If you believe a specific article is affected by this change, let me know which one and I'll take another look.

@IuliiaHerets

Copy link
Copy Markdown

Hi @TMisiukiewicz. Can you please share QA steps?

cc @mountiny @heyjennahay

@OSBotify

Copy link
Copy Markdown
Contributor

🚀 Deployed to production by https://github.com/roryabraham in version: 9.4.52-11 🚀

platform result
🕸 web 🕸 success ✅
🤖 android 🤖 success ✅
🍎 iOS 🍎 success ✅

Bundle Size Analysis (Sentry):

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.

6 participants