Skip to content

Skip the duplicate state and event emitter for views created and inserted in one transaction - #59707

Merged
meta-codesync[bot] merged 4 commits into
react:mainfrom
tomekzaw:skip-duplicate-state-and-emitter
Nov 18, 2026
Merged

meta-codesync[bot] merged 4 commits into
react:mainfrom
tomekzaw:skip-duplicate-state-and-emitter

Conversation

@tomekzaw

@tomekzaw tomekzaw commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

Summary:

A view that isn't preallocated gets a Create and an Insert in the same mount transaction, for example a layout-only View that gains a backgroundColor and stops being flattened. The Create item already passes the view's state and event emitter to Java: SurfaceMountingManager.createViewUnsafe stores both in the ViewState, and ViewManager.createView applies the state. The Insert then always adds UpdateState and UpdateEventEmitter items for the same objects. So C++ builds a second StateWrapperImpl and EventEmitterWrapper, and Java calls ViewManager.updateState again and destroys the first wrappers.

executeMount now remembers the state and event emitter of each view created in the transaction. Insert skips its UpdateState and UpdateEventEmitter items when they would pass the same objects. Preallocated views, which get no Create item, and views whose state changed between Create and Insert still get them.

Changelog:

[ANDROID] [CHANGED] - Stop sending the state and event emitter twice for views created and inserted in the same mount transaction

Test Plan:

A Playground screen with 300 layout-only views that gain a backgroundColor every 500 ms, so each such transaction creates and inserts 300 views. Time per transaction:

Galaxy A16 Pixel 9a OPPO CPH2269 moto g57 power
executeMount (C++) 2.01 ms → 1.67 ms (−17%) 2.20 ms → 1.97 ms (−11%) 7.44 ms → 6.16 ms (−17%) 1.80 ms → 1.57 ms (−13%)
Executing the mount items (Java) 33.32 ms → 33.18 ms (no change) 12.96 ms → 13.25 ms (no change) 51.92 ms → 51.02 ms (no change) 38.79 ms → 37.66 ms (no change)

These views have no state, so the C++ side builds 300 fewer EventEmitterWrappers per transaction. The Java side is dominated by creating the 300 Android views, so the skipped updateEventEmitter calls don't show there. On the Pixel 9a the C++ interval is wide, −23% to −4%.

Setup:

  • Release RNTester (./gradlew :packages:rn-tester:android:app:assembleRelease -PreactNativeArchitectures=arm64-v8a, R8 on) with default feature flags, compiled with cmd package compile -m speed.
  • Temporary timing probes, not part of this PR, and a Playground scenario opened by deep link.
  • Each run is a fresh process: a 12 s window after the scenario starts, reported as the median of its samples. Per phone, one discarded run of each build, then 8 A/B/B/A cycles of main and this PR (16 processes per build).
  • Each figure is the median of the 16 processes. A change is shown when its 95% bootstrap confidence interval excludes zero, and "no change" otherwise.
  • The Galaxy A16, Pixel 9a and moto g57 power run Android 16. The OPPO CPH2269 runs Android 11.

Also checked on the Galaxy A16: a box that gains a background and an onTouchEnd handler (so it gets a Create item) renders and counts taps.

🤖 Generated with Claude Code

…rted in one transaction

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@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 Oct 31, 2026
@facebook-github-tools facebook-github-tools Bot added p: Software Mansion Partner: Software Mansion Partner labels Oct 31, 2026
@tomekzaw
tomekzaw marked this pull request as ready for review October 31, 2026 08:22
@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 Oct 31, 2026
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@tomekzaw

Copy link
Copy Markdown
Contributor Author

cc @javache Friendly bump on this one 🙏

Comment on lines +771 to +777
auto createdView = createdViews.find(newChildShadowView.tag);
bool createdWithSameState = createdView != createdViews.end() &&
createdView->second.state == newChildShadowView.state.get();
bool createdWithSameEventEmitter =
createdView != createdViews.end() &&
createdView->second.eventEmitter ==
newChildShadowView.eventEmitter.get();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when would it be possible for this not to be true? In a given transaction, there's only one revision of a shadowview, so event emitter and state should always be consistent,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's what my AI agent says:

The premise holds for a single diff, but not quite for what executeMount actually receives on Android.

Where it's right. The differentiator builds the Create and the Insert for a new view from the same ShadowViewNodePair, so within one calculateShadowViewMutations call the two mutations carry the identical ShadowView, and the state and eventEmitter pointers always match. Every Create/Insert site in Differentiator.cpp works this way, and the Create item already serializes state and event emitter to Java, which createViewUnsafe stores and applies. So for one diff, the pointer comparison is always true and createdViews.contains(tag) would be equivalent.

Where the premise breaks. executeMount does not always get one diff. With enableAccumulatedUpdatesInRawPropsAndroid off, which is the default, FabricUIManagerBinding::schedulerDidFinishTransaction pulls a transaction per commit and, if the previous one has not been mounted yet, concatenates the new mutations onto it via MountingTransaction::mergeWith. So one executeMount can see two revisions of the same tag, for example commit 1 creating and inserting a view with state A, and commit 2 updating or reparenting it with state B. The mounting override delegates (LayoutAnimations, ViewTransition, Animated) also rewrite the mutation list before it reaches Android.

Does the difference matter in practice? Probably not for the merged case I can trace. The differ emits Update mutations before Insert mutations, so a reparented view in commit 2 would arrive as Update(A→B), Remove, Insert(B). The Update already delivers state B to Java, so skipping the second Insert's state would still be correct. I could not exhaustively rule out the override delegates, though, and the comparison costs two pointer compares after a hash lookup the code needs anyway.

Recommendation. Keep the comparison, since the downside of being wrong is silently stale state in Java with no error to point at it, and the cost is negligible. Dropping it to a std::unordered_set<Tag> and relying on the invariant is a reasonable call too. Either way, the comment above createdViews should say why the pointers are compared, since the original wording invited exactly this question.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also pushed a commit c54755e

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it could theoretically happen right now because we concatenate two commits instead of merging them. Let's add a TODO here to simplify this to a plain set once we've moved to the pull model or rollout enableAccumulatedUpdatesInRawPropsAndroid.

@tomekzaw tomekzaw Nov 17, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the TODO in bc821b1. I also dropped the mention of the override delegates from the comment, since they can only rewrite one diff and so don't produce a second revision of a tag on their own.

tomekzaw and others added 2 commits November 17, 2026 12:24
Within one diff the Create and Insert of a view carry the same
ShadowView, so the pointers always match. The comparison guards the
cases where executeMount gets more than one diff: merged pending
transactions and mounting override delegates.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Once transactions are pulled in schedulerShouldRenderTransactions, a
transaction is a single diff and the Create and Insert of a view always
carry the same state and event emitter.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@meta-codesync

meta-codesync Bot commented Nov 17, 2026

Copy link
Copy Markdown

@javache has imported this pull request. If you are a Meta employee, you can view this in D132557471.

@github-actions

Copy link
Copy Markdown

Visual regression

No visual differences at bc821b1b1219.

ios: no capture

@meta-codesync
meta-codesync Bot merged commit 364e6a0 into react:main Nov 18, 2026
57 checks passed
@react-native-bot

Copy link
Copy Markdown
Collaborator

This pull request was successfully merged by @tomekzaw in 364e6a0

When will my fix make it into a release? | How to file a pick request?

@react-native-bot react-native-bot added the Merged This PR has been merged. label Nov 18, 2026
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. Merged This PR has been merged. p: Software Mansion Partner: Software Mansion Partner 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.

3 participants