From 9da1a011530faa45b23bb18a382252e196011120 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dawid=20Ma=C5=82ecki?= Date: Wed, 29 Jul 2026 08:01:12 -0700 Subject: [PATCH] Wire the Android pull model in C++ behind the feature flag (#57579) Summary: Pull Request resolved: https://github.com/react/react-native/pull/57579 Makes `enableMountingCoordinatorPullModelAndroid` functional. With the flag on, the commit thread only signals transaction availability and the UI thread pulls and applies at mount time (matching iOS/macOS); with the flag off (default), behavior is byte-for-byte identical. - `schedulerShouldRenderTransactions`: notifies via JNI (`FabricMountingManager::onTransactionAvailable`) instead of pulling and building the batch. - `schedulerDidFinishTransaction`: no-op under the pull model. - `FabricUIManagerBinding::pullAndExecuteTransaction` (new JNI method): pulls the surface's transaction on the UI thread and runs `executeMount`. - `FabricMountingManager::executeMount`: gains a `synchronous` mode that executes the batch directly on the UI thread. - The accumulation sites remain gated on `enableAccumulatedUpdatesInRawPropsAndroid`; the pull model requires that flag to be co-enabled, since a pull may collapse several commits into one diff and therefore needs complete accumulated rawProps. ## Changelog: [Android] [Added] - Wire the pull-model mounting path in C++ behind `enableMountingCoordinatorPullModelAndroid` Reviewed By: christophpurrer Differential Revision: D112309053 fbshipit-source-id: 6e3ca5b8fbc8db647356ac555890c2e465890100 --- .../react/fabric/FabricUIManager.java | 34 ++++++---- .../react/fabric/FabricMountingManager.cpp | 19 ++++-- .../jni/react/fabric/FabricMountingManager.h | 18 +++++- .../react/fabric/FabricUIManagerBinding.cpp | 62 ++++++++++++++++++- .../jni/react/fabric/FabricUIManagerBinding.h | 2 + .../api-snapshots/ReactAndroidDebugCxx.api | 3 +- .../api-snapshots/ReactAndroidNewarchCxx.api | 3 +- .../api-snapshots/ReactAndroidReleaseCxx.api | 3 +- 8 files changed, 122 insertions(+), 22 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java index 72bed4566bc..7479f9cb53e 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/FabricUIManager.java @@ -938,7 +938,8 @@ public class FabricUIManager long layoutEndTime, long finishTransactionStartTime, long finishTransactionEndTime, - int affectedLayoutNodesCount) { + int affectedLayoutNodesCount, + boolean synchronous) { // When Binding.cpp calls scheduleMountItems during a commit phase, it always calls with // a BatchMountItem. No other sites call into this with a BatchMountItem, and Binding.cpp only // calls scheduleMountItems with a BatchMountItem. @@ -952,8 +953,9 @@ public class FabricUIManager } else { shouldSchedule = mountItem != null; } - // In case of sync rendering, this could be called on the UI thread. Otherwise, - // it should ~always be called on the JS thread. + // In the push model, this is ~always called on the JS thread, or on the UI + // thread in case of sync rendering. + // In the pull model, this is always called on the UI thread, at pull time. for (UIManagerListener listener : mListeners) { listener.didScheduleMountItems(this); } @@ -968,16 +970,22 @@ public class FabricUIManager if (shouldSchedule) { Assertions.assertNotNull(mountItem, "MountItem is null"); - mMountItemDispatcher.addMountItem(mountItem); - if (UiThreadUtil.isOnUiThread()) { - Runnable runnable = - new GuardedRunnable(mReactApplicationContext) { - @Override - public void runGuarded() { - mMountItemDispatcher.tryDispatchMountItems(); - } - }; - runnable.run(); + if (synchronous) { + // Pull model: we are already on the UI thread, inside the dispatcher's loop executing + // a PullTransactionMountItem. We don't schedule the item, we execute it directly. + mountItem.execute(mMountingManager); + } else { + mMountItemDispatcher.addMountItem(mountItem); + if (UiThreadUtil.isOnUiThread()) { + Runnable runnable = + new GuardedRunnable(mReactApplicationContext) { + @Override + public void runGuarded() { + mMountItemDispatcher.tryDispatchMountItems(); + } + }; + runnable.run(); + } } } diff --git a/packages/react-native/ReactAndroid/src/main/jni/react/fabric/FabricMountingManager.cpp b/packages/react-native/ReactAndroid/src/main/jni/react/fabric/FabricMountingManager.cpp index b82bdab278c..0926ffa7ded 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/react/fabric/FabricMountingManager.cpp +++ b/packages/react-native/ReactAndroid/src/main/jni/react/fabric/FabricMountingManager.cpp @@ -52,6 +52,13 @@ void FabricMountingManager::onSurfaceStop(SurfaceId surfaceId) { allocatedViewRegistry_.erase(surfaceId); } +void FabricMountingManager::onTransactionAvailable(SurfaceId surfaceId) { + static auto onTransactionAvailable = + JFabricUIManager::javaClassStatic()->getMethod( + "onTransactionAvailable"); + onTransactionAvailable(javaUIManager_, surfaceId); +} + bool FabricMountingManager::isViewAllocated(SurfaceId surfaceId, Tag tag) { std::lock_guard lock(allocatedViewsMutex_); auto it = allocatedViewRegistry_.find(surfaceId); @@ -568,7 +575,8 @@ inline void writeUpdateOverflowInsetMountItem( } // namespace void FabricMountingManager::executeMount( - const MountingTransaction& transaction) { + const MountingTransaction& transaction, + bool synchronous) { TraceSection section("FabricMountingManager::executeMount"); std::scoped_lock lock(commitMutex_); @@ -830,7 +838,8 @@ void FabricMountingManager::executeMount( jlong, jlong, jlong, - jint)>("scheduleMountItem"); + jint, + jboolean)>("scheduleMountItem"); if (batchMountItemIntsSize == 0) { auto finishTransactionEndTime = telemetryTimePointNow(); @@ -845,7 +854,8 @@ void FabricMountingManager::executeMount( telemetryTimePointToMilliseconds(telemetry.getLayoutEndTime()), telemetryTimePointToMilliseconds(finishTransactionStartTime), telemetryTimePointToMilliseconds(finishTransactionEndTime), - telemetry.getAffectedLayoutNodesCount()); + telemetry.getAffectedLayoutNodesCount(), + static_cast(synchronous)); return; } @@ -1012,7 +1022,8 @@ void FabricMountingManager::executeMount( telemetryTimePointToMilliseconds(telemetry.getLayoutEndTime()), telemetryTimePointToMilliseconds(finishTransactionStartTime), telemetryTimePointToMilliseconds(finishTransactionEndTime), - telemetry.getAffectedLayoutNodesCount()); + telemetry.getAffectedLayoutNodesCount(), + static_cast(synchronous)); env->DeleteLocalRef(buffer.ints); } diff --git a/packages/react-native/ReactAndroid/src/main/jni/react/fabric/FabricMountingManager.h b/packages/react-native/ReactAndroid/src/main/jni/react/fabric/FabricMountingManager.h index 377880b658b..3d31c58db7e 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/react/fabric/FabricMountingManager.h +++ b/packages/react-native/ReactAndroid/src/main/jni/react/fabric/FabricMountingManager.h @@ -54,7 +54,23 @@ class FabricMountingManager final { */ bool isViewAllocated(SurfaceId surfaceId, Tag tag); - void executeMount(const MountingTransaction &transaction); + /* + * Converts the transaction's mutations into an IntBufferBatchMountItem and + * hands it to Java. + * + * In the push model (`synchronous` = false), the batch is + * scheduled onto the UI thread asynchronously. + * + * In the pull model (`synchronous` = true) the batch is + * applied immediately on the calling (UI) thread. + */ + void executeMount(const MountingTransaction &transaction, bool synchronous = false); + + /* + * Pull model: notify Java that a transaction is available for `surfaceId` so + * the UI thread can pull it via a PullTransactionMountItem. + */ + void onTransactionAvailable(SurfaceId surfaceId); void dispatchCommand(const ShadowView &shadowView, const std::string &commandName, const folly::dynamic &args); diff --git a/packages/react-native/ReactAndroid/src/main/jni/react/fabric/FabricUIManagerBinding.cpp b/packages/react-native/ReactAndroid/src/main/jni/react/fabric/FabricUIManagerBinding.cpp index e1d3b3f32e0..31ccd1e68fd 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/react/fabric/FabricUIManagerBinding.cpp +++ b/packages/react-native/ReactAndroid/src/main/jni/react/fabric/FabricUIManagerBinding.cpp @@ -120,6 +120,55 @@ void FabricUIManagerBinding::reportMount(SurfaceId surfaceId) { scheduler->reportMount(surfaceId); } +void FabricUIManagerBinding::pullAndExecuteTransaction(SurfaceId surfaceId) { + TraceSection section("FabricUIManagerBinding::pullAndExecuteTransaction"); + + std::shared_ptr mountingCoordinator; + { + std::shared_lock lock(surfaceHandlerRegistryMutex_); + auto iterator = surfaceHandlerRegistry_.find(surfaceId); + if (iterator == surfaceHandlerRegistry_.end()) { + return; + } + const auto* surfaceHandler = std::get_if(&iterator->second); + jni::local_ref javaSurfaceHandler; + if (surfaceHandler == nullptr) { + javaSurfaceHandler = + std::get>( + iterator->second) + .lockLocal(); + if (javaSurfaceHandler) { + surfaceHandler = &javaSurfaceHandler->cthis()->getSurfaceHandler(); + } + } + if (surfaceHandler != nullptr) { + mountingCoordinator = surfaceHandler->getMountingCoordinator(); + } + } + + if (mountingCoordinator == nullptr) { + return; + } + + auto mountingManager = getMountingManager("pullAndExecuteTransaction"); + if (!mountingManager) { + return; + } + + // The UI thread pulls the transaction itself (it may accumulate several + // revisions committed since the notification was enqueued, and may be empty + // if a previous pull already consumed them). willPerformAsynchronously = + // false: the transaction is applied synchronously right here, so no + // `didPerformAsyncTransactions` bookkeeping is needed. + auto mountingTransaction = + mountingCoordinator->pullTransaction(/* willPerformAsynchronously = */ + false); + if (mountingTransaction.has_value()) { + mountingManager->executeMount( + *mountingTransaction, /* synchronous = */ true); + } +} + #pragma mark - Surface management // Used by bridgeless @@ -678,7 +727,15 @@ void FabricUIManagerBinding::schedulerShouldRenderTransactions( } } - if (ReactNativeFeatureFlags::enableAccumulatedUpdatesInRawPropsAndroid()) { + if (ReactNativeFeatureFlags::enableMountingCoordinatorPullModelAndroid()) { + // Pull model: do NOT pull the transaction or build the batch here (on the + // commit thread). Just notify Java that a transaction is available; the UI + // thread will pull it via a PullTransactionMountItem and call back into + // `pullAndExecuteTransaction`. + mountingManager->onTransactionAvailable( + mountingCoordinator->getSurfaceId()); + } else if (ReactNativeFeatureFlags:: + enableAccumulatedUpdatesInRawPropsAndroid()) { auto mountingTransaction = mountingCoordinator->pullTransaction( /* willPerformAsynchronously = */ true); if (mountingTransaction.has_value()) { @@ -867,6 +924,9 @@ void FabricUIManagerBinding::registerNatives() { "drainPreallocateViewsQueue", FabricUIManagerBinding::drainPreallocateViewsQueue), makeNativeMethod("reportMount", FabricUIManagerBinding::reportMount), + makeNativeMethod( + "pullAndExecuteTransaction", + FabricUIManagerBinding::pullAndExecuteTransaction), makeNativeMethod( "uninstallFabricUIManager", FabricUIManagerBinding::uninstallFabricUIManager), diff --git a/packages/react-native/ReactAndroid/src/main/jni/react/fabric/FabricUIManagerBinding.h b/packages/react-native/ReactAndroid/src/main/jni/react/fabric/FabricUIManagerBinding.h index 60e73000677..ea4b928357e 100644 --- a/packages/react-native/ReactAndroid/src/main/jni/react/fabric/FabricUIManagerBinding.h +++ b/packages/react-native/ReactAndroid/src/main/jni/react/fabric/FabricUIManagerBinding.h @@ -133,6 +133,8 @@ class FabricUIManagerBinding : public jni::HybridClass, void reportMount(SurfaceId surfaceId); + void pullAndExecuteTransaction(SurfaceId surfaceId); + jint findNextFocusableElement(jint parentTag, jint focusedTag, jint direction); jintArray getRelativeAncestorList(jint rootTag, jint childTag); diff --git a/scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api b/scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api index 2f1ede61e22..3b8b47c03d9 100644 --- a/scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api @@ -2370,12 +2370,13 @@ class facebook::react::FabricMountingManager { public void destroyUnmountedShadowNode(const facebook::react::ShadowNodeFamily& family); public void dispatchCommand(const facebook::react::ShadowView& shadowView, const std::string& commandName, const folly::dynamic& args); public void drainPreallocateViewsQueue(); - public void executeMount(const facebook::react::MountingTransaction& transaction); + public void executeMount(const facebook::react::MountingTransaction& transaction, bool synchronous = false); public void maybePreallocateShadowNode(const facebook::react::ShadowNode& shadowNode); public void onAllAnimationsComplete(); public void onAnimationStarted(); public void onSurfaceStart(facebook::react::SurfaceId surfaceId); public void onSurfaceStop(facebook::react::SurfaceId surfaceId); + public void onTransactionAvailable(facebook::react::SurfaceId surfaceId); public void preallocateShadowView(const facebook::react::ShadowView& shadowView); public void scheduleReactRevisionMerge(facebook::react::SurfaceId surfaceId); public void sendAccessibilityEvent(const facebook::react::ShadowView& shadowView, const std::string& eventType); diff --git a/scripts/cxx-api/api-snapshots/ReactAndroidNewarchCxx.api b/scripts/cxx-api/api-snapshots/ReactAndroidNewarchCxx.api index e2e2f1d000c..06aca314362 100644 --- a/scripts/cxx-api/api-snapshots/ReactAndroidNewarchCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAndroidNewarchCxx.api @@ -2353,12 +2353,13 @@ class facebook::react::FabricMountingManager { public void destroyUnmountedShadowNode(const facebook::react::ShadowNodeFamily& family); public void dispatchCommand(const facebook::react::ShadowView& shadowView, const std::string& commandName, const folly::dynamic& args); public void drainPreallocateViewsQueue(); - public void executeMount(const facebook::react::MountingTransaction& transaction); + public void executeMount(const facebook::react::MountingTransaction& transaction, bool synchronous = false); public void maybePreallocateShadowNode(const facebook::react::ShadowNode& shadowNode); public void onAllAnimationsComplete(); public void onAnimationStarted(); public void onSurfaceStart(facebook::react::SurfaceId surfaceId); public void onSurfaceStop(facebook::react::SurfaceId surfaceId); + public void onTransactionAvailable(facebook::react::SurfaceId surfaceId); public void preallocateShadowView(const facebook::react::ShadowView& shadowView); public void scheduleReactRevisionMerge(facebook::react::SurfaceId surfaceId); public void sendAccessibilityEvent(const facebook::react::ShadowView& shadowView, const std::string& eventType); diff --git a/scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api b/scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api index d584b9cd4c2..609ea01a0da 100644 --- a/scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api @@ -2368,12 +2368,13 @@ class facebook::react::FabricMountingManager { public void destroyUnmountedShadowNode(const facebook::react::ShadowNodeFamily& family); public void dispatchCommand(const facebook::react::ShadowView& shadowView, const std::string& commandName, const folly::dynamic& args); public void drainPreallocateViewsQueue(); - public void executeMount(const facebook::react::MountingTransaction& transaction); + public void executeMount(const facebook::react::MountingTransaction& transaction, bool synchronous = false); public void maybePreallocateShadowNode(const facebook::react::ShadowNode& shadowNode); public void onAllAnimationsComplete(); public void onAnimationStarted(); public void onSurfaceStart(facebook::react::SurfaceId surfaceId); public void onSurfaceStop(facebook::react::SurfaceId surfaceId); + public void onTransactionAvailable(facebook::react::SurfaceId surfaceId); public void preallocateShadowView(const facebook::react::ShadowView& shadowView); public void scheduleReactRevisionMerge(facebook::react::SurfaceId surfaceId); public void sendAccessibilityEvent(const facebook::react::ShadowView& shadowView, const std::string& eventType);