Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1118,6 +1118,10 @@ void FabricMountingManager::drainPreallocateViewsQueue() {
std::vector<QueuedView> queuedViews;
std::vector<ShadowView> 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_);
Expand Down Expand Up @@ -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<void()>(
"requestPreallocateViewsDrain");
requestPreallocateViewsDrain(javaUIManager_);
}
}

void FabricMountingManager::preallocateRegisteredView(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#pragma once

#include <atomic>
#include <memory>
#include <mutex>
#include <unordered_map>
Expand Down Expand Up @@ -127,6 +128,13 @@ class FabricMountingManager final {
*/
std::vector<QueuedView> 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<bool> preallocateDrainRequested_{false};

enum class AllocatedViewState { Preallocated, Mounted };

struct AllocatedView {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Loading