Skip to content

perf: shrink SearchRouter's initial recent-reports build, paginate on scroll - #95683

Merged
mountiny merged 7 commits into
Expensify:mainfrom
software-mansion-labs:perf/search-router-defer-recent-reports
Jul 17, 2026
Merged

mountiny merged 7 commits into
Expensify:mainfrom
software-mansion-labs:perf/search-router-defer-recent-reports

Conversation

@WojtekBoman

@WojtekBoman WojtekBoman commented Jul 9, 2026 •

Copy link
Copy Markdown
Contributor

Explanation of Change

SearchRouter built full option data for the default ~500-report recent-reports list on every open, even though the recent-reports view only ever shows up to 20 rows (CONST.AUTO_COMPLETE_SUGGESTER.MAX_AMOUNT_OF_SUGGESTIONS). This PR makes that build lazy without changing anything the user sees:

  • useFilteredOptions now starts with a 100-report cap (INITIAL_MAX_RECENT_REPORTS) instead of building option data for all ~500 raw reports up front.
  • If fewer than 20 rows survive filtering (e.g. lots of hidden/muted chats), the window escalates to the full report set in a single step (loadAll) so the remaining slots fill from less-recent reports. This runs at most once per open and only in that under-fill case; when the initial 100-report window already fills the 20 visible slots, nothing extra is built. There is no scroll-driven pagination, since the view is capped at 20 rows and can never show more.
  • Typing a query bypasses the cap entirely (isSearching drops the limit in createFilteredOptionList), so search suggestions are unaffected.
  • Fixed a regression where the raw top-N cap could drop the self-DM report before recentReportComparator had a chance to keep it, by mirroring the self-DM priority in reportSortComparator.

This is a pure performance optimization. The recent-reports view is still capped at 20 rows exactly as before — the loadAll escalation is an internal mechanism to top up those 20 slots when the initial 100-report window has too many filtered-out reports, not user-facing infinite scroll.

Measured over 19 warm opens of SearchRouter:

Android native:

Metric main with fix
Avg 935.6 ms 846.1 ms
Median 883 ms 779 ms

Web:

Metric web with fix v2 web
Avg 207.1 ms 222.7 ms
Median 205.5 ms 218 ms

Fixed Issues

$ #79353 (comment)
PROPOSAL:

Tests

  1. Open the app and click the search bar (or press the keyboard shortcut) to open SearchRouter without typing a query.
  2. Verify the recent reports list appears immediately, with the same rows and ordering as before this change (the view shows up to 20 recent reports — this cap is unchanged).
  3. Verify that the self-DM report (chat with yourself) is still visible in the recent reports list and not dropped, even in an account with many reports.
  4. Type a search query into SearchRouter.
  5. Verify that suggestions still appear correctly and are not limited by the 100-report initial cap (the cap only applies to the unfiltered recent reports view).
  6. Log into an account where most/all recent reports are hidden or muted (or simulate this state) and open SearchRouter without a query.
  7. Verify that the list still surfaces visible rows instead of showing an empty state (the window escalates automatically until rows appear or no reports remain).
  • Verify that no errors appear in the JS console

Offline tests

  1. Go offline.
  2. Open SearchRouter and verify the recent reports list still loads from cached Onyx data, showing the same rows as when online.
  3. Verify the self-DM report is still present in the list while offline.

QA Steps

  1. Open the app and click the search bar to open SearchRouter without typing a query.
  2. Verify the recent reports list appears immediately and matches the rows shown before this change (up to 20 recent reports).
  3. Verify that the self-DM report (chat with yourself) is still visible in the recent reports list.
  4. Type a search query into SearchRouter and verify suggestions still appear correctly.
  • 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
Screen.Recording.2026-07-17.at.11.01.29.mov

@WojtekBoman

Copy link
Copy Markdown
Contributor Author

@codex

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: a877117d42

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@WojtekBoman

Copy link
Copy Markdown
Contributor Author

@codex

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 82783475b1

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@WojtekBoman
WojtekBoman force-pushed the perf/search-router-defer-recent-reports branch from 8edfafd to 82e15dd Compare July 9, 2026 12:44
@WojtekBoman

Copy link
Copy Markdown
Contributor Author

@codex

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 82e15ddc62

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@WojtekBoman

Copy link
Copy Markdown
Contributor Author

@codex

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. 👍

Reviewed commit: 3dbab84da2

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

@WojtekBoman
WojtekBoman marked this pull request as ready for review July 10, 2026 09:22
@WojtekBoman
WojtekBoman requested review from a team as code owners July 10, 2026 09:22
@melvin-bot
melvin-bot Bot requested review from heyjennahay and mollfpr and removed request for a team July 10, 2026 09:23
@melvin-bot

melvin-bot Bot commented Jul 10, 2026

Copy link
Copy Markdown

@mollfpr 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]

@WojtekBoman

Copy link
Copy Markdown
Contributor Author

cc: @shubham1206agra

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 3dbab84da2

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment on lines +627 to +631
useEffect(() => {
if (trimmedAutocompleteQueryValue !== '' || isLoadingOptions || suggestionsCount > 0 || !hasMoreRecentReports) {
return;
}
loadMoreRecentReports();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Throttle empty-list pagination to avoid render loops

When the blank SearchRouter opens for an account whose raw reports keep getting filtered out (for example many hidden chats, or no visible chats at all), suggestionsCount stays 0, so this effect calls loadMoreRecentReports() after every render until hasMoreRecentReports becomes false or a row finally appears. Each call synchronously bumps the cap and rebuilds createFilteredOptionList over the report collection, so high-traffic accounts can do dozens of immediate renders/recomputations on open and may hit React's update-depth guard before anything is displayed; this should be capped, deferred, or limited to one retry per frame/user scroll.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

It has been addressed!

@WojtekBoman
WojtekBoman force-pushed the perf/search-router-defer-recent-reports branch from 3dbab84 to 37277dd Compare July 10, 2026 10:22
@codecov

codecov Bot commented Jul 10, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Looks like you've decreased code coverage for some files. Please write tests to increase, or at least maintain, the existing level of code coverage. See our documentation here for how to interpret this table.

Files with missing lines Coverage Δ
src/components/Search/SearchAutocompleteList.tsx 93.26% <100.00%> (+0.19%) ⬆️
src/libs/OptionsListUtils/index.ts 86.88% <100.00%> (+0.79%) ⬆️
src/hooks/useFilteredOptions.ts 80.00% <50.00%> (-3.34%) ⬇️
... and 663 files with indirect coverage changes

@mollfpr

mollfpr commented Jul 10, 2026

Copy link
Copy Markdown
Contributor

@WojtekBoman, is @shubham1206agra reviewing this? If yes, I will assign them for reviewer.

@WojtekBoman

Copy link
Copy Markdown
Contributor Author

@WojtekBoman, is @shubham1206agra reviewing this? If yes, I will assign them for reviewer.

Yes :) @shubham1206agra will review this

@mollfpr
mollfpr requested a review from shubham1206agra July 15, 2026 07:18
@shubham1206agra

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 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 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 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
    • 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 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

Screen.Recording.2026-07-16.at.6.51.59.PM.mov

@melvin-bot
melvin-bot Bot requested a review from mountiny July 17, 2026 09:12
@mountiny
mountiny merged commit f2e94e7 into Expensify:main Jul 17, 2026
38 checks passed
@OSBotify

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.

@github-actions

Copy link
Copy Markdown
Contributor

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

@OSBotify

Copy link
Copy Markdown
Contributor

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

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

@MelvinBot

Copy link
Copy Markdown
Contributor

Help site review: no docs changes required

I reviewed the changes in this PR against Expensify's help site articles under App/docs/articles and no documentation updates are required.

Why: This is a pure performance optimization of how SearchRouter builds its recent-reports option list. As the PR description states, the recent-reports view is still capped at 20 rows exactly as before, ordering is unchanged, and search behavior is unaffected — the loadAll escalation and the INITIAL_MAX_RECENT_REPORTS cap are internal mechanisms with no user-visible effect. There are no new features, UI labels, tabs, settings, or behavioral changes for a user to be documented.

I searched App/docs/articles for search/recent-reports related content and found nothing describing the SearchRouter recent-reports list behavior that this change would render inaccurate.

Note: I could not verify this in a browser (NewDot web browser control was unavailable in this run), so this assessment is based on static analysis of the diff and the help-site articles alone.

@WojtekBoman, since no help site changes are required, I did not create a draft docs PR. If you believe a user-facing behavior did change and docs should be updated, let me know and I'll create one.

@OSBotify

Copy link
Copy Markdown
Contributor

🚀 Deployed to production by https://github.com/yuwenmemon in version: 9.4.41-4 🚀

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.

7 participants