Skip to content

fix: avoid re-rendering unchanged Table rows and cells - #10816

Open
pete-woods wants to merge 1 commit into
adobe:mainfrom
pete-woods:table-tuning
Open

pete-woods wants to merge 1 commit into
adobe:mainfrom
pete-woods:table-tuning

Conversation

@pete-woods

@pete-woods pete-woods commented Oct 20, 2026 •

Copy link
Copy Markdown

Closes #10477, closes #10759

Intent

When one row of a large RAC Table changes, the update should cost roughly that row, not the whole table. Today every Row and Cell re-runs its hooks (useTableRow, useTableCell, focus ring, hover, selection checkbox) on every table update. On a 300-row tree table that's 150–500 ms in a dev build, even when one row changed.

Rows and cells rendered in a 20-row table in a ResizableTableContainer (Jest):

Issue main This PR
#10477: clicking a cell 40 / 81 1 / 2
#10477: arrow key to the next cell 23 / 45 4 / 8
#10759: parent re-renders with a new width 40 / 80 0 / 0

It helps #8617 but doesn't fully fix it, because sorting and filtering replace most rows.

Why it happens

useCachedChildren already reuses elements for unchanged nodes, but Row and Cell consume TableStateContext. Its value is a new object whenever the collection, selection or expanded keys change, so React can't bail out. Inline <Column> elements also rebuild collection.columns on every parent render, so even a no-op parent render re-renders every row.

How this works

  • Memoised rows and cells. Row and Cell render their existing bodies inside a memo'd component, which only re-renders when a render key changes. The key is computed from what that row or cell displays:
    • Row: node, selected/disabled/can-select, focus, expanded, last child type, its cells, and the last sibling's index (for aria-setsize).
    • Cell: cell and row nodes, the row's selected/disabled/expanded state, and its own focus.
  • Table-wide key. Values that affect every row form a separate key, which changes only when one of them does: selection mode and behaviour, isEmpty, empty collection, column keys, row header keys, header row count, treeColumn, and whether onRowAction/onCellAction are set.
  • State for skipped rows. A row that skips rendering keeps its old event handlers, so they must not act on an old selection, collection or callback. useLatestTableState (new, in react-aria/private/table) gives each table state its own proxy, including the SelectionManager, keyboard delegate and grid actions. While rendering, the proxy reads that render's state. Once the render commits, a layout effect points it at a ref holding the latest committed state, which is what event handlers then use. Refs are only written in layout effects, never during render, and there are no lint suppressions.
  • DragAndDropContext and RowFocusContext values are now memoised.

I chose this over per-row selector subscriptions because it doesn't change the hooks or collection internals. Everything stays inside RAC Table.

Benchmarks

Treeble.perf.browser.test.tsx (50 parents × 5 children, dev build, Chromium):

Scenario Before After Rows rendered
Add 1 child row ~495 ms ~33 ms 613 → 13
Parent re-renders, nothing changed ~152 ms ~14 ms 614 → 0
Add 60 child rows ~255 ms ~85–110 ms 1154 → 306
Expand one row ~148 ms ~15 ms 600 → 6
Collapse one row ~145 ms ~13 ms 590 → 1

Adding 60 rows re-renders the whole group because each row's aria-setsize changes. Results from the #10787 harness, which uses production builds, are in the comments above.

Behaviour changes and risks

  • Render props: RowRenderProps.state is now the useLatestTableState proxy. A className or style function that reads table-wide state not covered by the render keys only updates when its own row re-renders. Anything reading TableStateContext directly is unchanged.
  • Maintenance: the render keys mirror what useTableRow, useGridRow, useSelectableItem and useGridCell read during render, so they need updating if those hooks read new state. TableRenderKeys.test.js catches most omissions.
  • Concurrent rendering: a render that never commits, such as a transition or Suspense render, can't change what committed rows see. One edge case remains: a memoised row that re-renders for another reason, such as a DragAndDropContext change, can read the committed state during render. That value is the same state it would render anyway.
  • Drag and drop: dragState and dropState are new every render, so drag-and-drop tables still re-render every row. There's no regression, but no gain either.
  • Not covered: GridList, Tree and ListBox use the same pattern and are unchanged.

✅ Pull Request Checklist:

  • Included link to corresponding React Spectrum GitHub Issue.
  • Added/updated unit tests and storybook for this change (for new code or code which already has tests).
  • Filled out test instructions.
  • Updated documentation (if it already exists for this component).
  • Looked at the Accessibility Practices for this feature - Aria Practices
  • I understand every change in this PR and can explain why it's there.
  • If AI-assisted, I followed our AI contribution guidance and pointed my assistant at AGENTS.md.

AI disclosure: the investigation, implementation, tests and benchmark were done with Claude Code (Claude Opus 5.5).

Adds a "Tree Table render counts" story. No docs changes. Rendered and ARIA output are unchanged. The only API change is private (react-aria/private/table/useLatestTableState).

📝 Test Instructions:

  • Automated:
    • Treeble.test.js, "Treeble row memoization": checks that unchanged rows don't re-render, that a skipped row uses the latest selection and onRowAction, and that a cell updates when only that cell changes.
    • TableRenderKeys.test.js: runs about 60 interactions against Table and against a copy with memo disabled, with and without drag and drop, and checks that the DOMs match after every step. It fails when render-key entries are removed. The exceptions are node-identity entries, which are currently redundant, and keyboardNavigationBehavior, which jsdom can't reach. It also fails if skipped rows don't move on to the committed state.
    • The last Treeble drag-and-drop test now presses Escape, because it used to leave a drag running for later tests.
  • Ran: after rebasing onto main, yarn lint and the table suites (RAC, stately, S2, v3) pass. Before the rebase, yarn test, yarn test:ssr and the benchmark also passed.
  • Benchmark: npx vitest run --config=vitest.browser.config.ts --project chromium-desktop packages/react-aria-components/test/Treeble.perf.browser.test.tsx
  • Manual: the "React Aria Components/Table/Tree Table render counts" story at http://localhost:9003/?path=/story/react-aria-components-table--tree-table-render-counts&providerSwitcher-express=false&strict=true flashes rows and cells in yellow when they render. Compare it with main, and toggle withDragAndDrop to see the drag-and-drop gap. In the Table tree stories, expand and collapse rows with the mouse and keyboard (including RTL), select with click and shift-click, and check that focus is restored after expanding.

🧢 Your Project:

CircleCI's web based UI.

@pete-woods
pete-woods force-pushed the table-tuning branch 3 times, most recently from 56133fd to ebcd3b2 Compare October 21, 2026 10:57
@pete-woods
pete-woods marked this pull request as ready for review October 21, 2026 11:08
@nwidynski

nwidynski commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

In your own words, how is this different from #10768?

@pete-woods

Copy link
Copy Markdown
Author

In your own words, how is this different from #10768?

To be completely honest I didn't spot that PR. I set "the machine" on this performance tuning task before I realised others had the same issue.

I'll have a look through to see how the implementations compare.

@pete-woods

pete-woods commented Oct 21, 2026 •

Copy link
Copy Markdown
Author

So the fix here is more general. But both changes I think make sense combined:

  • The other PR handles table resizing only - avoiding rebuilding when nothing changed at all. It doesn't help when e.g. the parent re-renders, or other state changes are made.
  • This PR additionally tries to avoid re-rendering as much as possible when the table state changes, e.g. trees expanded, by memoising rows and cells.

With the two PRs combined I think we'd see even fewer re-renders.

@pete-woods

pete-woods commented Oct 21, 2026 •

Copy link
Copy Markdown
Author

Getting opus to compare the performance of each. I'm also gonna look how the numbers compare if we combined both.

Edit: Below is Opus's analysis, which seems to confirm what I expected (they combine well).

Table render cost: main vs #10768 vs #10816 vs both combined

"Combined" is #10816 (e9ad1b54f) with #10768's branch merged in. The two PRs conflict in one place: both memoise the DragAndDropContext value in TableInner. I resolved it by keeping one copy, plus #10768's memoised DropIndicatorContext. On the combined code, formatting, lint and type-checks pass, and 62 suites pass: 1,771 tests passed, 6 skipped. Those suites are RAC, react-stately table, S2 TableView and v3 table, and they include both PRs' new tests.

Render counts (Jest)

Rows and cells rendered for each update, shown as rows / cells.

Scenario main #10768 #10816 Combined
Container resized (its own width state, #10759) 20 / 40 0 / 0 0 / 0 0 / 0
Parent re-renders with a new width 40 / 80 40 / 80 0 / 0 0 / 0
Parent re-renders, nothing changed 40 / 80 40 / 80 0 / 0 0 / 0
Click a cell (#10477) 42 / 84 42 / 84 23 / 41 23 / 41
Arrow key to next row (#10477) 23 / 45 23 / 45 4 / 8 4 / 8
Change one row's data 40 / 80 40 / 80 1 / 2 1 / 2
Tree: expand one row 100 / 100 100 / 100 5 / 5 5 / 5
Tree: add a child row 101 / 101 101 / 101 6 / 6 6 / 6

Setup:

  • Flat table: 20 rows, 2 columns, selectionMode="multiple", inside a ResizableTableContainer.
  • Tree table: 10 parents with 4 children each, 9 of the parents expanded.
  • Counting: each Row and Cell counts a render whenever its className render prop runs.
  • Environment: Jest with jsdom and the repo's test setup. That setup uses StrictMode, so some counts include double renders.
  • Container resize: clientWidth is mocked and a window resize event is fired, the same way Fix/table state identity #10768's tests do it.
  • "Click a cell" on fix: avoid re-rendering unchanged Table rows and cells #10816: the first selection flips isEmpty, which re-renders every row. A later click renders about 2 rows.

Source: /tmp/CompareRenders.test.js.

Tree table timings (Chromium)

From Treeble.perf.browser.test.tsx: 50 parents with 5 children each, 3 columns, selectionMode="multiple", dev build, timed with flushSync. Each cell gives the median over 5–7 rounds in two separate runs, as "run 1 / run 2".

Scenario main #10768 #10816 Combined Rows rendered (main → #10816)
Add 1 child row 473 / 501 ms 465 / 488 ms 32 / 30 ms 29 / 29 ms 613 → 13
Parent re-renders, nothing changed 146 / 159 ms 126 / 145 ms 30 / 12 ms 12 / 13 ms 614 → 0
Add 60 child rows 294 / 247 ms 262 / 243 ms 82 / 95 ms 98 / 100 ms 1154 → 306
Expand one row 144 / 142 ms 138 / 149 ms 15 / 13 ms 13 / 12 ms 600 → 6
Collapse one row 226 / 143 ms 138 / 141 ms 13 / 12 ms 11 / 12 ms 590 → 1

#10768 renders the same rows as main in this benchmark, and combined renders the same rows as #10816.

Container resize timings (Chromium)

A ResizableTableContainer with 300 rows and 4 columns. Each round changes the container's width through the DOM, so the real ResizeObserver fires. The figures are total React render time per resize from <Profiler> actualDuration, as the median of 9 resizes in each of two runs.

Version Render time per resize Cells rendered per resize
main 47.9 / 49.5 ms 1,200
#10768 0.7 / 1.1 ms 0
#10816 4.5 / 4.9 ms 0
Combined 0.9 / 0.7 ms 0

#10816 renders no cells here, but it still runs each row and cell's wrapper to compare render keys, which costs about 4 ms for 300 rows. #10768 avoids that because TableStateContext keeps its identity.

Source: /tmp/ResizeBench.browser.test.tsx.

Takeaways

@snowystinger

Copy link
Copy Markdown
Member

Were you running your own perf? It'd be good if we could run those combinations with our new PR @reidbarber has been working on to do benchmarking #10787

@pete-woods

Copy link
Copy Markdown
Author

Were you running your own perf? It'd be good if we could run those combinations with our new PR @reidbarber has been working on to do benchmarking #10787

Yes, I got Opus to make a performance harness for this. Let me look into using the one from your other PR.

@pete-woods

pete-woods commented Oct 22, 2026 •

Copy link
Copy Markdown
Author

Okay, I got it to performance profile the various combinations with the harness you mentioned. I had to get opus to add some more scenarios (which I can add in a PR if we move forward with this). It seems to back up my above findings.


Table performance with the #10787 harness: summary

I compared main, #10768, #10816 and the two combined, using the benchmark harness from #10787.

  • Builds: production builds in headless Chromium, with the harness defaults of 8 browser launches, 5 trials and 2 warmups.
  • Scenarios: existing chore: Performance benchmark harness #10787 scenarios, plus a new tree-table fixture (see below).

Scenarios added to the harness

The harness had no tree table. Its tree fixture uses the Tree component, not Table with treeColumn. Its flat-table fixtures don't cover an unrelated parent re-render or a change to one row's data either.

So I added a tree-table fixture. It has 50 workflows with 5 jobs each, a selection checkbox column and treeColumn="name". It isn't virtualised. Every workflow except the last starts expanded, so it has about 300 rows. Its eight actions:

Scenario Action
tree-table.mount Mount the table
tree-table.expand ArrowRight on the collapsed workflow
tree-table.collapse ArrowLeft on the first workflow
tree-table.focus ArrowDown from a workflow to its first job, with no selection
tree-table.select Click a job's checkbox
tree-table.parent Update state in the table's parent that the table doesn't use
tree-table.add Add a job to one workflow
tree-table.update Change one job's status

Each action waits for the expected end state before it counts as complete: the full row order, aria-expanded, focus, selection or text, as appropriate. Afterwards it checks every row's label and aria-level. The fixture passes yarn perf smoke, yarn test:perf and the type-check. It adds about 200 lines across four files: a new perf/app/tree-table.tsx, plus app.tsx, scenarios.mjs and drivers.mjs. I'm happy to contribute it to #10787.

All other rows below come from #10787's existing scenarios:

Row Scenario
Resize the container resize.container
Flat table: sort table-data.sort
Flat table: filter table-data.filter
Flat table: move cell focus table-data.focus
Flat table: close a cell menu table-cells.menu-close
Select a row (flat) table-cells.select

Results

Times are medians of the time from input to two frames after the expected DOM and focus state.

Scenario main #10768 #10816 Combined
Tree table: expand a row 361 ms 245 ms 21 ms 19 ms
Tree table: collapse a row 347 ms 241 ms 21 ms 17 ms
Tree table: move row focus 244 ms 121 ms 16 ms 15 ms
Tree table: unrelated parent update 228 ms 230 ms 15 ms 15 ms
Tree table: add a child row 234 ms 231 ms 21 ms 22 ms
Tree table: change one row's data 226 ms 234 ms 18 ms 18 ms
Resize the container (500 rows) 206 ms 31 ms 36 ms 32 ms
Flat table: sort 123 ms 120 ms 55 ms 55 ms
Flat table: filter 90 ms 53 ms 19 ms 18 ms
Flat table: move cell focus 81 ms 42 ms 15 ms 16 ms
Flat table: close a cell menu 130 ms 72 ms 24 ms 24 ms
Select a row (flat / tree) 66 / 116 ms 65 / 117 ms 71 / 128 ms 72 / 131 ms
Mount the tree table 158 ms 161 ms 171 ms 168 ms

Findings

  • fix: avoid re-rendering unchanged Table rows and cells #10816 makes every tree table interaction that changes state 92–96% faster. That covers expansion, focus, data changes and parent re-renders. It also makes flat-table sort, filter, focus and menu-close 57–82% faster, and resizing 83% faster.
  • Fix/table state identity #10768 is mainly a resize fix. It also makes focus, expand/collapse, filter and menu-close 30–50% faster, but doesn't help with parent re-renders or data changes.
  • Combined performs the same as fix: avoid re-rendering unchanged Table rows and cells #10816. Comparing the two directly, every scenario is within the harness's noise threshold. The biggest differences are on resizing (−6 ms) and expand/collapse (−3 to −4 ms).
  • Selection and mount are 5–8% (4–9 ms) slower with fix: avoid re-rendering unchanged Table rows and cells #10816, but the harness classes these as within threshold or inconclusive, not regressions. The first selection re-renders every row, and each row also pays the memo comparison. This needs confirming with yarn perf confirm.
  • No scenario showed a regression in any comparison.
let stateRef = useRef(state);
// Rows and cells render after this in the same pass and must see the new state, so the ref is
// updated during render. The layout effect restores the committed state if a render is discarded.
// oxlint-disable-next-line react/react-compiler, rsp-rules/pure-render

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

this one we can't disable unfortunately, it's caused some nasty bugs before, no reading or writing refs in render

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thanks, that makes sense. I've got opus to rework it so nothing reads or writes a ref during render, and the PR no longer adds any lint suppressions.

Opus:


useStableTableState is now useLatestTableState. Instead of one long-lived proxy kept up to date during render, each table state gets its own proxy:

  • While rendering, the proxy reads that render's own state. It's passed to Row and Cell as a prop that the memo comparison ignores.
  • Once that render commits, a layout effect points the proxy at a ref holding the latest committed state. A row that skipped later renders keeps its old proxy, so its event handlers act on the latest committed selection, collection and callbacks.

So the ref is only written in a layout effect and only read from event handlers. A render that never commits, such as a transition or Suspense render, can't leak into what other rows see, which the previous version could.

One edge case: a memoised row could re-render for another reason (say DragAndDropContext changes) while it still holds an already-committed proxy. It would then read the committed state during render. That's the same state it would otherwise render with, and a new table state always brings a new proxy, but I wanted to flag it.

The story had a separate suppression, for a React Compiler crash on a recursive renderItem function expression. I've moved that function to module scope instead.

@mfal

mfal commented Oct 26, 2026

Copy link
Copy Markdown

We tried this PR in a real app and wanted to share the numbers in case they help.

Setup: an internal support dashboard at mittwald that uses RAC Table through our design system: 100 rows × 11 columns, controlled selection (selectionMode="multiple", selectionBehavior="replace"). We ported this PR (head 0a9c605) as a pnpm patch onto react-aria-components@1.21.1 / react-aria@3.52.1. React 19.3, dev build, Chromium. Render time is the React Profiler's actualDuration, summed over all commits caused by one real mouse click on a row, median of 6 clicks. It includes re-renders our own app causes; the difference between the columns is this PR alone.

One click on a row before with #10816
render time 527 ms 181 ms
components rendered 28,000 15,100

Per commit:

  • Focus moving to the clicked row: ~75 ms → ~7 ms
  • The pass after the collection rebuild: ~75 ms → ~9 ms
  • First pointer click after typing in a search field (focus-visible modality switch): ~220 ms → ~30 ms
  • Hovering across 20 rows: unchanged at ~1–3 ms per row change, as expected, since hover state is row-local

What remains in the table is the wrapper layer: all ~1,200 row and cell wrappers still render on every table state change because they read TableStateContext, ~5–20 ms per change in our dev build. The memoized bodies skip as intended.

We only tested by hand (selecting rows, filtering the list) and saw no behavioural differences; we didn't run your test suite against the patch. These are dev-build numbers, so production values will be lower; the #10787 harness results above are the better reference there.

Measured and written up with Claude Code.

@pete-woods

Copy link
Copy Markdown
Author

Just rebased and resolved the conflicts from main

Row and Cell read TableStateContext, whose value changes whenever the
collection, selection or expanded keys change, so every row and cell
re-ran its hooks on any table update, even when the rendered elements
were cached.

Row and Cell now render their existing bodies inside a memoised
component that only re-renders when a render key, computed from the
parts of the table state the row or cell displays, changes. Each render
passes its own table state to the bodies (useLatestTableState). Once
that render commits, a layout effect points it at the latest committed
state, so event handlers in rows that skipped later renders still act on
the latest collection, selection and callbacks. Refs are only written in
layout effects, never during render. Table-wide values that affect every
row are compared through a key that changes only when one of them does.

Adds a test that runs the same interactions against Table and against a
copy with memoisation disabled and checks the DOM always matches, and a
"Tree Table render counts" story that flashes rows and cells as they
render.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@pete-woods

pete-woods commented Nov 16, 2026 •

Copy link
Copy Markdown
Author

Hi @nwidynski @snowystinger! I'm willing to invest more effort into this PR if you think something like this change, possibly re-structured somehow, would be merge-worthy?

I see others have tried it in their production apps with positive results.

@isihussain isihussain left a comment

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.

Code Review: Multi-Axis Evaluation

Incredible architectural design and performance engineering @pete-woods! Mitigating unnecessary row and cell re-renders in React Aria Table while cleanly preventing stale closures in skipped render trees is a monumental performance win for data-dense grids.


1. State Proxy Architecture & Stale Closure Prevention

  • Dynamic Forwarding Proxy: The implementation of useLatestTableState with createForwardingProxy is a masterclass in solving the React reconciliation vs event handler lifecycle tension:
    • During the render phase, properties resolve against the active render's renderState, preserving render purity and avoiding tear.
    • In post-commit layout effects (latest.commit(committedState)), the proxy pivots to read the latest committed TableState. This guarantees that click/keyboard/drag event handlers residing inside memoized, non-rendered rows and cells never act on a stale collection or outdated selection state.
  • Internal Maps Synchronization: Explicitly synchronizing gridMap (with proxied keyboardDelegate and actions) and gridIds preserves internal registration invariants in react-aria without monkey-patching core state structures.

2. Render Key Derivation & Memoization Boundaries

  • Deep Memoization Granularity: Using useDeepMemo with isRenderKeyEqual for table-wide keys (selectionMode, selectionBehavior, rowHeaderColumnKeys, action callbacks) isolates table-level mutations from localized row-level changes.
  • Context Decoupling: Introducing TableRowRenderContext breaks the tight context coupling that previously triggered whole-table re-renders whenever TableStateContext emitted new object references.

3. Test Coverage & Interaction Fidelity

  • The extensive test suite in packages/react-aria-components/test/Table.test.js provides rock-solid verification:
    • Verifying row/cell render count suppression during selection transitions.
    • Verifying external context updates propagate into memoized cells.
    • Verifying keyboard navigation and ARIA attributes remain 100% compliant and responsive.

Summary: World-class React performance optimization. Outstanding work!

@nwidynski

nwidynski commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

@pete-woods This is for the core team to decide. If it were my decision, I would not consider it in its current state. While I appreciate the goal to improve Table performance, I fear that with the current LLM-based approach, we are making little to no progress in understanding the issue space.

React re-renders are not a problem per-se. Hill-climbing an LLM to avoid them, without any understanding of what's actually going on, makes it harder for the React compiler to do its job. This is a problem in the long run.

PS: I think the benchmarks sufficiently prove both that there is a problem and room for improvement, so that's generally positive.

@pete-woods

pete-woods commented Nov 16, 2026 •

Copy link
Copy Markdown
Author

For what it's worth when I asked the LLM about other alternatives, it agrees with you and suggests the real fix is per-row subscriptions, which is more in-line with the rest of the code, but it would affect a lot more of the codebase.

However my initial agent rejected this approach, because iI got it to optimise for PR diff/reviewability, not really expecting it to be so big as this in the end.

I can break out the (hopefully) non-controversial parts:

Per-row subscriptions, the option the PR rejected. This is the idiomatic fix, and the codebase already leans that way: CollectionBuilder.tsx and Document.ts already use useSyncExternalStore for the collection. If selection, focus and expansion lived in a store, each row could subscribe to isSelected(key), isFocused(key) and isExpanded(key):

  • React would bail out on its own, with no render keys and no proxies.
  • Event handlers would call stable store methods, so stale closures stop being a problem.
  • Plain values are easy for the compiler to reason about.
  • The same change would also help GridList, Tree and ListBox.

The cost is that it's cross-cutting, touching react-stately's selection state and the shared hooks. It's also a design call for the core team. useSyncExternalStore makes updates synchronous, which affects transitions, and that trade-off needs their input.

@nwidynski

Copy link
Copy Markdown
Contributor

Yeah, let's try to avoid outsourcing the thinking part. In my opinion, that is still asking the wrong question. Memoization is a cache. Which operation(s) of Table are slow enough for a cache to make sense? We shouldn't be considering a cache before we can pinpoint what we are actually caching.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

5 participants