Skip to content

Batch setNativeProps commits within a task - #60355

Open
robhogan wants to merge 1 commit into
mainfrom
robhogan/batch-set-native-props
Open

robhogan wants to merge 1 commit into
mainfrom
robhogan/batch-set-native-props

Conversation

@robhogan

@robhogan robhogan commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

Summary:

Currently, every setNativeProps call commits the shadow tree:

shadowTreeRegistry_.visit(
family.getSurfaceId(), [&](const ShadowTree& shadowTree) {
// The lambda passed to `commit` may be executed multiple times.
// We need to create fresh copy of the `RawProps` object each time.
auto ancestorShadowNode =
shadowTree.getCurrentRevision().rootShadowNode;
shadowTree.commit(
[&](const RootShadowNode& oldRootShadowNode) {
auto rootNode = oldRootShadowNode.cloneTree(
family, [&](const ShadowNode& oldShadowNode) {
auto& componentDescriptor =
componentDescriptorRegistry_->at(
shadowNode->getComponentHandle());
PropsParserContext propsParserContext{
family.getSurfaceId(), *contextContainer_};
auto props = componentDescriptor.cloneProps(
propsParserContext,
getShadowNodeInSubtree(*shadowNode, ancestorShadowNode)
->getProps(),
RawProps(rawProps));
return oldShadowNode.clone({.props = props});
});
return std::static_pointer_cast<RootShadowNode>(rootNode);
},
{/* default commit options */});
});

That commit clones the node's ancestors, and cloning a parent with a new child list also clones each of its other children (YogaLayoutableShadowNode::adoptYogaChild). While enableAccumulatedUpdatesInRawPropsAndroid is off, Android also diffs each commit on the JS thread. So moving N sibling views with one call each per frame, as JS-driven animations do, is O(N²) per frame.

This adds enableSetNativePropsBatching. With it on, setNativeProps still records its props in nativeProps_DEPRECATED straight away, but queues the node, and the first call in a task queues a microtask that commits every queued node in one cloneMultiple commit per surface, applying the props the batch set on it. Committing in a microtask keeps these commits ahead of observers and the mount later in the task, as they are today, and JS reads of the shadow tree (NativeDOM, the measure methods, findNodeAtPoint, getRelativeLayoutMetrics) commit the queue first. With enableFabricCommitBranching, a React commit earlier in the task replaces the main branch when it's merged, so then the batch goes on top of React's revision instead. It's bridgeless only, because bridge mode doesn't drain microtasks.

One thing changes with the flag on: native code that reads the tree directly, rather than through JS, sees the new props once the microtask has run rather than after each call.

A bouncing-ball stress test in RNTester's Playground (N sibling views, each moved with setNativeProps every frame), release builds of main and this PR side by side:

android-main-vs-pr-public.mp4
ios-main-vs-pr-public.mp4
Device Views main This PR
Galaxy A16 200 5.0 fps 49.5 fps (~10x)
iPhone 12 mini 200 28.5 fps 60 fps (display cap)
iPhone 12 mini 400 8 fps 60 fps (display cap)

Changelog:

[Internal]

Test Plan:

New Fantom test, run with the flag and enableFabricCommitBranching each on and off:

yarn fantom packages/react-native/src/private/renderer/mounting/__tests__/SetNativeProps-itest.js

46 tests pass. Its ResizeObserver and throwing-task tests fail against a version that commits in the rendering update instead of a microtask. With commit branching on, its test for a React update before the call fails without the commit on top of React's revision. Its test with a native-driven animation fails against a version that reapplies every prop ever set through setNativeProps, which reverts the animated value.

The existing Mounting, ReactNativeElement, Animated, ResizeObserver and IntersectionObserver suites pass (945 tests).

Benchmark (AI-driven): RNTester release builds at main (3b49389) and this branch with the flag defaulted on, installed side by side, Playground running N absolutely positioned sibling views and one setNativeProps per view per frame from requestAnimationFrame. 200-view figures are medians of 6 interleaved cold launches each, from the app's own 500 ms frame counter after a 6 s warm-up. The iPhone 400-view figures are read from the app's on-screen frame counter in a screen recording (WebDriverAgent, about 18 fps), with capture running. The builds are from an earlier revision of this branch, which applied all of a view's stored native props in each batch rather than only the ones the batch set. Here every call sets all of them, so the work is the same.

@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 Nov 27, 2026
@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 Nov 27, 2026
@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 Nov 28, 2026
@robhogan
robhogan force-pushed the robhogan/batch-set-native-props branch 4 times, most recently from 7156d8f to 5a7beb5 Compare November 28, 2026 02:03
Every `setNativeProps` call commits the shadow tree. The commit clones the node's ancestors, and cloning a parent with a new child list also clones each of its other children (`YogaLayoutableShadowNode::adoptYogaChild`). While `enableAccumulatedUpdatesInRawPropsAndroid` is off, the default, Android also diffs each commit on the JS thread. So updating N siblings with one call each, as JS-driven animations do every frame, costs O(N²) per frame.

With `enableSetNativePropsBatching`, `setNativeProps` from JS still records its props in `nativeProps_DEPRECATED` straight away, but queues the node instead of committing it. The first call in a task queues a microtask that commits every queued node in one `cloneMultiple` commit per surface, applying the props the batch set on it at their current values. Committing in a microtask keeps these commits ahead of the observers and mount that follow in the task, as they are when each call commits, and the microtask still runs when the task throws. With `enableFabricCommitBranching`, a React commit earlier in the task replaces the main branch when it's merged, so then the batch is committed on top of React's revision instead.

JS reads of the shadow tree commit the queue first. `getShadowTreeRevisionProvider()` returns a provider that does this, which covers `NativeDOM`, the measure methods, `ResizeObserver` and view transitions, and `findNodeAtPoint` and `getRelativeLayoutMetrics` in `UIManagerBinding` do the same.

Queueing a microtask needs the `jsi::Runtime`, so the JS callers use a new `setNativeProps_DEPRECATED` overload that takes it. The existing overload is unchanged. Batching is bridgeless only, because bridge mode doesn't drain microtasks (Hermes has no microtask queue there), so with `enableBridgelessArchitecture` off every call still commits. The flag is off by default.

Changelog: [Internal]
@robhogan
robhogan force-pushed the robhogan/batch-set-native-props branch from 5a7beb5 to 694ee42 Compare November 28, 2026 02:17

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

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging. 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.

1 participant