Repository navigation
Fix performance of search router in p95 tail #98282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
3a124fe
5064258
3d1e807
50f9533
ff7c193
d42684e
2a79e79
e7f08da
3c32afa
ab916e5
6864931
8cd9dcc
bb473ea
824ce95
e43ec01
8ab2d73
d5bac35
403eb77
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import useFilteredOptions from '@hooks/useFilteredOptions'; | ||
| import useOnyx from '@hooks/useOnyx'; | ||
| import useReportAttributes from '@hooks/useReportAttributes'; | ||
| import useSortedActions from '@hooks/useSortedActions'; | ||
|
|
||
| import {Scheduler} from '@libs/Scheduler'; | ||
| import type {IdleTask} from '@libs/Scheduler'; | ||
|
|
||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
|
|
||
| import {useEffect, useRef, useState} from 'react'; | ||
|
|
||
| import {getIsSearchRouterOpenOrOpening} from './SearchRouterContext'; | ||
| import SEARCH_ROUTER_OPTIONS_CONFIG from './searchRouterOptionsConfig'; | ||
|
|
||
| type SearchRouterOptionsWarmerProps = { | ||
| /** Called once the option list is cached (or the warm is no longer needed), so the parent can unmount this component. */ | ||
| onDone: () => void; | ||
| }; | ||
|
sumo-slonik marked this conversation as resolved.
Resolved
|
||
|
|
||
| // Stop waiting for quiet after this many idle windows. A chatty account never fully stops writing. | ||
| const MAX_QUIET_WAIT_ATTEMPTS = 5; | ||
|
|
||
| /** | ||
| * Builds the SearchRouter's empty-query option list so the first open of the session hits | ||
| * `createFilteredOptionList`'s cache instead of building it on the critical path. The build waits until | ||
| * the list's Onyx inputs hold still across idle windows, because the cache is keyed on the identity of | ||
| * the `useOnyx` snapshots and a list built mid-write is discarded. | ||
| */ | ||
| function SearchRouterOptionsWarmer({onDone}: SearchRouterOptionsWarmerProps) { | ||
| const [shouldBuild, setShouldBuild] = useState(false); | ||
|
|
||
| // The high-churn inputs of the option-list cache, read through the same `useOnyx` layer that keys it. | ||
| const [reports] = useOnyx(ONYXKEYS.COLLECTION.REPORT); | ||
| const [policies] = useOnyx(ONYXKEYS.COLLECTION.POLICY); | ||
| const [personalDetails] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST); | ||
| const reportAttributes = useReportAttributes(); | ||
| const sortedActions = useSortedActions(); | ||
|
|
||
| const churnVersionRef = useRef(0); | ||
| useEffect(() => { | ||
| churnVersionRef.current += 1; | ||
| }, [reports, policies, personalDetails, reportAttributes, sortedActions]); | ||
|
|
||
| useEffect(() => { | ||
| let attempts = 0; | ||
| let lastSeenChurn = -1; | ||
| let task: IdleTask | undefined; | ||
|
|
||
| const waitForQuiet = () => { | ||
| task = Scheduler.scheduleWhenIdle(() => { | ||
| // The real screen builds (and caches) the list itself, so warming would only compete with it. | ||
| if (getIsSearchRouterOpenOrOpening()) { | ||
| onDone(); | ||
| return; | ||
| } | ||
|
|
||
| if (churnVersionRef.current !== lastSeenChurn && attempts < MAX_QUIET_WAIT_ATTEMPTS) { | ||
| lastSeenChurn = churnVersionRef.current; | ||
| attempts += 1; | ||
| waitForQuiet(); | ||
|
Comment on lines
+58
to
+61
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When React Scheduler still has budget, an idle-priority callback scheduled from a currently running idle callback can execute in the same scheduler work loop; Useful? React with 👍 / 👎.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Confirmed: a recursive |
||
| return; | ||
| } | ||
|
|
||
| setShouldBuild(true); | ||
| }); | ||
| }; | ||
|
|
||
| waitForQuiet(); | ||
| return () => task?.cancel(); | ||
| }, [onDone]); | ||
|
|
||
| const {options} = useFilteredOptions({ | ||
| ...SEARCH_ROUTER_OPTIONS_CONFIG, | ||
| isSearching: false, | ||
| enabled: shouldBuild, | ||
| }); | ||
|
|
||
| useEffect(() => { | ||
| if (!options) { | ||
| return; | ||
| } | ||
| onDone(); | ||
| }, [options, onDone]); | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| export default SearchRouterOptionsWarmer; | ||
| export type {SearchRouterOptionsWarmerProps}; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import useOnyx from '@hooks/useOnyx'; | ||
|
|
||
| import {loadRightModalNavigator, loadSearchRouterPage} from '@libs/Navigation/AppNavigator/searchRouterLazyLoaders'; | ||
| import {Scheduler} from '@libs/Scheduler'; | ||
|
|
||
| import ONYXKEYS from '@src/ONYXKEYS'; | ||
|
|
||
| import type {ComponentType} from 'react'; | ||
|
|
||
| import {useEffect, useState} from 'react'; | ||
|
|
||
| import type {SearchRouterOptionsWarmerProps} from './SearchRouterOptionsWarmer'; | ||
|
|
||
| import {getIsSearchRouterOpenOrOpening} from './SearchRouterContext'; | ||
|
|
||
| /** | ||
| * Moves the first search-open of the session off the critical path: evaluates the SearchRouterPage and | ||
| * RightModalNavigator module graphs, then mounts SearchRouterOptionsWarmer to build the empty-query | ||
| * option list into `createFilteredOptionList`'s cache. Runs in the first idle window after OpenApp is | ||
| * applied, or after the scheduler's starvation fallback if the thread never goes idle. | ||
| */ | ||
| function SearchRouterWarmup() { | ||
| const [isLoadingApp] = useOnyx(ONYXKEYS.IS_LOADING_APP); | ||
| const [OptionsWarmer, setOptionsWarmer] = useState<ComponentType<SearchRouterOptionsWarmerProps> | null>(null); | ||
|
|
||
| useEffect(() => { | ||
| // Only OpenApp raises this flag, so it gates the fresh sign-in launch, where the account holds no data | ||
| // to build from yet. A relaunch runs ReconnectApp and reads the flag as false from disk, so the warm-up | ||
| // builds from the persisted collections; writes landing afterwards only invalidate the cache entry and | ||
| // the first open rebuilds the list, as it does without the warm-up. | ||
| if (isLoadingApp !== false) { | ||
| return; | ||
| } | ||
|
Comment on lines
+23
to
+33
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not a user-visible regression but this guard only works on the very first launch after logging in.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch, you're right that it's inert on a relaunch. I'd still keep it as-is: the collections are already on disk by then, and if reconnect data lands after the build we only lose the cache hit, so the first open rebuilds the list exactly as it does today. The stricter gates I looked at are worse — |
||
|
|
||
| const task = Scheduler.scheduleWhenIdle(() => { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we reuse
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea, but I don't think it fits this spot: at launch nothing has started a transition yet, so |
||
| // Already opening or open, so these costs are being paid live. | ||
| if (getIsSearchRouterOpenOrOpening()) { | ||
| return; | ||
| } | ||
|
|
||
| loadSearchRouterPage(); | ||
| loadRightModalNavigator(); | ||
| setOptionsWarmer(() => require<{default: ComponentType<SearchRouterOptionsWarmerProps>}>('@components/Search/SearchRouter/SearchRouterOptionsWarmer').default); | ||
| }); | ||
|
|
||
| return () => task.cancel(); | ||
| }, [isLoadingApp]); | ||
|
|
||
| if (!OptionsWarmer) { | ||
| return null; | ||
| } | ||
|
|
||
| return <OptionsWarmer onDone={() => setOptionsWarmer(null)} />; | ||
| } | ||
|
|
||
| export default SearchRouterWarmup; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| // The list renders at most MAX_AMOUNT_OF_SUGGESTIONS rows, so start from a small raw cap (100 leaves | ||
| // buffer for chats filtered out later) and let SearchAutocompleteList's loadAll effect expand it when | ||
| // needed. The batch stays large because createFilteredOptionList rebuilds its whole top-N slice each call. | ||
| const INITIAL_MAX_RECENT_REPORTS = 100; | ||
| const RECENT_REPORTS_BATCH_SIZE = 500; | ||
|
|
||
| // Shared by SearchAutocompleteList and SearchRouterOptionsWarmer: createFilteredOptionList keys its cache | ||
| // on these values, so the two call sites must not drift. Contacts can be deferred because the empty-query | ||
| // state shows none. | ||
| const SEARCH_ROUTER_OPTIONS_CONFIG = { | ||
| enabled: true, | ||
| includeP2P: true, | ||
| deferContactsUntilSearch: true, | ||
| maxRecentReports: INITIAL_MAX_RECENT_REPORTS, | ||
| batchSize: RECENT_REPORTS_BATCH_SIZE, | ||
| } as const; | ||
|
|
||
| export default SEARCH_ROUTER_OPTIONS_CONFIG; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import type ReactComponentModule from '@src/types/utils/ReactComponentModule'; | ||
|
|
||
| // Shared by AuthScreens (lazy screen registration) and SearchRouterWarmup (idle pre-evaluation). | ||
| const loadSearchRouterPage = () => require<ReactComponentModule>('../../../components/Search/SearchRouter/SearchRouterPage').default; | ||
| const loadRightModalNavigator = () => require<ReactComponentModule>('./Navigators/RightModalNavigator').default; | ||
|
|
||
| export {loadSearchRouterPage, loadRightModalNavigator}; |
Uh oh!
There was an error while loading. Please reload this page.