Skip to content

Commit 88fe26d

Browse files
sammy-SCfacebook-github-bot
authored andcommitted
Make measure calls asynchronous
Summary: Changelog: [internal] Add an option to make all measure calls asynchronous. Reviewed By: JoshuaGross Differential Revision: D28934444 fbshipit-source-id: 57a320b03add0182b4646b13ed4b692b899ddea3
1 parent 6b601db commit 88fe26d

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

ReactCommon/react/renderer/scheduler/Scheduler.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,23 @@ Scheduler::Scheduler(
9595
uiManager->setDelegate(this);
9696
uiManager->setComponentDescriptorRegistry(componentDescriptorRegistry_);
9797

98+
#ifdef ANDROID
99+
auto asyncMeasure =
100+
reactNativeConfig_->getBool("react_fabric:enable_async_measure_android");
101+
#else
102+
auto asyncMeasure =
103+
reactNativeConfig_->getBool("react_fabric:enable_async_measure_ios");
104+
#endif
105+
98106
runtimeExecutor_([uiManager,
107+
asyncMeasure,
108+
runtimeExecutor = runtimeExecutor_,
99109
runtimeScheduler = schedulerToolbox.runtimeScheduler](
100110
jsi::Runtime &runtime) {
101-
auto uiManagerBinding = UIManagerBinding::createAndInstallIfNeeded(runtime);
111+
auto uiManagerBinding =
112+
UIManagerBinding::createAndInstallIfNeeded(runtime, runtimeExecutor);
102113
uiManagerBinding->attach(uiManager);
114+
uiManagerBinding->setEnableAsyncMeasure(asyncMeasure);
103115
if (runtimeScheduler) {
104116
RuntimeSchedulerBinding::createAndInstallIfNeeded(
105117
runtime, runtimeScheduler);

ReactCommon/react/renderer/uimanager/UIManagerBinding.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,16 @@ static bool checkGetCallableModuleIsActive(jsi::Runtime &runtime) {
5858
}
5959

6060
std::shared_ptr<UIManagerBinding> UIManagerBinding::createAndInstallIfNeeded(
61-
jsi::Runtime &runtime) {
61+
jsi::Runtime &runtime,
62+
RuntimeExecutor const &runtimeExecutor) {
6263
auto uiManagerModuleName = "nativeFabricUIManager";
6364

6465
auto uiManagerValue =
6566
runtime.global().getProperty(runtime, uiManagerModuleName);
6667
if (uiManagerValue.isUndefined()) {
6768
// The global namespace does not have an instance of the binding;
6869
// we need to create, install and return it.
69-
auto uiManagerBinding = std::make_shared<UIManagerBinding>();
70+
auto uiManagerBinding = std::make_shared<UIManagerBinding>(runtimeExecutor);
7071
auto object = jsi::Object::createFromHostObject(runtime, uiManagerBinding);
7172
runtime.global().setProperty(
7273
runtime, uiManagerModuleName, std::move(object));
@@ -93,6 +94,9 @@ std::shared_ptr<UIManagerBinding> UIManagerBinding::getBinding(
9394
return uiManagerObject.getHostObject<UIManagerBinding>(runtime);
9495
}
9596

97+
UIManagerBinding::UIManagerBinding(RuntimeExecutor const &runtimeExecutor)
98+
: runtimeExecutor_(runtimeExecutor) {}
99+
96100
UIManagerBinding::~UIManagerBinding() {
97101
LOG(WARNING) << "UIManagerBinding::~UIManagerBinding() was called (address: "
98102
<< this << ").";
@@ -102,6 +106,10 @@ void UIManagerBinding::attach(std::shared_ptr<UIManager> const &uiManager) {
102106
uiManager_ = uiManager;
103107
}
104108

109+
void UIManagerBinding::setEnableAsyncMeasure(bool enable) {
110+
enableAsyncMeasure_ = enable;
111+
}
112+
105113
static jsi::Value callMethodOfModule(
106114
jsi::Runtime &runtime,
107115
std::string const &moduleName,
@@ -826,8 +834,12 @@ jsi::Value UIManagerBinding::get(
826834

827835
void UIManagerBinding::executeMeasure(
828836
jsi::Runtime &runtime,
829-
std::function<void(jsi::Runtime &)> callback) const noexcept {
830-
callback(runtime);
837+
std::function<void(jsi::Runtime &)> &&callback) const noexcept {
838+
if (enableAsyncMeasure_) {
839+
runtimeExecutor_(std::move(callback));
840+
} else {
841+
callback(runtime);
842+
}
831843
}
832844

833845
} // namespace facebook::react

ReactCommon/react/renderer/uimanager/UIManagerBinding.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#pragma once
99

10+
#import <ReactCommon/RuntimeExecutor.h>
1011
#include <folly/dynamic.h>
1112
#include <jsi/jsi.h>
1213
#include <react/renderer/core/RawValue.h>
@@ -28,14 +29,17 @@ class UIManagerBinding : public jsi::HostObject {
2829
* Thread synchronization must be enforced externally.
2930
*/
3031
static std::shared_ptr<UIManagerBinding> createAndInstallIfNeeded(
31-
jsi::Runtime &runtime);
32+
jsi::Runtime &runtime,
33+
RuntimeExecutor const &runtimeExecutor);
3234

3335
/*
3436
* Returns a pointer to UIManagerBinding previously installed into a runtime.
3537
* Thread synchronization must be enforced externally.
3638
*/
3739
static std::shared_ptr<UIManagerBinding> getBinding(jsi::Runtime &runtime);
3840

41+
UIManagerBinding(RuntimeExecutor const &runtimeExecutor);
42+
3943
~UIManagerBinding();
4044

4145
/*
@@ -45,6 +49,8 @@ class UIManagerBinding : public jsi::HostObject {
4549
*/
4650
void attach(std::shared_ptr<UIManager> const &uiManager);
4751

52+
void setEnableAsyncMeasure(bool enable);
53+
4854
/*
4955
* Starts React Native Surface with given id, moduleName, and props.
5056
* Thread synchronization must be enforced externally.
@@ -102,11 +108,14 @@ class UIManagerBinding : public jsi::HostObject {
102108
private:
103109
void executeMeasure(
104110
jsi::Runtime &runtime,
105-
std::function<void(jsi::Runtime &)> callback) const noexcept;
111+
std::function<void(jsi::Runtime &)> &&callback) const noexcept;
106112

107113
std::shared_ptr<UIManager> uiManager_;
108114
std::unique_ptr<EventHandler const> eventHandler_;
109115
mutable ReactEventPriority currentEventPriority_;
116+
117+
bool enableAsyncMeasure_;
118+
RuntimeExecutor runtimeExecutor_;
110119
};
111120

112121
} // namespace facebook::react

0 commit comments

Comments
 (0)