Skip to content

Update only the children near the clipping rect in clipping views on Android - #60522

Open
hryhoriiK97 wants to merge 2 commits into
react:mainfrom
hryhoriiK97:incremental-subview-clipping
Open

hryhoriiK97 wants to merge 2 commits into
react:mainfrom
hryhoriiK97:incremental-subview-clipping

Conversation

@hryhoriiK97

Copy link
Copy Markdown
Contributor

Summary:

With removeClippedSubviews, which FlatList turns on by default on Android, every scroll update calls ReactViewGroup.updateClippingToRect. That checks every child the view has had (allChildren): a rect intersection and a getTag lookup per child, though only the children near the visible area can change.

During flings through a 2,000-row FlatList on 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).

  • Finding the band. For each axis, a child can intersect a rect only if it, or a child before it, ends past the rect's start, and if it, or a child after it, starts before the rect's end. The view keeps the running maximum of its children's ends and the running minimum of their starts from the last child. Both are monotone for any order of children, so a binary search over each gives the band, and the bands of the two axes are intersected.
    • In a list, whose rows (or the flattened pieces of its rows) come one after the other, the band is the rows near the rect.
    • Children don't need to be sorted: pieces of a row in any order only widen the band a little.
    • The old and new bands are updated as two ranges when they don't overlap, as after scrollToIndex.
  • Updating it. The band runs the existing per-child update, so attaching and detaching work as before. Children outside the band are left as they are. That relies on one invariant: every child that doesn't intersect the last clipping rect is clipped.
    • A pass over every child establishes it.
    • Adding, removing or laying out a child keeps it, so the band is only rebuilt. That costs a few field reads per child and no getTag.
    • A focused, animating or excluded child that stays attached outside the rect breaks it. So does a child laid out while the view is detached, or one attached against a clipping rect the band doesn't know of. After any of these, the next update checks every child, as before.
  • Fallbacks:
    • Before each band update, the view checks that every attached child is in the band. If not, it logs a soft exception (ReactViewGroup.clippingBand) and checks every child from then on, until clipping is turned off or the view is recycled.
    • A band of more than half the children falls back to checking every child, for example when a child spans the list like a background.
    • Views with fewer than 16 children always check every child.
    • Children that aren't laid out yet (an empty frame at the origin) are left out of the running bounds. Any rect that could contain them gets a pass over every child.
  • Memory: the state lives in a ClippingBand that 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 child

Test 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:

  • scrolls and jumps;
  • rows added at the end, out of order, or before their layout;
  • rows removed, laid out elsewhere, or growing;
  • excluded, focused and animating children;
  • the list moving in its container;
  • clipping turned off and on.

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:

  • a focused child kept attached before a jump forward;
  • a child laid out while its clipping view is detached or has no parent;
  • a child attached against another clipping rect;
  • a child added without the clipping bookkeeping;
  • a child spanning the list;
  • children not laid out yet, including a rect that contains the origin;
  • zero-height separators;
  • recycling;
  • views with few children.

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.

./gradlew :packages:react-native:ReactAndroid:testDebugUnitTest

1,137 tests in 146 suites pass. yarn android-api-validate reports ReactAndroid.api unchanged, and yarn format-check-kotlin passes.

Device: an RNTester release build on a Galaxy S10+ (Android 12), on one device. The test screen is a FlatList of 2,000 rows (avatar, title, body text, counters), flung up and down 18 times with adb shell input swipe over 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 from dumpsys gfxinfo, and the time in updateClippingToRect from temporary instrumentation.

Vertical list Before After Change
Time in updateClippingToRect 519 ms 200 ms −61%
UI thread CPU 2,640 ms 2,293 ms −13%
Updates that used the band – 99.7%
Janky frames per run 6, 6, 0 2, 6, 1
90th percentile frame 18, 18, 8 ms 17, 18, 8 ms
Horizontal list (300 px items) Before After Change
Time in updateClippingToRect 165 ms 94 ms −43%
UI thread CPU 1,760 ms 1,780 ms within noise
Updates that used the band – 99.8%

Mounting 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

…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>
@github-actions github-actions Bot added the Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging. label Dec 3, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@hryhoriiK97

Copy link
Copy Markdown
Contributor Author

Hi @javache, with removeClippedSubviews (FlatList's default on Android), every scroll update checks every child of the content view. That's hundreds per update, with about 37 attached. This PR keeps running min/max bounds of the children, finds the few that can change with a binary search, and falls back to the full check whenever that assumption could break.

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

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

Labels

Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant