Prevent View Preallocation (#45163)

Summary:
Pull Request resolved: https://github.com/facebook/react-native/pull/45163

Avoid view preallocation when rendering on the main thread

Changelog: [Internal]

Reviewed By: rubennorte

Differential Revision: D58833983

fbshipit-source-id: a942d1fac684be5a8073941dbf043ba1d738e3a0
This commit is contained in:
Thomas Nardone
2024-07-02 14:16:32 -07:00
committed by Facebook GitHub Bot
parent d99960744c
commit 0ba2e9adf2
4 changed files with 25 additions and 3 deletions
@@ -764,6 +764,14 @@ public class FabricUIManager
isLayoutable));
}
@SuppressLint("NotInvokedPrivateMethod")
@SuppressWarnings("unused")
@AnyThread
@ThreadConfined(ANY)
private boolean isOnMainThread() {
return UiThreadUtil.isOnUiThread();
}
@SuppressWarnings("unused")
@AnyThread
@ThreadConfined(ANY)
@@ -513,7 +513,7 @@ void Binding::schedulerDidRequestPreliminaryViewAllocation(
if (!mountingManager) {
return;
}
mountingManager->preallocateShadowView(shadowNode);
mountingManager->maybePreallocateShadowView(shadowNode);
}
void Binding::schedulerDidDispatchCommand(
@@ -784,11 +784,16 @@ void FabricMountingManager::executeMount(
env->DeleteLocalRef(intBufferArray);
}
void FabricMountingManager::preallocateShadowView(
void FabricMountingManager::maybePreallocateShadowView(
const ShadowNode& shadowNode) {
if (!shadowNode.getTraits().check(ShadowNodeTraits::Trait::FormsView)) {
return;
}
static thread_local bool onMainThread = isOnMainThread();
if (onMainThread) {
// View preallocation is not beneficial when rendering on the main thread
return;
}
SystraceSection section("FabricMountingManager::preallocateShadowView");
@@ -844,6 +849,13 @@ void FabricMountingManager::preallocateShadowView(
isLayoutableShadowNode);
}
bool FabricMountingManager::isOnMainThread() {
static auto isOnMainThread =
JFabricUIManager::javaClassStatic()->getMethod<jboolean()>(
"isOnMainThread");
return isOnMainThread(javaUIManager_);
}
void FabricMountingManager::dispatchCommand(
const ShadowView& shadowView,
const std::string& commandName,
@@ -32,7 +32,7 @@ class FabricMountingManager final {
void onSurfaceStop(SurfaceId surfaceId);
void preallocateShadowView(const ShadowNode& shadowNode);
void maybePreallocateShadowView(const ShadowNode& shadowNode);
void executeMount(const MountingTransaction& transaction);
@@ -55,6 +55,8 @@ class FabricMountingManager final {
void onAllAnimationsComplete();
private:
bool isOnMainThread();
jni::global_ref<JFabricUIManager::javaobject> javaUIManager_;
std::recursive_mutex commitMutex_;