Repository navigation
Skip the duplicate state and event emitter for views created and inserted in one transaction - #59707
Conversation
…rted in one transaction Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
cc @javache Friendly bump on this one 🙏 |
| 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(); |
There was a problem hiding this comment.
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,
There was a problem hiding this comment.
Here's what my AI agent says:
The premise holds for a single diff, but not quite for what
executeMountactually 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 onecalculateShadowViewMutationscall the two mutations carry the identicalShadowView, and thestateandeventEmitterpointers always match. Every Create/Insert site inDifferentiator.cppworks this way, and the Create item already serializes state and event emitter to Java, whichcreateViewUnsafestores and applies. So for one diff, the pointer comparison is always true andcreatedViews.contains(tag)would be equivalent.Where the premise breaks.
executeMountdoes not always get one diff. WithenableAccumulatedUpdatesInRawPropsAndroidoff, which is the default,FabricUIManagerBinding::schedulerDidFinishTransactionpulls a transaction per commit and, if the previous one has not been mounted yet, concatenates the new mutations onto it viaMountingTransaction::mergeWith. So oneexecuteMountcan 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 abovecreatedViewsshould say why the pointers are compared, since the original wording invited exactly this question.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
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>
|
@javache has imported this pull request. If you are a Meta employee, you can view this in D132557471. |
Visual regressionNo visual differences at ios: no capture |
|
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? |
Summary:
A view that isn't preallocated gets a Create and an Insert in the same mount transaction, for example a layout-only
Viewthat gains abackgroundColorand stops being flattened. The Create item already passes the view's state and event emitter to Java:SurfaceMountingManager.createViewUnsafestores both in theViewState, andViewManager.createViewapplies the state. The Insert then always addsUpdateStateandUpdateEventEmitteritems for the same objects. So C++ builds a secondStateWrapperImplandEventEmitterWrapper, and Java callsViewManager.updateStateagain and destroys the first wrappers.executeMountnow remembers the state and event emitter of each view created in the transaction. Insert skips itsUpdateStateandUpdateEventEmitteritems 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
backgroundColorevery 500 ms, so each such transaction creates and inserts 300 views. Time per transaction:executeMount(C++)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 skippedupdateEventEmittercalls don't show there. On the Pixel 9a the C++ interval is wide, −23% to −4%.Setup:
./gradlew :packages:rn-tester:android:app:assembleRelease -PreactNativeArchitectures=arm64-v8a, R8 on) with default feature flags, compiled withcmd package compile -m speed.mainand this PR (16 processes per build).Also checked on the Galaxy A16: a box that gains a background and an
onTouchEndhandler (so it gets a Create item) renders and counts taps.🤖 Generated with Claude Code