Skip to content

Avoid copying ShadowViews into Android mount items - #60224

Open
riteshshukla04 wants to merge 1 commit into
react:mainfrom
riteshshukla04:android-cppmountitem-shadowview-refs
Open

riteshshukla04 wants to merge 1 commit into
react:mainfrom
riteshshukla04:android-cppmountitem-shadowview-refs

Conversation

@riteshshukla04

@riteshshukla04 riteshshukla04 commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary:

[AI Generated PR] .
The code was written by AI.

While working on some new features in react-native-release-profiler , I found that building the items, destroying them (~CppMountItem alone was 16.5%) and reallocating the vectors made up about 44% of executeMount using simple perf.

So I created a test bench in RN Tester. I created 6*500 view matrix in RN tester . It took FabricMountingManager::executeMount 87ms there.

Problem:

FabricMountingManager::executeMount turns each mutation into short-lived CppMountItems, writes them into the JNI buffers, and discards them. Each item copied its old and new ShadowView. A copy costs three atomic reference count increments (props, event emitter, state), three decrements on destruction, and ~160-byte moves whenever the item vectors grow. In a release profile on a Galaxy M14, creating, destroying and reallocating these items took ~44% of executeMount. The copies are unnecessary: the views already live in the transaction's mutations, which outlive the call and don't change during it.

Fix:

CppMountItem now stores const ShadowView& references into the transaction's mutations instead of copies, which removes six atomic reference count updates per view.
Items with nothing on one side point at a shared static emptyShadowView(), and the one modified copy (the preallocated props) lives in a function-local std::deque so its address stays stable.

Changelog:

[ANDROID] [CHANGED] - FabricMountingManager::executeMount no longer copies ShadowViews into every mount item

Test Plan:

Tested on RN tester
image

`FabricMountingManager::executeMount` turns every mutation into one or more
`CppMountItem`s, and each item stored copies of its old and new
`ShadowView`. A copy costs three atomic reference count increments (props,
event emitter, state) and three decrements when the item is destroyed, and
the item vectors move these ~160-byte structs on every reallocation.

`CppMountItem` now holds `const ShadowView&` references instead. The items
only exist inside `executeMount`, and the transaction whose mutations they
point into is a `const&` parameter that outlives the call and is not
modified during it. The empty side of an item binds to a shared static
`CppMountItem::emptyShadowView()`. The one modified local copy (the
preallocated-props diff on insert) is kept in a `std::deque` local to
`executeMount`, whose element addresses are stable.

The JNI buffers written are unchanged.
@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

Development

Successfully merging this pull request may close these issues.

1 participant