Repository navigation
Stop VirtualizedList re-laying out forever after a width change - #60287
jkasprzyk17 wants to merge 1 commit into
Conversation
c69e33e to
9d6674b
Compare
cipolleschi
left a comment
There was a problem hiding this comment.
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 ( |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Thanks, that was a real regression. The trigger now also needs the list itself to have been resized:
VirtualizedList._onLayoutpasses 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).
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
9d6674b to
db8d3aa
Compare
| /** | ||
| * Notify ListMetricsAggregator that the list itself has been laid out. | ||
| */ | ||
| notifyListLayout({ |
There was a problem hiding this comment.
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.
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 firesonContentSizeChangeand moves its render window forever.The cause is in
ListMetricsAggregator. The firstinitialNumToRenderrows 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.VirtualizedListnow 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.getCellMetricsApproxusedaverageLength * indexfor 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,
getHighestMeasuredCellIndexcan go down.scrollToIndexto a far row that was measured only before the resize then callsonScrollToIndexFailed, the same as for a row that was never measured.This relies on the list's
onLayoutreaching 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
FlatListandVirtualizedListfiringonContentSizeChangein an endless loop after the list's width changes while scrolled downTest Plan:
FlatList-itest.jsrenders 200 rows whose height comes fromaspectRatiobelow a 600 tall header. It scrolls to y = 30000, changes the width (674 → 381, and 381 → 674), and checks thatonContentSizeChangestops firing. Onmainit fails for both widths, and it also fails with only one of the two changes, or whenVirtualizedListdoesn't callnotifyListLayout; with all of it the list settles in under 10 steps.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.ListMetricsAggregator-test.jscover 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.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.