Skip to content

[iOS][Fabric] Skip the ScrollView clipping pass when no view uses removeClippedSubviews - #60230

Open
beqramo wants to merge 1 commit into
react:mainfrom
beqramo:scrollview-skip-clipping-pass
Open

beqramo wants to merge 1 commit into
react:mainfrom
beqramo:scrollview-skip-clipping-pass

Conversation

@beqramo

@beqramo beqramo commented Nov 23, 2026 •

Copy link
Copy Markdown

Summary:

Fixes #60227. Repro: #60229.

On iOS (Fabric), -[RCTScrollViewComponentView mountingTransactionDidMount:withSurfaceTelemetry:] calls _remountChildren after every mounting transaction on the surface. _remountChildren sends updateClippedSubviewsWithClipRect:relativeToView: into the content, and the default UIView (ComponentViewProtocol) implementation (also used by RCTViewComponentView when removeClippedSubviews is off) does one convertRect:toView: and recurses into every subview. Only an RCTViewComponentView with removeClippedSubviews acts on this pass. In an app where no view sets it, the pass walks every view under every ScrollView and changes nothing. With a large tree and a commit every frame (Reanimated UI-thread commits, or back-to-back React commits), this is seconds of main-thread time: our production app reports 5–6 s main-thread hangs and MetricKit CPU exceptions with this exact stack, mostly on iPads.

This change:

  • RCTViewComponentView keeps a count of views with _removeClippedSubviews set. All writes go through one setter, so the count follows prop changes, prepareForRecycle, and deallocation. +anyViewRemovesClippedSubviews exposes it.
  • RCTScrollViewComponentView._remountChildren returns early when the count is zero. When any view has removeClippedSubviews, the pass runs exactly as before, so clipping behavior does not change.

Alternatives considered:

  • Run _remountChildren from mountingTransactionDidMount only when the transaction has mutations under this ScrollView. That needs a subtree lookup per transaction and still walks everything for unrelated changes inside the ScrollView.
  • Recurse only into subviews that are, or contain, a clipping view. That needs per-view bookkeeping up the ancestor chain. The global count is simpler, costs O(1) per check, and covers the common case (no clipping views at all) completely; apps that use removeClippedSubviews keep today's behavior.

Notes:

  • The count is a static, read and written on the main thread only (mounting, recycling and deallocation of component views happen there).
  • Paper's RCTView uses a different selector (react_updateClippedSubviewsWithClipRect:relativeToView:), so legacy-interop views are not reached by this pass today either.
  • The new class method is added to RCTViewComponentView.h. If you would rather not grow the public header, it can move to a private header.

Changelog:

[IOS] [FIXED] - Skip the ScrollView clipping pass after each mounting transaction when no view uses removeClippedSubviews

Test Plan:

Unit tests (added, run locally):

  • RCTViewComponentViewTests testAnyViewRemovesClippedSubviewsFollowsPropsRecyclingAndDeallocation: the count follows props on, recycle, props on again, and deallocation.
  • RCTScrollViewComponentViewTests testClippingPassRunsOnlyWhenAViewRemovesClippedSubviews: scrolling past the clipping leeway does not call updateClippedSubviewsWithClipRect: on content when no view clips, and does once a view has removeClippedSubviews.

These files belong to the Tests test spec of React-RCTFabric.podspec, which open-source CI does not pull in, so I ran them locally: in RNTester I enabled :testspecs => ['Tests'] for React-RCTFabric in scripts/cocoapods/fabric.rb and limited the spec to Tests/Mounting/**/*.mm (Tests/Text/* does not compile on its own: react/renderer/element/ComponentBuilder.h not found). Neither edit is in this PR. Then xcodebuild test-without-building -scheme React-RCTFabric-Unit-Tests -only-testing:React-RCTFabric-Unit-Tests/RCTViewComponentViewTests -only-testing:React-RCTFabric-Unit-Tests/RCTScrollViewComponentViewTests on an iOS 26.5 simulator (Xcode 26.6):

  • The two classes: 39/39 pass; with -test-iterations 3: 117/117; 5 runs in random order (classes and tests shuffled): 39/39 each. No order dependence from the shared count.
  • All 7 classes in Tests/Mounting: 51/51 pass.
  • Mutation checks: reverting only the ScrollView early return fails testClippingPass… (clippingPassCount 1 ≠ 0); removing the decrement in dealloc, or putting back _removeClippedSubviews = NO in prepareForRecycle, fails the count test.
  • clang-format (the repo's pinned 21.1.2 via scripts/clang-format) reports no changes for all five files.

On a simulator (RN 0.88.1 built from source with this patch applied, Release, iOS 26.5, iPhone 17): a ScrollView with about 4k plain views and a Reanimated useAnimatedStyle opacity animation above it (the tree from #60229). sample <pid> for 10 s each:

main-thread samples in -[RCTMountingManager performTransaction:] in updateClippedSubviewsWithClipRect: app CPU (one core)
0.88.1 908 809 13.8 %
0.88.1 + this change 50 0 2.9 %
+ a mounted ScrollView removeClippedSubviews with 60 rows 776 707 13.1 %
after unmounting that ScrollView again 67 0 4.0 %

With the clipping ScrollView mounted (third row), clipping still works: rows far outside the viewport are detached from the view hierarchy, and rows scrolled into view are attached again (checked by querying the native view hierarchy for the rows' nativeIDs before and after scrolling). After unmounting it (fourth row), the count is back to zero and the pass is skipped again.

The 0.88.1 patch needed one hunk adjusted by hand (updateProps compares oldViewProps there); the code is otherwise the same as in this PR.

🤖 Generated with Claude Code

…oveClippedSubviews

After every mounting transaction, RCTScrollViewComponentView walks its whole content with updateClippedSubviewsWithClipRect:relativeToView: (one convertRect:toView: per view). Only views with removeClippedSubviews act on that pass. RCTViewComponentView now counts those views, and _remountChildren returns early when the count is zero.

Fixes react#60227

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@meta-cla

meta-cla Bot commented Nov 23, 2026

Copy link
Copy Markdown

Hi @beqramo!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla

meta-cla Bot commented Nov 23, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@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 23, 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 23, 2026

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

1 participant