Skip to content

Stop VirtualizedList re-laying out forever after a width change - #60287

Open
jkasprzyk17 wants to merge 1 commit into
react:mainfrom
jkasprzyk17:fix/virtualizedlist-width-change-loop
Open

jkasprzyk17 wants to merge 1 commit into
react:mainfrom
jkasprzyk17:fix/virtualizedlist-width-change-loop

Conversation

@jkasprzyk17

@jkasprzyk17 jkasprzyk17 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary:

Fixes #60251.

When a list's width changes while it is scrolled down, and its rows change height with the width (wrapping text, aspectRatio), the list fires onContentSizeChange and moves its render window forever.

The cause is in ListMetricsAggregator. The first initialNumToRender rows stay mounted, so they are measured again at the new width. The row after them is the first row of the spacer. It isn't mounted, so it keeps the offset it had at the old width. The spacer is sized from that offset, so it starts away from where the mounted rows now end, and every row after it moves. The list then renders another window and measures those rows at new offsets, which moves the spacer again, and it never settles. Rows skipped by fast scrolling aren't needed for this: scrolling down and changing the width is enough.

This changes two things in ListMetricsAggregator. With only one of them, the list still doesn't settle.

  1. Forget unmounted cells after the list is resized. VirtualizedList now passes its own layout to the aggregator (notifyListLayout), and each cell records the list's cross-axis length (its width in a vertical list) from when the cell was last laid out. When a mounted cell's cross-axis length and length both change, and the list's cross-axis length differs from the one recorded for that cell, the aggregator forgets the unmounted cells that were laid out at a different list size. They would lay out differently too. The cell count, average length and highest measured index are updated to match. A cell that changes size while the list keeps its size doesn't affect the other cells. The trigger needs the cells to change too, because a horizontal list without a fixed height takes its height from the tallest mounted cell, which changes during ordinary scrolling.
  2. Estimate unmeasured cells from their neighbors. getCellMetricsApprox used averageLength * index for a cell that wasn't measured, which ignores the header and whatever was measured before it. It now splits the gap between the nearest measured cells before and after it evenly, or continues at the average length after the previous measured cell. This keeps offsets increasing across a spacer, and keeps the list close to the rows the user was looking at. Measured cells are kept in a sorted array of indices, so finding the neighbors is a binary search. Forgetting many cells at once rebuilds the array in one pass.

After the list is resized and its rows change with it, getHighestMeasuredCellIndex can go down. scrollToIndex to a far row that was measured only before the resize then calls onScrollToIndexFailed, the same as for a row that was never measured.

This relies on the list's onLayout reaching JS before its cells' in the same commit. Fabric emits layout events parent first.

This overlaps with #58916, which anchors a spacer's start when its first cell isn't measured. The test from that PR also passes with this change.

Changelog:

[GENERAL] [FIXED] - Fix FlatList and VirtualizedList firing onContentSizeChange in an endless loop after the list's width changes while scrolled down

Test Plan:

  • A new Fantom test in FlatList-itest.js renders 200 rows whose height comes from aspectRatio below a 600 tall header. It scrolls to y = 30000, changes the width (674 → 381, and 381 → 674), and checks that onContentSizeChange stops firing. On main it fails for both widths, and it also fails with only one of the two changes, or when VirtualizedList doesn't call notifyListLayout; with all of it the list settles in under 10 steps.
  • Another Fantom test scrolls a list of rows that aren't stretched across it (alignItems: 'flex-start') to the end and back, then grows only the first row. The content height must not drop below the height of all the rows. Without the list-size check it dropped from 10000 to 1050.
  • Another Fantom test replaces the data with fewer items after scrolling, so that some measured cells are past the end of the new data. The estimates skip those cells instead of reading their metrics.
  • New Jest tests in ListMetricsAggregator-test.js cover the estimates (including cells measured again at a new index and data that shrinks below measured cells), forgetting unmounted cells after the list is resized, keeping them when a single cell changes size or when they were laid out at the current list size, and a forgotten cell that shares its index with a mounted one.
  • With 80,000 cached rows, one invalidation took about 700 ms before the one-pass rebuild and about 50 ms after it (Jest on desktop Node, not committed).
yarn fantom packages/react-native/Libraries/Lists
Test Suites: 3 passed, 3 total
Tests:       79 passed, 79 total

yarn test packages/virtualized-lists
Tests:       1 skipped, 211 passed, 212 total
Snapshots:   69 passed, 69 total

yarn flow-check
Found 0 errors

ESLint and Prettier report nothing on the changed files.

I didn't run this on a device or in RNTester, and I didn't run the reporter's app.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 25, 2026
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Nov 25, 2026
@jkasprzyk17
jkasprzyk17 force-pushed the fix/virtualizedlist-width-change-loop branch from c69e33e to 9d6674b Compare November 25, 2026 21:02

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

Could we address the two cases noted in the inline comments before merging: individual cell size changes invalidating unrelated offscreen measurements, and quadratic index-array updates during bulk invalidation?

The existing 208 Jest tests, 69 snapshots, and lint passed locally. The reproductions described in the comments were checked against this commit and its parent.

const prevCrossAxisLength = this._cellCrossAxisLengths.get(cellKey);
let curr = this._cellMetrics.get(cellKey);

if (

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.

Could we distinguish a list-wide reflow from an individual cell changing size before invalidating the offscreen measurements?

I tried keeping the list width fixed, measuring 200 rows, returning to the first 10, and changing only row 0’s width and height. The highest measured index dropped from 199 to 9, and the tail spacer collapsed from 9,500 to 0. The parent commit retains that spacer.

Would a regression test for an independently expanding, non-stretched row help us cover this case?

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.

Thanks, that was a real regression. The trigger now also needs the list itself to have been resized:

  • VirtualizedList._onLayout passes the list's layout to the aggregator (notifyListLayout), and each cell records the list's cross-axis length from when it was last laid out.
  • Unmounted cells are forgotten only when a mounted cell changes length together with its cross-axis length, and the list's cross-axis length differs from the one recorded for that cell.
  • Only unmounted cells laid out at a different list size are forgotten, so cells already measured at the new size are kept.

I added your case as a Jest test (200 rows, 10–199 unmounted, row 0 changes width and height while the list width stays the same, and the highest measured index stays 199). There's also a Fantom test with rows that aren't stretched (alignItems: 'flex-start'): it scrolls to the end and back, then grows only row 0. On 9d6674b the content height dropped from 10000 to 1050 there. Now it stays at 10000, the same as on main. The width-change tests still settle, and they loop again if notifyListLayout isn't called.

Two cases this still treats as a re-flow:

  • A mounted cell whose frame didn't change when the list was resized (a row that doesn't depend on the width) keeps the old list size in its record. If that cell later changes width and height on its own, the unmounted cells measured before the resize are forgotten.
  • In a horizontal list without a fixed height, the list's height follows the tallest mounted cell, so a cell that grows in both directions also resizes the list.

This relies on the list's onLayout reaching JS before its cells' in the same commit, which is the order Fabric emits layout events in (parent first).

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.

Thanks for adding the list-size check and the regression tests. The original fixed-width case now retains its offscreen measurements, and the bulk invalidation no longer splices the index array.

Could we also protect the two remaining cases you mentioned, where the other rows still have valid measurements?

  • The list resizes while its fixed-size rows keep the same frames, so those rows emit no new layout events. One mounted row later expands independently in both dimensions.
  • One row expands in both dimensions in a horizontal list whose height follows its tallest mounted row.

I checked both with metric-level reproductions: after measuring 200 rows and unmounting rows 10–199, expanding row 0 drops the highest measured index from 199 to 9 and the tail spacer from 9,500 to zero. The parent retains the measurements and spacer. The first case still triggers because the unchanged row keeps its old list-width record until its later expansion.

Would regression coverage for these cases help us narrow the trigger further, so an unrelated row expansion keeps the valid offscreen measurements?

} else {
this._cellMetrics.delete(cellKey);
this._cellCrossAxisLengths.delete(cellKey);
this._removeMeasuredCellIndex(metrics.index);

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.

Could we rebuild the sorted indices once when invalidating multiple cells?

Each removal currently splices the array and shifts the remaining entries, so clearing a large cache becomes quadratic. With 80,000 cached rows, I measured roughly 3.2 billion shifted entries and about 0.6 seconds for one resize in desktop Node.

Filtering the indices in one pass looks like it could preserve the behavior while keeping resize work linear.

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.

Done. Forgetting cells now only decrements _measuredCellIndexCounts for each one, then filters _measuredCellIndices once at the end.

With 80,000 cached rows (all but 10 unmounted), one invalidation went from about 700 ms to about 50 ms in Jest on desktop Node. Most of what's left is deleting the entries from the maps. A new test covers a forgotten cell that shares its index with a mounted one, so that index stays in the array.

After the list's width changes while it is scrolled down, rows whose
height depends on the width (wrapping text, aspectRatio) made the list
fire onContentSizeChange and move its render window forever.

The rows that stay mounted are measured again at the new width, but the
first row of the spacer below them keeps the offset it had at the old
width, so the spacer starts away from where the mounted rows end. Every
row after it moves, the list renders another window, and the spacer
moves again.

ListMetricsAggregator now forgets the metrics of unmounted cells when
the list was resized and a mounted cell changes length together with
its cross-axis length, and estimates unmeasured cells from the measured
cells around them instead of averageLength * index. Each change alone
still loops. A cell that changes size while the list keeps its size
doesn't affect the metrics of the other cells.
@jkasprzyk17
jkasprzyk17 force-pushed the fix/virtualizedlist-width-change-loop branch from 9d6674b to db8d3aa Compare November 27, 2026 12:17
/**
* Notify ListMetricsAggregator that the list itself has been laid out.
*/
notifyListLayout({

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.

Could we run yarn build-types and include the regenerated packages/react-native/ReactNativeApi.d.ts snapshot with this change?

The new notifyListLayout method appears in the generated API, but the committed snapshot does not include it. Both test_js CI jobs currently fail at yarn build-types --validate for this difference, and I reproduced the same validation failure locally.

This branch has not been deployed

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

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

VirtualizedList / FlatList loops onContentSizeChange indefinitely after width change when scrolled deep

2 participants