Skip to content

perf: faster virtualized item measurement - #10886

Open
nwidynski wants to merge 4 commits into
adobe:mainfrom
nwidynski:perf/virtualized-item-measurement
Open

nwidynski wants to merge 4 commits into
adobe:mainfrom
nwidynski:perf/virtualized-item-measurement

Conversation

@nwidynski

Copy link
Copy Markdown
Contributor

Closes #10858

Each item with an estimated size forced two style and layout passes, one for isElementVisible after the previous item restored its style.height and one for its own getSize after clearing it, so useVirtualizer now clears all heights, reads all sizes and restores them in a single pass per commit.

@github-actions github-actions Bot added the RAC label Oct 28, 2026
}

let sizes = nodes.map(node =>
isElementVisible(node) ? new Size(node.scrollWidth, node.scrollHeight) : null

@nwidynski nwidynski Oct 28, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Perhaps we could also just check visibility once for the collection, now that the initial measuring is hoisted.

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.

I think this would mean we are checking every element on every render rather than only the ones with estimated sizes

@nwidynski nwidynski Nov 14, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure I can follow? I meant checking visibility of the scrollRef instead of once per item. The visibility check was introduced to prevent items from performing their initial measurement while the collection is hidden.

state: VirtualizerState<T, V>,
_ref: RefObject<HTMLElement | null>
): VirtualizerAria {
useLayoutEffect(() => {

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.

Doesn't this mean that the size will only update if the entire virtualizer re-renders, not if only a single child does? React won't fire effects for parents when children update.

@nwidynski nwidynski Oct 29, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes it does, but isn't that fine in this case? This is only for the initial measurement of an item, which can only happen if virtualizer re-renders. Other child size changes can be disregarded if the flag for the RO is off, no?

Also, in RAC, did VirtualizerItem even run its effect on child re-render before this change? If I'm not mistaken, the wrapper and its rendered node don't share a component boundary?

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.

Can we try keeping the size measurement in the item, but batching the react re-render? Basically inside updateItemSize schedule a requestAnimationFrame (if we haven't already) and run the invalidate inside it. That would keep measuring synchronous but result in only one react re-render instead of one per item.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

React is already batching the invalidation re-render anyway, so I’m not sure I understand the idea behind that? The issue is that we force at least one (currently 2) style recalculation for every item. That can't be avoided as long as useVirtualizerItem does the measuring I think?

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.

I thought the issue was that for each item we measure, update, measure, update etc, and this changes it to measure, measure, update.

@nwidynski nwidynski Nov 14, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Ah, no. It's that we temporarily unset the height, then measure scrollHeight to get the content size. That forces the first style recalc. Then we call checkVisibility after resetting the height, which forces a second one. We can get this down to one, but we can't avoid both. This PR essentially tries to batch these operations, so that the browser only needs to recalc styles once.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Virtualized Picker recalculates its layout several times for each visible option when it opens

2 participants