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 2727e880d083..3cf991ccf821 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 @@ -940,6 +940,33 @@ private void preallocateView( isLayoutable)); } + /** + * Called from C++ when a view is queued for preallocation and no drain has been requested yet. + * The frame callback drains the queue once per frame; this drains it between frames too, so + * that the views of a render that commits before the next frame are still created ahead of + * their mount. + */ + @SuppressLint("NotInvokedPrivateMethod") // Called from C++ via JNI + @SuppressWarnings("unused") + @AnyThread + @ThreadConfined(ANY) + private void requestPreallocateViewsDrain() { + UiThreadUtil.runOnUiThread(mDrainPreallocateViewsRunnable); + } + + private final Runnable mDrainPreallocateViewsRunnable = + new Runnable() { + @Override + public void run() { + FabricUIManagerBinding binding = mBinding; + if (mDestroyed || binding == null) { + return; + } + binding.drainPreallocateViewsQueue(); + mMountItemDispatcher.dispatchPreMountItemsBetweenFrames(); + } + }; + @SuppressLint("NotInvokedPrivateMethod") // Called from C++ via JNI @SuppressWarnings("unused") @AnyThread diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/MountItemDispatcher.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/MountItemDispatcher.kt index e2068b372e12..033566f0d020 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/MountItemDispatcher.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/fabric/mounting/MountItemDispatcher.kt @@ -372,6 +372,20 @@ internal class MountItemDispatcher( dispatchPreMountItemsImpl(deadline) } + /** + * Executes pre-mount items outside of a frame callback, in what is left of the budget of the + * last frame. Items that do not fit stay queued for the next frame callback. + */ + @UiThread + @ThreadConfined(UI) + fun dispatchPreMountItemsBetweenFrames() { + if (preMountItems.isEmpty()) { + return + } + + dispatchPreMountItemsImpl(lastFrameTimeNanos + FRAME_TIME_NS / 2) + } + private fun dispatchPreMountItemsImpl(deadline: Long) { Systrace.beginSection(Systrace.TRACE_TAG_REACT, "MountItemDispatcher::premountViews") 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 c20cefc00913..df56873c2f1d 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 @@ -1118,6 +1118,10 @@ void FabricMountingManager::drainPreallocateViewsQueue() { std::vector queuedViews; std::vector shadowViews; + // Cleared before the queue is taken: a view queued from here on requests a + // drain of its own, which may find the queue empty but never misses a view. + preallocateDrainRequested_.store(false); + { std::lock_guard lock(preallocateMutex_); std::swap(queuedViews, preallocatedViewsQueue_); @@ -1232,6 +1236,16 @@ void FabricMountingManager::maybePreallocateShadowNode( {.shadowView = std::move(shadowView), .family = shadowNode.getFamilyShared()}); } + + // The frame callback drains the queue once per frame. A render that commits + // before the next frame would get no views preallocated at all, so the UI + // thread is also asked to drain as soon as there is something queued. + if (!preallocateDrainRequested_.exchange(true)) { + static auto requestPreallocateViewsDrain = + JFabricUIManager::javaClassStatic()->getMethod( + "requestPreallocateViewsDrain"); + requestPreallocateViewsDrain(javaUIManager_); + } } void FabricMountingManager::preallocateRegisteredView( 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 b835826de4d1..0a229148b040 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 @@ -7,6 +7,7 @@ #pragma once +#include #include #include #include @@ -127,6 +128,13 @@ class FabricMountingManager final { */ std::vector preallocatedViewsQueue_{}; + /* + * True from the moment a drain of preallocatedViewsQueue_ has been requested + * on the UI thread until a drain starts, so that queueing many views asks + * for one drain, with one JNI call. + */ + std::atomic preallocateDrainRequested_{false}; + enum class AllocatedViewState { Preallocated, Mounted }; struct AllocatedView { diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/fabric/mounting/MountItemDispatcherTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/fabric/mounting/MountItemDispatcherTest.kt index ebc5c9349dae..dea52d2741ac 100644 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/fabric/mounting/MountItemDispatcherTest.kt +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/fabric/mounting/MountItemDispatcherTest.kt @@ -141,6 +141,37 @@ class MountItemDispatcherTest { assertThat(followUpItem.executed).isTrue() } + /** + * A render that commits before the next frame callback never meets one, so its views can only be + * preallocated between frames, in what is left of the last frame's budget. + */ + @Test + fun dispatchPreMountItemsBetweenFrames_withinLastFrameBudget_executesPreMountItem() { + val item = RecordingMountItem() + dispatcher.dispatchPreMountItems(System.nanoTime()) + + dispatcher.addPreAllocateMountItem(item) + dispatcher.dispatchPreMountItemsBetweenFrames() + + assertThat(item.executed).isTrue() + } + + @Test + fun dispatchPreMountItemsBetweenFrames_pastLastFrameBudget_leavesItemForNextFrame() { + val item = RecordingMountItem() + val oneSecondNanos = 1_000_000_000L + dispatcher.dispatchPreMountItems(System.nanoTime() - oneSecondNanos) + + dispatcher.addPreAllocateMountItem(item) + dispatcher.dispatchPreMountItemsBetweenFrames() + + assertThat(item.executed).isFalse() + + dispatcher.dispatchPreMountItems(System.nanoTime()) + + assertThat(item.executed).isTrue() + } + private fun enableFollowUpMountItemDispatch() { ReactNativeFeatureFlags.override( object : ReactNativeFeatureFlagsDefaults() {