Skip to content

perf(virtual-core): drop closures from calculateRange on the lanes===1 hot path#1206

Merged
piecyk merged 4 commits into
TanStack:mainfrom
mds-ant:perf/zero-alloc-calculate-range
Jun 30, 2026
Merged

perf(virtual-core): drop closures from calculateRange on the lanes===1 hot path#1206
piecyk merged 4 commits into
TanStack:mainfrom
mds-ant:perf/zero-alloc-calculate-range

Conversation

@mds-ant

@mds-ant mds-ant commented Jun 22, 2026

Copy link
Copy Markdown
Contributor

🎯 Changes

Cut per-scroll-frame allocations on the default lanes === 1 path. Range computation previously allocated an options object and two closures on every scroll event; it now does the same work allocation-free, reducing GC pressure during continuous scroll.

  • calculateRangeImpl takes positional args instead of an options object.
  • New findNearestBinarySearchFlat(flat, high, value) — monomorphic typed-array binary search that indexes flat[i*2] directly. The closure-based findNearestBinarySearch is kept for the lanes > 1 / no-flat fallback.
  • The lanes === 1 forward-walk reads start + size from the typed array inline.

Motivation

scrollOffset is a memo dep on calculateRange, so its body runs on every scroll event. On main each run …

  • allocates a 5-field {measurements, outerSize, scrollOffset, lanes, flat} options object,
  • allocates two getStart / getEnd closures, and
  • pays a polymorphic indirect call through those closures for each binary-search probe.

The codebase already has a Float64Array fast path for lanes === 1 (_flatMeasurements). This change reads from it directly instead of through closures.

Performance

Benchmark main (Hz) This PR (Hz) Δ
n=1000, 10k scrolls 104.7 130.7 +25%
n=100000, 10k scrolls 87.7 109.1 +24%
n=1000, 10k scrolls + getVirtualItems 34.0 37.1 +9%
n=100000, 10k scrolls + getVirtualItems 32.5 35.3 +9%

✅ 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).

This PR was assisted by Claude Code.

Summary by CodeRabbit

Summary of Changes

  • Bug Fixes

    • Clears virtualized range state when there are no measurements or when the outer container size is zero.
  • Performance Improvements

    • Reduced per-scroll allocations during continuous scrolling, with the biggest gains when running with a single lane.
  • Tests

    • Added a new scroll-loop benchmark suite that simulates thousands of rapid scroll frames to compare range-only vs. full virtual item updates.

Exercises `calculateRange` and the memo machinery the way a real
scroll does: bump `scrollOffset`, recompute range. None of the
existing benches covered the per-scroll-frame path.
@coderabbitai

coderabbitai Bot commented Jun 22, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: e627912c-1533-4039-b349-7d9348d5d383

📥 Commits

Reviewing files that changed from the base of the PR and between 7b5413e and cfd5a83.

📒 Files selected for processing (1)
  • packages/virtual-core/src/index.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/virtual-core/src/index.ts

📝 Walkthrough

Walkthrough

The PR replaces the per-scroll closure-based calculateRange helper in virtual-core with findNearestBinarySearchFlat and calculateRangeImpl, adds explicit null early-return branches in Virtualizer.calculateRange, and adds scroll-loop benchmarks plus a patch changeset.

Changes

Range Calculation Allocation Optimization

Layer / File(s) Summary
calculateRangeImpl and flat binary search
packages/virtual-core/src/index.ts
Introduces findNearestBinarySearchFlat and calculateRangeImpl, with a typed-array fast path for lanes === 1 and a closure-based fallback for other cases. Updates Virtualizer.calculateRange to return null when there are no measurements or outerSize is 0, and to pass flat measurements only on the single-lane path.
Scroll-loop benchmarks and changeset
packages/virtual-core/tests/bench.bench.ts, .changeset/calculate-range-fewer-allocs.md
Adds a scroll-loop benchmark suite that runs 10,000 simulated frames for 1000 and 100000 items, measuring calculateRange and getVirtualItems. Adds a patch changeset entry documenting the optimization.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested reviewers

  • tannerlinsley

Poem

🐇 Hop, hop, the scroll goes light,
Flat arrays dart through the night.
No extra closures on the run,
Fewer allocs, more zooming fun.
Tiny paws applaud the faster tune 🥕

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 50.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly summarizes the main optimization to calculateRange on the lanes===1 hot path.
Description check ✅ Passed The description matches the template and includes changes, motivation, performance, checklist, and release impact.
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.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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

Comment @coderabbitai help to get the list of available commands.

…t path

`scrollOffset` is a memo dep, so the body runs on every scroll event.
Before: a 5-field options object, two `getStart`/`getEnd` closures,
and polymorphic indirect calls during binary search.

After: positional args; a monomorphic `findNearestBinarySearchFlat`
that indexes the `Float64Array` directly.
@mds-ant mds-ant force-pushed the perf/zero-alloc-calculate-range branch from 0c0cf35 to bc4d81e Compare June 22, 2026 08:46
@mds-ant mds-ant marked this pull request as ready for review June 22, 2026 08:47

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
packages/virtual-core/tests/bench.bench.ts (1)

210-212: 🧹 Nitpick | 🔵 Trivial | ⚡ Quick win

Keep benchmark offsets within realistic scroll bounds.

For n=1000, i * 5 overshoots max scroll offset for a large tail of iterations, so the loop benchmarks a clamped end-state repeatedly. Consider wrapping/clamping against the computed max offset to keep frame samples representative across sizes.

♻️ Suggested tweak
     bench(`n=${n}, 10k scrolls`, () => {
       const v = makeVirt(n)
       v.scrollRect = { width: 800, height: 600 }
+      const maxOffset = Math.max(v.getTotalSize() - v.scrollRect.height, 0)
       for (let i = 0; i < 10_000; i++) {
-        v.scrollOffset = i * 5
+        v.scrollOffset = maxOffset === 0 ? 0 : (i * 5) % maxOffset
         ;(v as any).calculateRange()
       }
     })
     bench(`n=${n}, 10k scrolls + getVirtualItems`, () => {
       const v = makeVirt(n)
       v.scrollRect = { width: 800, height: 600 }
+      const maxOffset = Math.max(v.getTotalSize() - v.scrollRect.height, 0)
       for (let i = 0; i < 10_000; i++) {
-        v.scrollOffset = i * 5
+        v.scrollOffset = maxOffset === 0 ? 0 : (i * 5) % maxOffset
         v.getVirtualItems()
       }
     })

Also applies to: 218-220

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@packages/virtual-core/tests/bench.bench.ts` around lines 210 - 212, The
benchmark loop setting v.scrollOffset = i * 5 allows offset values to exceed the
realistic maximum scroll offset, causing many iterations to repeatedly benchmark
a clamped end-state instead of representative scroll positions. Fix this by
computing the maximum scroll offset for the component and wrapping or clamping
the offset calculation (i * 5) against that max value within the loop before
calling calculateRange(). Apply this same wrapping/clamping approach to all
similar loops in the benchmark file where scrollOffset is being set
incrementally.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Nitpick comments:
In `@packages/virtual-core/tests/bench.bench.ts`:
- Around line 210-212: The benchmark loop setting v.scrollOffset = i * 5 allows
offset values to exceed the realistic maximum scroll offset, causing many
iterations to repeatedly benchmark a clamped end-state instead of representative
scroll positions. Fix this by computing the maximum scroll offset for the
component and wrapping or clamping the offset calculation (i * 5) against that
max value within the loop before calling calculateRange(). Apply this same
wrapping/clamping approach to all similar loops in the benchmark file where
scrollOffset is being set incrementally.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: d9742e3e-ac59-4457-b264-0f55f8a564dc

📥 Commits

Reviewing files that changed from the base of the PR and between 75ae896 and bc4d81e.

📒 Files selected for processing (3)
  • .changeset/calculate-range-fewer-allocs.md
  • packages/virtual-core/src/index.ts
  • packages/virtual-core/tests/bench.bench.ts

@mds-ant mds-ant force-pushed the perf/zero-alloc-calculate-range branch from bc4d81e to a677064 Compare June 23, 2026 07:59
For `n=1000` (30k px content, 600 px viewport), `i * 5` reached
~50k — past the ~29.4k max scroll — so a large share of iterations
benched the degenerate end-of-list range instead of a representative
position. Wrap with `% maxOffset` so every iteration does real
binary-search and forward-walk work.
@mds-ant mds-ant force-pushed the perf/zero-alloc-calculate-range branch from a677064 to 7b5413e Compare June 23, 2026 08:10
@pkg-pr-new

pkg-pr-new Bot commented Jun 30, 2026

Copy link
Copy Markdown
More templates

@tanstack/angular-virtual

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

@tanstack/lit-virtual

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

@tanstack/react-virtual

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

@tanstack/solid-virtual

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

@tanstack/svelte-virtual

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

@tanstack/virtual-core

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

@tanstack/vue-virtual

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

commit: cfd5a83

@piecyk piecyk merged commit 767ead4 into TanStack:main Jun 30, 2026
10 checks passed
@piecyk

piecyk commented Jun 30, 2026

Copy link
Copy Markdown
Collaborator

Thanks @mds-ant 👏

@github-actions github-actions Bot mentioned this pull request Jun 30, 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