Skip to content

Commit 290dae9

Browse files
Andrei Shikovfacebook-github-bot
authored andcommitted
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
1 parent 041398a commit 290dae9

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

ReactAndroid/src/main/java/com/facebook/react/fabric/jni/Binding.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,9 @@ void Binding::installFabricUIManager(
596596
enableEarlyEventEmitterUpdate_ = reactNativeConfig_->getBool(
597597
"react_fabric:enable_early_event_emitter_update");
598598

599+
dispatchPreallocationInBackground_ = reactNativeConfig_->getBool(
600+
"react_native_new_architecture:dispatch_preallocation_in_bg");
601+
599602
auto toolbox = SchedulerToolbox{};
600603
toolbox.contextContainer = contextContainer;
601604
toolbox.componentRegistryFactory = componentsRegistry->buildRegistryFunction;
@@ -1246,7 +1249,14 @@ void Binding::schedulerDidRequestPreliminaryViewAllocation(
12461249
return;
12471250
}
12481251

1249-
preallocateShadowView(surfaceId, shadowView);
1252+
if (dispatchPreallocationInBackground_) {
1253+
auto backgroundExecutor = backgroundExecutor_->get();
1254+
backgroundExecutor([this, surfaceId, shadowView = std::move(shadowView)] {
1255+
preallocateShadowView(surfaceId, shadowView);
1256+
});
1257+
} else {
1258+
preallocateShadowView(surfaceId, shadowView);
1259+
}
12501260
}
12511261

12521262
void Binding::schedulerDidCloneShadowNode(
@@ -1274,7 +1284,14 @@ void Binding::schedulerDidCloneShadowNode(
12741284
if (!oldShadowNode.getTraits().check(ShadowNodeTraits::Trait::FormsView) &&
12751285
newShadowNode.getTraits().check(ShadowNodeTraits::Trait::FormsView)) {
12761286
auto shadowView = ShadowView(newShadowNode);
1277-
preallocateShadowView(surfaceId, shadowView);
1287+
if (dispatchPreallocationInBackground_) {
1288+
auto backgroundExecutor = backgroundExecutor_->get();
1289+
backgroundExecutor([this, surfaceId, shadowView = std::move(shadowView)] {
1290+
preallocateShadowView(surfaceId, shadowView);
1291+
});
1292+
} else {
1293+
preallocateShadowView(surfaceId, shadowView);
1294+
}
12781295
}
12791296
}
12801297

ReactAndroid/src/main/java/com/facebook/react/fabric/jni/Binding.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ class Binding : public jni::HybridClass<Binding>,
201201
bool enableEarlyEventEmitterUpdate_{false};
202202
bool disableRevisionCheckForPreallocation_{false};
203203
bool enableEventEmitterRawPointer_{false};
204+
bool dispatchPreallocationInBackground_{false};
204205
};
205206

206207
} // namespace react

0 commit comments

Comments
 (0)