Skip to content

Fix slow updates after selecting all the rows - #4239

Merged
nstepien merged 4 commits into
mainfrom
memoize-header-row-selection
Oct 26, 2026
Merged

nstepien merged 4 commits into
mainfrom
memoize-header-row-selection

Conversation

@nstepien

@nstepien nstepien commented Oct 26, 2026 •

Copy link
Copy Markdown
Collaborator

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 call rowKeyGetter for 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 in useMemo, 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, DataGrid isn't compiled, and useMemo caches the loop:

const headerSelectionValue = useMemo(() => {
  let hasSelectedRow = false;
  let hasUnselectedRow = false;
  if (rowKeyGetter != null && selectedRows != null && selectedRows.size > 0)
    for (const row of rows) {
      // …
    }
  return {
    isRowSelected: hasSelectedRow && !hasUnselectedRow,
    isIndeterminate: hasSelectedRow && hasUnselectedRow
  };
}, [rows, selectedRows, rowKeyGetter]);

In the published 7.0.0-beta.63, the loop is outside of any memo block:

let hasSelectedRow = false;
let hasUnselectedRow = false;
if (rowKeyGetter != null && selectedRows != null && selectedRows.size > 0)
  for (const row of rows) {
    // …
  }
const t8 = hasSelectedRow && !hasUnselectedRow;
const t9 = hasSelectedRow && hasUnselectedRow;
let t10;
if ($[28] !== t8 || $[29] !== t9) {
  t10 = { isRowSelected: t8, isIndeterminate: t9 };
  // …
}

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 useMemo calls in src are memoized: their loops create objects, so the loops stay in the memo blocks.

Changes

  • The loop moves out of useMemo, to the top level of DataGrid, where the compiler memoizes it on rows, selectedRows, and rowKeyGetter:
    let hasSelectedRow;
    let hasUnselectedRow;
    if ($[28] !== rowKeyGetter || $[29] !== rows || $[30] !== selectedRows) {
      hasSelectedRow = false;
      hasUnselectedRow = false;
      // …
      $[31] = hasSelectedRow;
      $[32] = hasUnselectedRow;
    } else {
      hasSelectedRow = $[31];
      hasUnselectedRow = $[32];
    }
  • A comment says to keep the loop out of useMemo.
  • useMemo only 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.
  • A test selects all the rows of a grid with 1,000 rows, moves the active cell, and checks that rowKeyGetter was called for fewer rows than the grid has. It fails on main in all three browsers.

Performance

Mean wall-clock time per update in Chromium with React's production build, for a DataGrid with 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:

Update 7.0.0-beta.62 7.0.0-beta.63 main This PR
Scroll down by one row 0.39 ms 2.5 ms 1.6 ms 0.33 ms
Move the active cell with an arrow key 0.35 ms 2.6 ms 1.6 ms 0.36 ms

Until #4242, DataGrid rendered twice per update. With 7.0.0-beta.63, each of these updates calls rowKeyGetter about 200,000 times, twice per row, and on main, 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 TreeDataGrid with 1,000,000 rows, all of them selected, a scroll by one row takes 36 ms on main, and 0.28 ms with this PR.

vp run ci passes.

🤖 Generated with Claude Code

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]>
@nstepien
nstepien added this pull request to stack #4241 October 26, 2026 01:09
@codecov-commenter

codecov-commenter commented Oct 26, 2026 •

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.13%. Comparing base (ac276f9) to head (179ae2f).
⚠️ Report is 1 commits behind head on main.

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           
Files with missing lines Coverage Δ
src/DataGrid.tsx 99.06% <100.00%> (+<0.01%) ⬆️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@nstepien nstepien changed the title Memoize the state of the select-all checkbox Fix slow updates after selecting all the rows 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]>
nstepien and others added 3 commits October 26, 2026 21:30
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]>
@nstepien
nstepien marked this pull request as ready for review October 26, 2026 23:10
@nstepien
nstepien merged commit fd0cc21 into main Oct 26, 2026
5 checks passed
@nstepien
nstepien deleted the memoize-header-row-selection branch October 26, 2026 23:19

@amanmahajan7 amanmahajan7 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Possible to add benchmark tests?

@nstepien

Copy link
Copy Markdown
Collaborator Author

Yes, but I'm not sure they'd be valuable, the results would vary too much from environment to environment

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.

3 participants