Repository navigation
Fix slow updates after selecting all the rows - #4239
Merged
Merged
Conversation
React Compiler didn't memoize the loop that computes the state of the select-all checkbox, although it was in `useMemo`: the loop only produces booleans, so the compiler only cached the object that holds them. Every render of `DataGrid` checked the selection of every row, until it found both a selected and an unselected row. With all the rows selected, every scroll and active cell change called `rowKeyGetter` for each row. The loop now runs in `getHeaderRowSelection`, which returns the object, so the compiler memoizes the call. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4239 +/- ##
=======================================
Coverage 99.13% 99.13%
=======================================
Files 46 46
Lines 2194 2195 +1
Branches 967 968 +1
=======================================
+ Hits 2175 2176 +1
Misses 19 19
🚀 New features to boost your workflow:
|
This was referenced Oct 26, 2026
nstepien
added a commit
that referenced
this pull request
Oct 26, 2026
`useColumnWidths` sets `prevGridWidth` in a layout effect that runs after every render, even when the width hasn't changed. React doesn't skip a state update scheduled right after a commit, so every update of `DataGrid`, e.g., a scroll or an active cell change, renders it a second time. React's Profiler reports the second render as a `nested-update` commit. ### Changes - `useColumnWidths` only sets `prevGridWidth` when it differs from `gridWidth`. - A test wraps a grid in `<Profiler>` and checks that moving the active cell commits once. It fails on `main` in all three browsers. ### Performance React renders `DataGrid` again, but its memoized rows and cells don't re-render, so the second render is cheap. Mean wall-clock time per update in Chromium with React's production build, for a `DataGrid` with 10,000 rows and 41 columns, with no rows selected. Each value is the mean of 2 runs of 300 updates, alternating between the branches: | Update | `main` | This PR | | -------------------------------------- | ------: | ------: | | Scroll down by one row | 0.37 ms | 0.37 ms | | Scroll right by one column | 0.76 ms | 0.77 ms | | Move the active cell with an arrow key | 0.37 ms | 0.33 ms | The second render also repeated the work in `DataGrid` that isn't memoized. On `main`, that includes the state of the select-all checkbox, which #4239 memoizes: with 100,000 rows, all of them selected, every update called `rowKeyGetter` twice per row. `vp run ci` passes. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
React Compiler memoizes the loop at the top level of the component, on `rows`, `selectedRows`, and `rowKeyGetter`, so `getHeaderRowSelection` isn't needed. The state is memoized on its booleans, so it keeps its identity while it doesn't change. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
amanmahajan7
left a comment
Collaborator
There was a problem hiding this comment.
Possible to add benchmark tests?
Collaborator
Author
|
Yes, but I'm not sure they'd be valuable, the results would vary too much from environment to environment |
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Since 7.0.0-beta.63, every render of
DataGrid, e.g., for a scroll, an active cell change, or a keystroke in an editor, can callrowKeyGetterfor every row to compute the state of the select-all checkbox. It happens when the first unselected row is far down, e.g., after selecting all the rows. With 100,000 rows, a scroll by one row takes 2.5 ms instead of 0.4 ms in 7.0.0-beta.62, and the time grows with the number of rows.Cause
#4162 made React Compiler compile
DataGrid, which it skipped before. The loop that computes the state of the select-all checkbox is inuseMemo, but it only produces booleans, so the compiler caches the object that holds them, keyed on the booleans, and runs the loop on every render. The compiler doesn't report it.In the published 7.0.0-beta.62,
DataGridisn't compiled, anduseMemocaches the loop:In the published 7.0.0-beta.63, the loop is outside of any memo block:
The loop stops once it has found both a selected and an unselected row, which is why the benchmarks of #4162, with at most one selected row, didn't show it. The other
useMemocalls insrcare memoized: their loops create objects, so the loops stay in the memo blocks.Changes
useMemo, to the top level ofDataGrid, where the compiler memoizes it onrows,selectedRows, androwKeyGetter:useMemo.useMemoonly creates the object, from the two booleans, so it keeps its identity while the state of the select-all checkbox doesn't change. Selecting another row while the checkbox stays indeterminate no longer renders it again.rowKeyGetterwas called for fewer rows than the grid has. It fails onmainin all three browsers.Performance
Mean wall-clock time per update in Chromium with React's production build, for a
DataGridwith 100,000 rows and 11 columns, all of them selected. Each value is the mean of 2 runs of 200 updates, alternating between the four builds:mainUntil #4242,
DataGridrendered twice per update. With 7.0.0-beta.63, each of these updates callsrowKeyGetterabout 200,000 times, twice per row, and onmain, about 100,000 times. With this PR, it calls it 21–25 times, for the rendered rows, and twice as often with 7.0.0-beta.62.In a
TreeDataGridwith 1,000,000 rows, all of them selected, a scroll by one row takes 36 ms onmain, and 0.28 ms with this PR.vp run cipasses.🤖 Generated with Claude Code