Skip to content

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

Open
mds-ant wants to merge 3 commits into
TanStack:mainfrom
mds-ant:perf/zero-alloc-calculate-range
Open

perf(virtual-core): drop closures from calculateRange on the lanes===1 hot path#1206
mds-ant wants to merge 3 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

🎯 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 by CodeRabbit

  • Bug Fixes

    • Improved virtual scrolling performance by reducing per-scroll allocations during continuous scrolling, with a notable benefit when using a single lane.
    • Clears virtualized range state when there are no measurements or when the outer size is zero.
  • Tests

    • Added scroll-loop benchmarks to measure performance across many rapid scroll frames for both small and large item counts.

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: 392dcccd-8c11-40a7-beec-71fa91c060b6

📥 Commits

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

📒 Files selected for processing (3)
  • .changeset/calculate-range-fewer-allocs.md
  • packages/virtual-core/src/index.ts
  • packages/virtual-core/tests/bench.bench.ts
✅ Files skipped from review due to trivial changes (1)
  • .changeset/calculate-range-fewer-allocs.md
🚧 Files skipped from review as they are similar to previous changes (2)
  • packages/virtual-core/tests/bench.bench.ts
  • packages/virtual-core/src/index.ts

📝 Walkthrough

Walkthrough

The PR replaces the per-scroll closure-based calculateRange helper in virtual-core with findNearestBinarySearchFlat (stride-based Float64Array binary search) and calculateRangeImpl, which dispatches to the typed-array fast path when lanes === 1. The Virtualizer's calculateRange memo gains explicit null early-return branches. Scroll-loop benchmarks and a patch changeset are also added.

Changes

Range Calculation Allocation Optimization

Layer / File(s) Summary
calculateRangeImpl and flat binary search
packages/virtual-core/src/index.ts
Introduces findNearestBinarySearchFlat for stride-based Float64Array binary search and calculateRangeImpl that dispatches to the typed-array hot path for lanes === 1 or falls back to the closure-based findNearestBinarySearch. Updates the Virtualizer's calculateRange memo to add explicit null early-return branches and pass this._flatMeasurements for the single-lane fast path.
Scroll-loop benchmarks and changeset
packages/virtual-core/tests/bench.bench.ts, .changeset/calculate-range-fewer-allocs.md
Adds a scroll-loop benchmark describe block measuring calculateRange and getVirtualItems over 10,000 scroll frames at item counts of 1000 and 100000. Adds the patch changeset entry documenting the removed per-scroll allocations.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested reviewers

  • tannerlinsley

Poem

🐇 Hop, hop, no heap to mop!
The binary search skips right along,
Float64Array sings a speedy song.
No closures born on every scroll,
GC rests — that's the goal!
Less alloc, more zoom — watch the frames roll! 🥕

🚥 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 accurately describes the main optimization: removing closures from calculateRange on the lanes===1 hot path, which is the primary focus of the changeset.
Description check ✅ Passed The description includes all required template sections: detailed Changes section explaining the motivation and implementation, completed Checklist items, and Release Impact with changeset generated.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 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/bench.bench.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/bench.bench.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

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

1 participant