Skip to content

Keep a node's layout when React appends children that lay out the same - #60496

Open
hryhoriiK97 wants to merge 1 commit into
react:mainfrom
hryhoriiK97:keep-layout-unchanged-appended-children
Open

hryhoriiK97 wants to merge 1 commit into
react:mainfrom
hryhoriiK97:keep-layout-unchanged-appended-children

Conversation

@hryhoriiK97

Copy link
Copy Markdown
Contributor

Summary:

React's Fabric renderer rebuilds the children of an updated host node by cloning the node without children and calling appendChild for each child. The exception is when passChildrenWhenCloningPersistedNodes is on, which it is not in the open source renderer. YogaLayoutableShadowNode::appendChild always 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 keepLayoutWhenAppendingUnchangedChildren flag (default false). With the flag on:

  • A node cloned from a clean node with an empty list of children keeps the source's Yoga children.
  • appendChild compares each appended child with the source child at the same index. It uses the same checks that 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 was appended by the time a parent takes the node, a layout pass reads it, or the node is cloned. The clone case covers an inline view that its paragraph lays out.

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 with deferYogaChildCloningUntilLayout (#59302).

passChildrenWhenCloning.md now describes the append path.

Changelog:

[General] [Added] - Add the keepLayoutWhenAppendingUnchangedChildren feature flag, which keeps a node's layout when React appends children that lay out the same, so paint-only updates skip layout

Test Plan:

Tests

  • YogaAppendChildrenTest (14 new tests in ViewTest.cpp) rebuilds nodes the way React does. It checks that each of the following keeps the node clean or dirties it:

    • unchanged children;
    • paint-only and layout prop changes;
    • fewer, more, and reordered children;
    • nested rebuilds;
    • a hidden node;
    • a root committed without children;
    • a node cloned mid-append.

    The new tests pass. With the flag off, unchanged children still dirty the node, as before.

  • View-keepLayoutWhenAppendingUnchangedChildren-itest.js runs with keepLayoutWhenAppendingUnchangedChildren:* and updateRuntimeShadowNodeReferencesOnCommit:*. It renders sequences of updates and checks that the layout matches a fresh render:

    • paint-only then size changes;
    • removed, added and reordered children;
    • children changed while their parent was hidden;
    • an inline view whose children were removed;
    • re-rendering after an empty root.

    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-check and yarn lint pass. 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.

Workload skipUnchangedChildrenInDifferentiator + this flag
end to end / JS
also deferYogaChildCloningUntilLayout
end to end / JS
Memoized 5,461-view tree, one leaf changes color −61% / −64% −62% / −88%
Memoized 5,461-view tree, one leaf changes width −34% / −39% −34% / −42%
Non-memoized 5,461-view tree, one leaf changes 0% / −1% −10% / −9%
Feed of 60 rows, update one row −1% / −3% −1% / −4%
Feed of 60 rows, update all rows 0% / +2% −17% / +2%
341-view tree, leaf update and resize 0% / ±2% 0% / ±2%
Mounts +2 to +7% / +1 to +5% +1 to +7% / +1 to +3%
  • 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:

    • 40 ms with all flags off;
    • 32 ms with only this flag;
    • 23 ms with only skipUnchangedChildrenInDifferentiator;
    • 6.7 ms with both.

    deferYogaChildCloningUntilLayout alone 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

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>
@github-actions github-actions Bot added the Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging. label Dec 2, 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

Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant