Skip to content

Emit Fabric subtree mount and unmount mutations directly - #60540

Open
ide wants to merge 1 commit into
react:mainfrom
expo:@ide/differentiator-direct-mutations
Open

ide wants to merge 1 commit into
react:mainfrom
expo:@ide/differentiator-direct-mutations

Conversation

@ide

@ide ide commented Dec 4, 2026 •

Copy link
Copy Markdown
Contributor

Summary:

The Fabric Differentiator writes mount and unmount mutations for whole subtrees directly into the destination list when enableDirectSubtreeMutations is enabled. The flag defaults to off. Direct emission avoids allocating and merging a temporary mutation list at each level of the subtree.

Changelog:

[INTERNAL] [CHANGED] - Add a feature flag for direct emission of Fabric subtree mount and unmount mutations.

Test Plan:

ctest --test-dir private/react-native-fantom/build/tester/react-common-tests --output-on-failure -j 10 passes with the flag default off and in a local build with the default on. The regression test runs in both modes. It mounts and unmounts a subtree in which one view forms a stacking context, one view is flattened away, and one view stays in the mounted tree while its children mount in its parent. It compares the results with independently built trees.

yarn fantom packages/react-native/src/private/renderer/mounting/__tests__ passes with the default and with both values of the flag selected by a local @fantom_flags enableDirectSubtreeMutations:* override.

A single Release C++ binary on Apple M6 uses proc_pid_rusage to count retired instructions for diffs of a 341-view tree with four children per parent. For each operation, both flag settings emit the same ordered list of 682 mutations. Across five batches of 1,000 diffs per setting with alternating flag order, the median per mount diff falls from 1,527,412 to 458,622 and the median per unmount diff from 1,526,089 to 457,443.

node scripts/clang-format.js --check, yarn flow-check and yarn lint pass, and yarn featureflags --update reproduces the generated bindings.

@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 4, 2026
@ide
ide force-pushed the @ide/differentiator-direct-mutations branch from 3b1e8d5 to 21241d4 Compare December 4, 2026 11:32
@ide ide changed the title Emit mutations directly for inserted and removed Fabric subtrees Emit Fabric subtree mount and unmount mutations directly Dec 4, 2026
@ide
ide force-pushed the @ide/differentiator-direct-mutations branch from 21241d4 to 6668815 Compare December 4, 2026 12:35
@ide
ide marked this pull request as ready for review December 4, 2026 14:37
@javache

javache commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Can you add a feature flag for this so we can manage the rollout?

@ide
ide force-pushed the @ide/differentiator-direct-mutations branch from 6668815 to 885af56 Compare December 4, 2026 19:26
@ide

ide commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

Added enableDirectSubtreeMutations in 885af56. It defaults to off, and with it off the Differentiator runs the existing code. When it is on, a child list that is only inserted or only removed has its mutations emitted directly. I also narrowed the change to those whole-subtree mounts and unmounts, and measured again in a Release build. The test plan has the new numbers.

@chrfalch chrfalch left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The fast path matches the existing all-create and all-delete branches. Creates still come before the subtree and inserts after it. Removes still run in reverse mount order and deletes in forward order. It also skips the same pairs that the existing code skips: non-concrete pairs, and concrete pairs whose children are flattened, for which the slicer returns an empty list anyway. Resizing the scope back after each child keeps memory bounded, and std::deque keeps the caller's pair pointers valid. I found no correctness problem. The two comments below are about a hidden side effect on the caller's pairs and one branch the new test does not reach.

@ide
ide force-pushed the @ide/differentiator-direct-mutations branch from 885af56 to aacb3fc Compare December 5, 2026 08:30
@ide

ide commented Dec 5, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review. I made the move out of the pairs visible in the signature and added the missing tree shape to the test; details are in the two threads. The branch is also rebased onto current main, where the generated feature flag files conflicted, and the benchmark numbers in the test plan are re-measured on that base with the same method: the mount median falls from 1,527,412 to 458,622 instructions and the unmount median from 1,526,089 to 457,443.

The Fabric Differentiator writes mount and unmount mutations for whole subtrees directly into the destination list when `enableDirectSubtreeMutations` is enabled. The flag defaults to off. Direct emission avoids allocating and merging a temporary mutation list at each level of the subtree.

`ctest --test-dir private/react-native-fantom/build/tester/react-common-tests --output-on-failure -j 10` passes with the flag default off and in a local build with the default on. The regression test runs in both modes. It mounts and unmounts a subtree in which one view forms a stacking context, one view is flattened away, and one view stays in the mounted tree while its children mount in its parent. It compares the results with independently built trees.

`yarn fantom packages/react-native/src/private/renderer/mounting/__tests__` passes with the default and with both values of the flag selected by a local `@fantom_flags enableDirectSubtreeMutations:*` override.

A single Release C++ binary on Apple M6 uses `proc_pid_rusage` to count retired instructions for diffs of a 341-view tree with four children per parent. For each operation, both flag settings emit the same ordered list of 682 mutations. Across five batches of 1,000 diffs per setting with alternating flag order, the median per mount diff falls from 1,527,412 to 458,622 and the median per unmount diff from 1,526,089 to 457,443.

`node scripts/clang-format.js --check`, `yarn flow-check` and `yarn lint` pass, and `yarn featureflags --update` reproduces the generated bindings.

Signed-off-by: James Ide <ide@expo.dev>
@ide
ide force-pushed the @ide/differentiator-direct-mutations branch from aacb3fc to f71e48d Compare December 5, 2026 19:08

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.

3 participants