From 290dae9df56e909f73ea195f872acde0f2c5048d Mon Sep 17 00:00:00 2001 From: Andrei Shikov Date: Tue, 30 Nov 2021 09:14:52 -0800 Subject: [PATCH] Move preallocation calls to background under MC Summary: Preallocation can take 10-20% of time when creating new nodes. (according to systrace measurements). At the moment, we are executing all preallocation calls in JS thread, potentially slowing down the progress there. Moving them to bg thread is a simple micro-optimization that ensures we return the node to JS as soon as possible. Changelog: [Internal] Reviewed By: sammy-SC Differential Revision: D32677843 fbshipit-source-id: 3df47ae9aa365a8db720d71e2423c87659e9128c --- .../com/facebook/react/fabric/jni/Binding.cpp | 21 +++++++++++++++++-- .../com/facebook/react/fabric/jni/Binding.h | 1 + 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/Binding.cpp b/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/Binding.cpp index a5f5236847a..eb9082b854b 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/Binding.cpp +++ b/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/Binding.cpp @@ -596,6 +596,9 @@ void Binding::installFabricUIManager( enableEarlyEventEmitterUpdate_ = reactNativeConfig_->getBool( "react_fabric:enable_early_event_emitter_update"); + dispatchPreallocationInBackground_ = reactNativeConfig_->getBool( + "react_native_new_architecture:dispatch_preallocation_in_bg"); + auto toolbox = SchedulerToolbox{}; toolbox.contextContainer = contextContainer; toolbox.componentRegistryFactory = componentsRegistry->buildRegistryFunction; @@ -1246,7 +1249,14 @@ void Binding::schedulerDidRequestPreliminaryViewAllocation( return; } - preallocateShadowView(surfaceId, shadowView); + if (dispatchPreallocationInBackground_) { + auto backgroundExecutor = backgroundExecutor_->get(); + backgroundExecutor([this, surfaceId, shadowView = std::move(shadowView)] { + preallocateShadowView(surfaceId, shadowView); + }); + } else { + preallocateShadowView(surfaceId, shadowView); + } } void Binding::schedulerDidCloneShadowNode( @@ -1274,7 +1284,14 @@ void Binding::schedulerDidCloneShadowNode( if (!oldShadowNode.getTraits().check(ShadowNodeTraits::Trait::FormsView) && newShadowNode.getTraits().check(ShadowNodeTraits::Trait::FormsView)) { auto shadowView = ShadowView(newShadowNode); - preallocateShadowView(surfaceId, shadowView); + if (dispatchPreallocationInBackground_) { + auto backgroundExecutor = backgroundExecutor_->get(); + backgroundExecutor([this, surfaceId, shadowView = std::move(shadowView)] { + preallocateShadowView(surfaceId, shadowView); + }); + } else { + preallocateShadowView(surfaceId, shadowView); + } } } diff --git a/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/Binding.h b/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/Binding.h index 5f261f34534..b1d646e11ee 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/Binding.h +++ b/ReactAndroid/src/main/java/com/facebook/react/fabric/jni/Binding.h @@ -201,6 +201,7 @@ class Binding : public jni::HybridClass, bool enableEarlyEventEmitterUpdate_{false}; bool disableRevisionCheckForPreallocation_{false}; bool enableEventEmitterRawPointer_{false}; + bool dispatchPreallocationInBackground_{false}; }; } // namespace react