Repository navigation
Update only the children near the clipping rect in clipping views on Android - #60522
Open
hryhoriiK97 wants to merge 2 commits into
Open
hryhoriiK97 wants to merge 2 commits into
hryhoriiK97 wants to merge 2 commits into
Conversation
…Android With removeClippedSubviews, which FlatList turns on by default on Android, every scroll update checked every child the view has had: a rect intersection and a getTag lookup per child. In a long list that is hundreds of children per update while a few dozen are attached. A new clipping rect now only updates the children in the band of indices that can intersect it or the previous rect. Per axis, the running maximum of the children's ends and the running minimum of their starts are monotone for any order of children, so binary searches find the band; the two axes' bands are intersected. The band runs the existing per-child update. Children outside it stay clipped, which relies on every child outside the last rect being clipped: a pass over every child establishes that, adding, removing and laying out children keep it, and anything that breaks it (a focused, animating or excluded child kept attached, a child laid out while the view is detached, a child attached against another rect) makes the next update check every child. A pre-check that every attached child is in the band logs a soft exception and falls back to checking every child. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
Author
|
Hi @javache, with During FlatList flings, it cut the time spent clipping by about 61% on a Galaxy S10+ and an A16, and UI-thread CPU by 13%, with frames unchanged. What do you think about this approach? I'm happy to put it behind a feature flag if you'd prefer. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
With
removeClippedSubviews, whichFlatListturns on by default on Android, every scroll update callsReactViewGroup.updateClippingToRect. That checks every child the view has had (allChildren): a rect intersection and agetTaglookup per child, though only the children near the visible area can change.During flings through a 2,000-row
FlatListon a Galaxy S10+, the list's content view had 500–800 children, of which about 37 were attached. Each update took 0.3–0.7 ms, and the loop was about 19% of the UI thread's CPU time.With this change, a new clipping rect only updates the children in the band of indices that can intersect it or the previous rect (
ClippingBand).scrollToIndex.getTag.ReactViewGroup.clippingBand) and checks every child from then on, until clipping is turned off or the view is recycled.ClippingBandthat a view creates only when it clips its subviews and has enough children, and releases when clipping is turned off or the view is recycled.There is no feature flag. Behavior is the same, and the tests compare it with checking every child; happy to put it behind one if you'd prefer.
Changelog:
[ANDROID] [CHANGED] - With
removeClippedSubviews, a scroll only updates the children near the visible area instead of every childTest Plan:
Unit tests.
ReactViewGroupIncrementalClippingTest(18 tests) compares the band with checking every child. The comparison uses a test-only switch, and after every change the same children must be attached, in the same order. The random tests use a list in a scrolling clipping container, as in an app, over four layouts: rows, flattened rows with sorted pieces, rows with centered pieces whose tops aren't in order, and a horizontal list. Each runs 8 seeds × 250 random changes:They also check that the band was used for over a third of the scrolls, that it stayed under a quarter of the children, and that the fallback for attached children outside the band never triggered.
Targeted tests cover:
Removing any one guard makes at least one test fail. The guards are: the detached-view invalidation, the kept-child invalidation, the unknown-clipping-rect check, the not-laid-out check, the band's end, and the second range's insertion offset.
1,137 tests in 146 suites pass.
yarn android-api-validatereportsReactAndroid.apiunchanged, andyarn format-check-kotlinpasses.Device: an RNTester release build on a Galaxy S10+ (Android 12), on one device. The test screen is a
FlatListof 2,000 rows (avatar, title, body text, counters), flung up and down 18 times withadb shell input swipeover 20 s. Each measurement is the mean of three interleaved runs with and without the change; the rows that compare UI-thread CPU, the clipping loop and the band are means. UI-thread CPU time comes from/proc/<pid>/task/<tid>/stat, frames fromdumpsys gfxinfo, and the time inupdateClippingToRectfrom temporary instrumentation.updateClippingToRectupdateClippingToRectMounting the vertical list, measured as UI-thread CPU from launch to 6 s, was 563 ms before and 547 ms after.
🤖 Generated with Claude Code