Skip to content

Perf/spend filters hover - #97697

Draft
sumo-slonik wants to merge 8 commits into
Expensify:mainfrom
software-mansion-labs:perf/spend-filters-hover
Draft

Perf/spend filters hover#97697
sumo-slonik wants to merge 8 commits into
Expensify:mainfrom
software-mansion-labs:perf/spend-filters-hover

Conversation

@sumo-slonik

@sumo-slonik sumo-slonik commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Explanation of Change

What was wrong

Every hover over a filter row remounted the whole content pane (key={baseFilterKey} forces a full remount), and the people filters (From, To, Attendee) rebuilt the entire personal detail option list from scratch on every mount: several full O(n) passes over the personalDetailsList collection (building options, filtering, sorting, cloning). On accounts with a large number of contacts this blocked the JS thread for hundreds of milliseconds per hover, so sweeping the cursor down the filter list froze the popover and the highlight lagged several rows behind the cursor.

What this PR changes

  1. Hover intent debounce: the row highlight follows the cursor immediately, but the content pane now follows only once the cursor has rested on a row for SEARCH_FILTER_HOVER_INTENT_DELAY (80 ms). Sweeping across rows no longer renders a content pane per row.
  2. Keeping visited panes mounted: the MAX_MOUNTED_FILTER_CONTENTS (3) most recently visited filter contents stay mounted and are hidden instead of unmounted, with LRU eviction. Returning to a recently used filter toggles visibility instead of paying the full remount cost, and it preserves the pane state (e.g. a typed search term). A backgrounded content keeps the form values it had at its last visit, so moving between filters does not re-render it; a content whose values went stale is remounted on the way back in.
  3. Memoizing the derivations behind the people filters: createOptionList in usePersonalDetailOptions, and getValidOptions plus the selected-state mapping in usePersonalDetailSearchSelector, are pure derivations of Onyx data, so they are memoized at module level and survive a remount. They use a new equivalentArgsComparator exported from @libs/memoize, which compares memoization keys argument by argument: shallowly first, and one level deeper for plain objects that are rebuilt on every call from unchanged sources (e.g. a mapped Onyx collection). usePersonalDetailOptions also skips its whole derivation chain while it is loading or disabled.

Measurements

Chrome, dev build, an account seeded with 100k personal details. One run = sweeping the cursor over all 12 filter rows without stopping (3 passes each way), then resting on each row, then alternating between From and To. The metric is the total longtask duration - how long the JS thread was blocked - summed over the whole run. Variants were interleaved rather than measured in blocks, and each number below is a mean over 2-5 runs.

blocked JS vs main removed by this step
main 14804 ms - -
+ hover intent debounce + keeping panes mounted (1 and 2) 4176 ms -72% -72%
+ memoized createOptionList (3) 2181 ms -85% -48%
+ memoized getValidOptions and selected-state mapping (3) 1686 ms -89% -23%

The debounce alone accounts for most of it: it takes the fast-sweep phase from 9.1 s of blocked JS to 0.

Two further changes were built and measured, and are deliberately not part of this PR because they did not earn their complexity:

tried removed why it was dropped
a render-less component holding the personal detail Onyx subscriptions open for as long as the popover is open -2% below the run-to-run spread of the step it sits on (that step's own range is 386 ms wide)
memoizing the final list building in UserSelector -5% same, and it adds a third module-level cache

Absolute values are inflated by the dev build; the ordering and the ratios are the part to trust.

Before / after comparison

performance.filters.mp4

Fixed Issues

$ #97455
PROPOSAL:

Tests

Best tested on an account with a large number of contacts (e.g. a High Traffic account), where the freeze was clearly visible before this change.

  1. Open the advanced filters popover on the Reports page (the Filters button next to the search field) on web with a wide layout.
  2. Quickly sweep the cursor up and down the filter list without stopping, and verify the row highlight follows the cursor instantly, no content pane flashes by for the rows the cursor only passes over, and the popover does not freeze.
  3. Rest the cursor on From and verify its content pane appears right after the cursor stops.
  4. Type a few characters into the search input of the From pane, hover over To, then move back to From, and verify the From pane shows up instantly (no loading placeholder) with the typed search term preserved.
  5. Select one or two users in the From pane and verify the selection is applied to the search query and the selected users appear at the top of the list.
  6. Close the popover, reopen it and hover over From again, and verify the list is correct and reflects the selection made in step 5.
  7. Walk the filter list with the keyboard (Tab / arrow keys) and verify the content pane follows the focused row.
  • Verify that no errors appear in the JS console

Offline tests

Unnecessary - this is a rendering performance change only, no network interaction is affected.

QA Steps

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.

@codecov

codecov Bot commented Aug 3, 2026

Copy link
Copy Markdown

Codecov Report

✅ Changes either increased or maintained existing code coverage, great job!

Files with missing lines Coverage Δ
src/CONST/index.ts 94.81% <ø> (ø)
...lterDropdowns/SearchAdvancedFiltersPopup/index.tsx 100.00% <100.00%> (+100.00%) ⬆️
... and 878 files with indirect coverage changes

@sumo-slonik
sumo-slonik force-pushed the perf/spend-filters-hover branch 9 times, most recently from 51bb726 to 49096a3 Compare August 7, 2026 10:49
Hovering a filter row in the Search filters popover remounted its whole
content and rebuilt the option lists from scratch, which blocked the main
thread for over a second per row on accounts with large personal details.

- Switch the content pane to a debounced rested filter, so sweeping the
  cursor across rows moves only the highlight.
- Keep the last three rested filter contents mounted inside <Activity>,
  so returning to one toggles visibility instead of remounting.
- Memoize the three full passes over the personal details list
  (createOptionList, getValidOptions, the UserSelector list copy) with a
  comparator that walks one level into rebuilt record arguments, since the
  snapshot-aware useOnyx rebuilds collection identities on Search pages
  every render.
- Keep the option list Onyx subscriptions warm for the popover lifetime.
@sumo-slonik
sumo-slonik force-pushed the perf/spend-filters-hover branch from 49096a3 to b745fb8 Compare August 7, 2026 10:56
Drop the keep-warm Onyx subscription, the module-level memoization of the
personal detail derivations and the equivalentArgsComparator they needed.
Freezing the form values of backgrounded contents and comparing props on
them keeps the same panes from re-rendering, so the caches are unnecessary.

Explain why React.memo is still needed next to React Compiler: the compiler
caches the whole mountedFilters.map() result in a single slot, so it cannot
bail out on an individual element.
Building an option per personal details entry and filtering the whole option
list are pure derivations of Onyx data, so a remounted filter content can reuse
the previous result instead of walking the collection again.

Both are memoized at module level behind a new equivalentArgsComparator in
@libs/memoize, which compares memoization keys argument by argument: shallowly
first, and one level deeper for plain objects that are rebuilt on every call
from unchanged sources.

Measured on an account with 100k personal details, sweeping and resting on
every filter row: 4.2s of blocked JS down to 1.7s.
Comment thread src/libs/memoize/index.ts Outdated
* The two-level fallback below reads arguments through `Object.keys`, which only describes plain objects - for other
* types (Set, Map, Date, class instances) it returns an empty list and any two instances would look identical.
*/
const isPlainObject = (value: unknown): value is Record<string, unknown> => {

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.

Can't we use lodash-es/isPlainObject instead ?

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.

Yes, switching to it.

Comment thread src/CONST/index.ts Outdated
ACCESSIBILITY_ANNOUNCEMENT_DEBOUNCE_TIME: 1000,
SUGGESTION_DEBOUNCE_TIME: 100,
/** How long the cursor has to stay on an advanced filter row before its content is rendered */
SEARCH_FILTER_HOVER_INTENT_DELAY: 80,

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.

Can be a potential accessibility issue, if user navigates with keyboard they can land in old pane and then it switches ?

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.

Good catch, you're right, I'm fixing it.

The 80ms delay is currently applied to both mouse hover and keyboard focus. It makes sense for hover, because it avoids rendering filters you only pass over with the cursor. For keyboard navigation, every focus change is intentional, so the delay just causes the details pane to lag behind the selected row.

});
const [contentVersions, setContentVersions] = useState<Partial<Record<SearchFilter['key'], number>>>({});
if (mountedFilters.at(-1) !== restedFilter) {
if (mountedFilters.includes(restedFilter) && formAtLastRest[restedFilter] !== searchAdvancedFiltersForm) {

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.

NAB: Not sure if I understad this correct but in a scenario:

  1. We open filter
  2. Hover & Select From (From memo)
  3. Hover To (To memo)
  4. Hover back on From - the objects identities are different and we get full remount?

If this is correct, can it be changed to prevent this ?

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.

You read it correctly — it's intentional.

UserSelector snapshots the pre-selected accountIDs on mount to keep them pinned at the top (#61414). Keeping the pane mounted would make that snapshot stale, so newly selected users would not float to the top when returning to the view.

I did try an isActive prop that re-snapshots the selection when the pane becomes visible, but dropped that approach since it would require threading the prop through multiple components just to reach UserSelector.

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.

yes intentional but only when list size is large (>= 12)

* Building an option per personal details entry is the expensive step of this hook and depends only on Onyx values.
* Arguments are compared one level deep because the maps are rebuilt each render from those same unchanged values.
*/
const memoizedCreateOptionList = memoize(createOptionList, {

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.

Does this memoize survive logout ?

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 does - great catch, fixing it.

if (mountedFilters.includes(restedFilter) && formAtLastRest[restedFilter] !== searchAdvancedFiltersForm) {
setContentVersions({...contentVersions, [restedFilter]: (contentVersions[restedFilter] ?? 0) + 1});
}
setFormAtLastRest({...formAtLastRest, [restedFilter]: searchAdvancedFiltersForm});

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.

NAB: You want to keep 3 latest forms, but in theory this object can grow to how many restedFilter s variants there are

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.

I’ll update it to cap the maps like mountedFilters.

- Keyboard focus no longer goes through the hover intent delay. Moving focus is
  deliberate and never sweeps across rows, so its content is shown right away
  instead of replacing the pane a keyboard user has already moved into.
- The memoized personal detail caches are released on sign-out, so option lists
  built for the previous account do not stay in memory. Needed a clear() on the
  memoize cache, which it did not expose.
- formAtLastRest and contentVersions are pruned to the mounted filters instead of
  gaining an entry per visited filter.
- equivalentArgsComparator uses lodash/isPlainObject instead of a local copy.
@github-actions

Copy link
Copy Markdown
Contributor

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

@github-actions

This comment has been minimized.

@roryabraham
roryabraham self-requested a review August 13, 2026 19:14
@github-actions

Copy link
Copy Markdown
Contributor

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

@github-actions

Copy link
Copy Markdown
Contributor

🧪🧪 Use the links below to test this adhoc build on Android, iOS, and Web. Happy testing! 🧪🧪
Built from App PR #97697.

Android 🤖 iOS 🍎
❌ FAILED ❌ ❌ FAILED ❌
The QR code can't be generated, because the Android build failed The QR code can't be generated, because the iOS build failed
Web 🕸️
https://97697.pr-testing.expensify.com
Web

👀 View the workflow run that generated this build 👀

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.

4 participants