Repository navigation
Keep a node's layout when React appends children that lay out the same - #60496
Open
hryhoriiK97 wants to merge 1 commit into
Open
hryhoriiK97 wants to merge 1 commit into
hryhoriiK97 wants to merge 1 commit into
Conversation
React's Fabric renderer rebuilds the children of an updated host node by cloning the node without children and appending them one by one, unless `passChildrenWhenCloningPersistedNodes` is on, which it is not in the open source renderer. `YogaLayoutableShadowNode::appendChild` always dirtied the node, so every update, including a paint-only one such as a background color change, dirtied every node from the updated one to the root, and Yoga laid out that path again. Behind the new `keepLayoutWhenAppendingUnchangedChildren` flag, a node cloned from a clean node with an empty list of children keeps the source's Yoga children, and `appendChild` compares each appended child with the source child at the same index using the checks `updateYogaChildren` applies when children are passed into the clone: the child is clean and has the same style and layout results. The node is dirtied when a child differs, or when a different number of children has been appended by the time a parent takes the node, a layout pass reads it, or the node is cloned. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
React's Fabric renderer rebuilds the children of an updated host node by cloning the node without children and calling
appendChildfor each child. The exception is whenpassChildrenWhenCloningPersistedNodesis on, which it is not in the open source renderer.YogaLayoutableShadowNode::appendChildalways dirties the node. So any update, even a paint-only one such as a background color change on one leaf, dirties every node from that leaf to the root, and Yoga lays that path out again.On a 5,461-view tree, Yoga logs about 1,270 uncached measurements and 324 node clones for one leaf's color change.
This change adds a
keepLayoutWhenAppendingUnchangedChildrenflag (defaultfalse). With the flag on:appendChildcompares each appended child with the source child at the same index. It uses the same checks thatupdateYogaChildrenapplies when children are passed into the clone: the child is clean, and has the same style and layout results.It works together with
skipUnchangedChildrenInDifferentiator(#59242). When the path is no longer relaid out, the differ can also skip the unchanged subtrees. Each change alone gains much less than the two together. I also measured it withdeferYogaChildCloningUntilLayout(#59302).passChildrenWhenCloning.mdnow describes the append path.Changelog:
[General] [Added] - Add the
keepLayoutWhenAppendingUnchangedChildrenfeature flag, which keeps a node's layout when React appends children that lay out the same, so paint-only updates skip layoutTest Plan:
Tests
YogaAppendChildrenTest(14 new tests inViewTest.cpp) rebuilds nodes the way React does. It checks that each of the following keeps the node clean or dirties it:The new tests pass. With the flag off, unchanged children still dirty the node, as before.
View-keepLayoutWhenAppendingUnchangedChildren-itest.jsruns withkeepLayoutWhenAppendingUnchangedChildren:*andupdateRuntimeShadowNodeReferencesOnCommit:*. It renders sequences of updates and checks that the layout matches a fresh render:20 of 20 pass.
With the flag's default switched to
true, all 1,638 ReactCommon C++ tests (ctest) and the full Fantom suite (4,261 passed, 64 skipped) pass.yarn flow-checkandyarn lintpass. The C++ API snapshots validate unchanged (Doxygen 1.16.1).Performance
Samsung Galaxy S10+ (Exynos 9820, Android 12), RNTester release build. Changes are relative to all three flags off, measured from the state update to the last UI mount (end to end) and to the layout effect (JS). Two interleaved launches per variant.
skipUnchangedChildrenInDifferentiator+ this flagend to end / JS
deferYogaChildCloningUntilLayoutend to end / JS
The color-only update in absolute terms: 41.3 → 16.0 ms end to end, 32.9 → 12.0 ms JS.
In a separate loop of 30 one-leaf color updates, timing each flag alone, JS time was:
skipUnchangedChildrenInDifferentiator;deferYogaChildCloningUntilLayoutalone did not change it.Mounts do not go through this path, and their JS time is nearly unchanged. The small end-to-end differences are within the noise of two launches.
An update that changes a size can still re-measure much of the tree: Yoga only reuses a container's cached layout for exactly the same available size. That is outside this change.
🤖 Generated with Claude Code