Skip to content

perf(virtual-core): rebuild the layout once per resize batch - #1393

Merged
piecyk merged 1 commit into
TanStack:mainfrom
tunardev:perf/resize-batch-start-offset
Nov 22, 2026
Merged

piecyk merged 1 commit into
TanStack:mainfrom
tunardev:perf/resize-batch-start-offset

Conversation

@tunardev

@tunardev tunardev commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

🎯 Changes

Summary

Dragging the scrollbar on a large list with measured rows drops frames (#1072). In one ResizeObserver batch, after an entry adjusts scroll, resizeItem rebuilds the whole layout for every later entry. With 100k rows that is about 13 full rebuilds per frame. This PR computes each later entry's start from the size changes since the last build, so the layout is rebuilt once.

Benchmarks

Chromium (Playwright), 100k rows with real text of different heights, default measureElement. Each frame sets scrollTop to a random spot, like dragging the scrollbar. 120 frames, same result in 2 runs:

CPU before after
1x 50 fps, 25 dropped frames 60 fps, 0 dropped
4x slowdown 16 fps, p95 frame 183ms 60 fps, 0 dropped

What changed

  • resizeItem keeps the size changes made since the last layout build.
  • When the batch has adjusted scroll, it adds the earlier changes to the item's start instead of calling getMeasurements().
  • It still rebuilds when that would not be exact: lanes > 1, a custom shouldAdjustScrollPositionOnItemSizeChange, or measurement options or the size cache changed another way.

Correctness

  • After the drag in Chromium, before and after have the same measured rows and total size, and no rows out of place.
  • In a headless run of 4 scroll scenarios at 10k and 100k rows, scroll offset, total size and every virtual item's start and size are the same as before in every frame.
  • New test: a batch whose first entry adjusts scroll keeps the later entries anchored, and calls getItemKey about 1k times instead of 10.5k.

✅ Checklist

  • I have followed the steps in the Contributing guide.
  • I have tested this code locally with pnpm run test:pr.

🚀 Release Impact

  • This change affects published code, and I have generated a changeset.
  • This change is docs/CI/dev-only (no release).

@changeset-bot

changeset-bot Bot commented Nov 22, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: cc64aef

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 9 packages
Name Type
@tanstack/virtual-core Patch
@tanstack/angular-virtual Patch
@tanstack/lit-virtual Patch
@tanstack/marko-virtual Patch
@tanstack/react-virtual Patch
@tanstack/solid-virtual Patch
@tanstack/svelte-virtual Patch
@tanstack/vue-virtual Patch
@tanstack/virtual-benchmarks Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai

coderabbitai Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The virtualizer tracks measurement-build inputs and pending resize deltas. For eligible synchronous resize batches, it adjusts item starts using earlier deltas instead of rebuilding measurements. The added test checks the resulting scroll offset, item position, and key-call count.

Changes: Resize batch measurement updates

Layer / File(s) Summary
Track measurement build state
packages/virtual-core/src/index.ts
The virtualizer records measurement options and the item-size cache version used for a build. Each new build clears pending resize deltas.
Apply resize batch deltas
packages/virtual-core/src/index.ts, packages/virtual-core/tests/adjustment.test.ts, .changeset/resize-batch-rebuild-once.md
When the fast-path conditions hold, resizeItem uses earlier resize deltas to calculate item starts. Otherwise, it rebuilds measurements. The test checks the scroll offset, item 40’s position, and getItemKey call count. The changeset marks a patch release and describes the resize-batch change.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Refactor

Possibly related PRs

  • TanStack/virtual#1381: Both changes rely on item-size cache invalidation and rebuilding measurements when a cached size is removed.

Suggested reviewers: piecyk, tannerlinsley

Merge Risk: Low · up to cc64a

This change speeds up scrollbar dragging on large lists with measured rows by rebuilding layout once per resize batch. The main behavior is preserved and covered by a new test. One rare edge case remains. With fractional row sizes, an extra scroll correction of a few pixels could occasionally be applied. This is a minor visual glitch with no data impact, so merging is low risk.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: the layout is rebuilt once per resize batch instead of once per entry, in virtual-core. It matches the summary, objectives, and benchmarks. The perf() prefix is a conv…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description check Passed The description covers the change, its motivation, benchmarks, correctness checks, and a new test. It follows the template's sections. Both the checklist and the release-impact items are ticked, which…
Full details: Title check

Explanation

The title names the main change: the layout is rebuilt once per resize batch instead of once per entry, in virtual-core. It matches the summary, objectives, and benchmarks. The perf() prefix is a conventional type and does not reduce clarity.

Full details: Description check

Explanation

The description covers the change, its motivation, benchmarks, correctness checks, and a new test. It follows the template's sections. Both the checklist and the release-impact items are ticked, which is consistent with the changeset in the summary. The description does not list the changed files, but the template does not require that. The description is mostly complete.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

packages/virtual-core/tests/adjustment.test.ts

Parsing error: "parserOptions.project" has been provided for @typescript-eslint/parser.
The file was not found in any of the provided project(s): packages/virtual-core/tests/adjustment.test.ts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: db7836ad-f7e1-4c2e-8045-343dfa6153bb
Commits

Reviewing files that changed from the base of the PR and between f3db69c and cc64aef.

Files selected for processing: 3

  • .changeset/resize-batch-rebuild-once.md
  • packages/virtual-core/src/index.ts
  • packages/virtual-core/tests/adjustment.test.ts

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@nx-cloud

nx-cloud Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

View your CI Pipeline Execution ↗ for commit cc64aef

Command Status Duration Result
nx affected --targets=test:sherif,test:knip,tes... ✅ Succeeded 4m 30s View ↗
nx run-many --target=build --exclude=examples/** ✅ Succeeded 15s View ↗

💡 With Ultracache enabled, Nx Cloud learns each task's file inputs in CI and uses them on later runs. Turn on Ultracache in workspace settings ↗ · Read docs ↗


☁️ Nx Cloud last updated this comment at 2026-11-22 17:32:16 UTC

@pkg-pr-new

pkg-pr-new Bot commented Nov 22, 2026

Copy link
Copy Markdown
More templates

@tanstack/angular-virtual

npm i https://pkg.pr.new/@tanstack/angular-virtual@1393

@tanstack/lit-virtual

npm i https://pkg.pr.new/@tanstack/lit-virtual@1393

@tanstack/marko-virtual

npm i https://pkg.pr.new/@tanstack/marko-virtual@1393

@tanstack/react-virtual

npm i https://pkg.pr.new/@tanstack/react-virtual@1393

@tanstack/solid-virtual

npm i https://pkg.pr.new/@tanstack/solid-virtual@1393

@tanstack/svelte-virtual

npm i https://pkg.pr.new/@tanstack/svelte-virtual@1393

@tanstack/virtual-core

npm i https://pkg.pr.new/@tanstack/virtual-core@1393

@tanstack/vue-virtual

npm i https://pkg.pr.new/@tanstack/vue-virtual@1393

commit: cc64aef

@piecyk piecyk 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.

Thanks @tunardev, nice one!

@piecyk
piecyk merged commit c8ec72d into TanStack:main Nov 22, 2026
10 checks passed
@github-actions github-actions Bot mentioned this pull request Nov 22, 2026
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.

2 participants