-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
perf(router-core): publish fewer store updates during navigation #8416
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
Open
schiller-manuel
wants to merge
1
commit into
main
Choose a base branch
from
codex/fewer-store-updates
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+648
−131
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@tanstack/router-core': patch | ||
| --- | ||
|
|
||
| Publish fewer router store updates during a navigation. Every synchronous frame between two `beforeLoad` awaits now publishes once, so ending one `beforeLoad` and starting the next hook or the loaders no longer produces separate `isFetching` updates. Loader starts for a whole lane publish together, and a superseding navigation clears the previous lane's fetching state in the same update that publishes its new location. `waitFor` no longer registers an abort listener for plain values. |
106 changes: 106 additions & 0 deletions
106
packages/react-router/tests/store-updates-during-navigation.bench.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| import { bench, describe, expect } from 'vitest' | ||
| import { render } from '@testing-library/react' | ||
| import { | ||
| Outlet, | ||
| RouterProvider, | ||
| createMemoryHistory, | ||
| createRootRoute, | ||
| createRoute, | ||
| createRouter, | ||
| useParams, | ||
| useRouterState, | ||
| useSearch, | ||
| } from '../src' | ||
|
|
||
| // Navigation cost under store-subscriber fan-out. A retained layout mounts | ||
| // `subscribers` broad `useRouterState` consumers plus as many narrow | ||
| // `useSearch`/`useParams` consumers. Every navigation re-runs two retained | ||
| // synchronous `beforeLoad`s and a stale layout loader, so the router publishes | ||
| // fetching transitions on presented matches. All selections are stable, so the | ||
| // measured work is store propagation and selection rather than re-rendering. | ||
| async function setup(subscribers: number) { | ||
| let notifications = 0 | ||
|
|
||
| const Broad = () => { | ||
| useRouterState({ select: (state) => state.location.pathname }) | ||
| return null | ||
| } | ||
| const Narrow = () => { | ||
| useSearch({ strict: false }) | ||
| useParams({ strict: false }) | ||
| return null | ||
| } | ||
| const Counter = () => { | ||
| useRouterState({ | ||
| select: () => { | ||
| notifications++ | ||
| }, | ||
| }) | ||
| return null | ||
| } | ||
|
|
||
| const rootRoute = createRootRoute({ | ||
| beforeLoad: () => ({ root: true }), | ||
| component: () => ( | ||
| <> | ||
| <Counter /> | ||
| <Outlet /> | ||
| </> | ||
| ), | ||
| }) | ||
| const layoutRoute = createRoute({ | ||
| getParentRoute: () => rootRoute, | ||
| id: 'layout', | ||
| beforeLoad: () => ({ layout: true }), | ||
| loader: () => ({ items: [] }), | ||
| staleTime: 0, | ||
| component: () => ( | ||
| <> | ||
| {Array.from({ length: subscribers }, (_, index) => ( | ||
| <Broad key={index} /> | ||
| ))} | ||
| {Array.from({ length: subscribers }, (_, index) => ( | ||
| <Narrow key={index} /> | ||
| ))} | ||
| <Outlet /> | ||
| </> | ||
| ), | ||
| }) | ||
| const pages = ['a', 'b'].map((path) => | ||
| createRoute({ | ||
| getParentRoute: () => layoutRoute, | ||
| path, | ||
| beforeLoad: () => ({ page: path }), | ||
| loader: () => path, | ||
| component: () => <p>{path}</p>, | ||
| }), | ||
| ) | ||
| const router = createRouter({ | ||
| routeTree: rootRoute.addChildren([layoutRoute.addChildren(pages)]), | ||
| history: createMemoryHistory({ initialEntries: ['/a'] }), | ||
| }) | ||
| render(<RouterProvider router={router} />) | ||
| await router.load() | ||
|
|
||
| const lap = async () => { | ||
| await router.navigate({ to: '/b', replace: true }) | ||
| await router.navigate({ to: '/a', replace: true }) | ||
| } | ||
| await lap() | ||
| const before = notifications | ||
| await lap() | ||
| const perNavigation = (notifications - before) / 2 | ||
| expect(router.state.location.pathname).toBe('/a') | ||
| expect(perNavigation).toBeGreaterThan(0) | ||
| return { lap, perNavigation } | ||
| } | ||
|
|
||
| for (const subscribers of [0, 50, 200]) { | ||
| const { lap, perNavigation } = await setup(subscribers) | ||
| describe(`${subscribers} broad + ${subscribers} narrow subscribers (${perNavigation} store updates per navigation)`, () => { | ||
| bench('two navigations with retained beforeLoads and a stale loader', lap, { | ||
| time: 1000, | ||
| warmupTime: 200, | ||
| }) | ||
| }) | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀 Performance & Scalability | 🟡 Minor | ⚡ Quick win
Keep
Broadselections stable.state.location.pathnamechanges on every/a↔/bnavigation. EachBroadconsumer can re-render. This contradicts the benchmark contract in lines 19-20 and mixes render work into the measured store propagation cost. Select a stable value if the benchmark must isolate notification and selection work.Proposed fix
📝 Committable suggestion
🤖 Prompt for AI Agents