Skip to content

Commit

Permalink
Use RuntimeScheduler in EventBeat
Browse files Browse the repository at this point in the history
Summary:
This will allows us to batch sync events which leads to better performance.

Changelog: [Internal]

Differential Revision: D64631346
  • Loading branch information
lyahdav authored and facebook-github-bot committed Oct 22, 2024
1 parent 3dfe22b commit 733825b
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 39 deletions.
4 changes: 2 additions & 2 deletions packages/react-native/React/Fabric/AppleEventBeat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ namespace facebook::react {
AppleEventBeat::AppleEventBeat(
std::shared_ptr<OwnerBox> ownerBox,
std::unique_ptr<const RunLoopObserver> uiRunLoopObserver,
RuntimeExecutor runtimeExecutor)
: EventBeat(std::move(ownerBox), std::move(runtimeExecutor)),
RuntimeScheduler& runtimeScheduler)
: EventBeat(std::move(ownerBox), runtimeScheduler),
uiRunLoopObserver_(std::move(uiRunLoopObserver)) {
uiRunLoopObserver_->setDelegate(this);
uiRunLoopObserver_->enable();
Expand Down
4 changes: 3 additions & 1 deletion packages/react-native/React/Fabric/AppleEventBeat.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

namespace facebook::react {

class RuntimeScheduler;

/*
* Event beat associated with JavaScript runtime.
* The beat is called on `RuntimeExecutor`'s thread induced by the UI thread
Expand All @@ -23,7 +25,7 @@ class AppleEventBeat : public EventBeat, public RunLoopObserver::Delegate {
AppleEventBeat(
std::shared_ptr<OwnerBox> ownerBox,
std::unique_ptr<const RunLoopObserver> uiRunLoopObserver,
RuntimeExecutor runtimeExecutor);
RuntimeScheduler& RuntimeScheduler);

#pragma mark - RunLoopObserver::Delegate

Expand Down
4 changes: 2 additions & 2 deletions packages/react-native/React/Fabric/RCTSurfacePresenter.mm
Original file line number Diff line number Diff line change
Expand Up @@ -258,10 +258,10 @@ - (RCTScheduler *)_createScheduler
toolbox.bridgelessBindingsExecutor = _bridgelessBindingsExecutor;

toolbox.eventBeatFactory =
[runtimeExecutor](std::shared_ptr<EventBeat::OwnerBox> ownerBox) -> std::unique_ptr<EventBeat> {
[runtimeScheduler](std::shared_ptr<EventBeat::OwnerBox> ownerBox) -> std::unique_ptr<EventBeat> {
auto runLoopObserver =
std::make_unique<const MainRunLoopObserver>(RunLoopObserver::Activity::BeforeWaiting, ownerBox->owner);
return std::make_unique<AppleEventBeat>(std::move(ownerBox), std::move(runLoopObserver), runtimeExecutor);
return std::make_unique<AppleEventBeat>(std::move(ownerBox), std::move(runLoopObserver), *runtimeScheduler);
};

RCTScheduler *scheduler = [[RCTScheduler alloc] initWithToolbox:toolbox];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ namespace facebook::react {
AndroidEventBeat::AndroidEventBeat(
std::shared_ptr<OwnerBox> ownerBox,
EventBeatManager* eventBeatManager,
RuntimeExecutor runtimeExecutor,
RuntimeScheduler& runtimeScheduler,
jni::global_ref<jobject> javaUIManager)
: EventBeat(std::move(ownerBox), std::move(runtimeExecutor)),
: EventBeat(std::move(ownerBox), runtimeScheduler),
eventBeatManager_(eventBeatManager),
javaUIManager_(std::move(javaUIManager)) {
eventBeatManager->addObserver(*this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class AndroidEventBeat final : public EventBeat,
AndroidEventBeat(
std::shared_ptr<OwnerBox> ownerBox,
EventBeatManager* eventBeatManager,
RuntimeExecutor runtimeExecutor,
RuntimeScheduler& runtimeScheduler,
jni::global_ref<jobject> javaUIManager);

~AndroidEventBeat() override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -471,28 +471,25 @@ void FabricUIManagerBinding::installFabricUIManager(

auto runtimeExecutor = runtimeExecutorHolder->cthis()->get();

if (runtimeSchedulerHolder) {
auto runtimeScheduler = runtimeSchedulerHolder->cthis()->get().lock();
if (runtimeScheduler) {
runtimeExecutor =
[runtimeScheduler](
std::function<void(jsi::Runtime & runtime)>&& callback) {
runtimeScheduler->scheduleWork(std::move(callback));
};
contextContainer->insert(
"RuntimeScheduler",
std::weak_ptr<RuntimeScheduler>(runtimeScheduler));
}
auto runtimeScheduler = runtimeSchedulerHolder->cthis()->get().lock();
if (runtimeScheduler) {
runtimeExecutor =
[runtimeScheduler](
std::function<void(jsi::Runtime & runtime)>&& callback) {
runtimeScheduler->scheduleWork(std::move(callback));
};
contextContainer->insert(
"RuntimeScheduler", std::weak_ptr<RuntimeScheduler>(runtimeScheduler));
}

EventBeat::Factory eventBeatFactory =
[eventBeatManager, runtimeExecutor, globalJavaUiManager](
[eventBeatManager, &runtimeScheduler, globalJavaUiManager](
std::shared_ptr<EventBeat::OwnerBox> ownerBox)
-> std::unique_ptr<EventBeat> {
return std::make_unique<AndroidEventBeat>(
std::move(ownerBox),
eventBeatManager,
runtimeExecutor,
*runtimeScheduler,
globalJavaUiManager);
};

Expand Down
29 changes: 15 additions & 14 deletions packages/react-native/ReactCommon/react/renderer/core/EventBeat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@

#include "EventBeat.h"

#include <react/renderer/runtimescheduler/RuntimeScheduler.h>
#include <utility>

namespace facebook::react {

EventBeat::EventBeat(
std::shared_ptr<OwnerBox> ownerBox,
RuntimeExecutor runtimeExecutor)
: ownerBox_(std::move(ownerBox)),
runtimeExecutor_(std::move(runtimeExecutor)) {}
RuntimeScheduler& runtimeScheduler)
: ownerBox_(std::move(ownerBox)), runtimeScheduler_(runtimeScheduler) {}

void EventBeat::request() const {
isRequested_ = true;
Expand All @@ -33,17 +33,18 @@ void EventBeat::induce() const {
isRequested_ = false;
isBeatCallbackScheduled_ = true;

runtimeExecutor_([this, ownerBox = ownerBox_](jsi::Runtime& runtime) {
auto owner = ownerBox->owner.lock();
if (!owner) {
return;
}

isBeatCallbackScheduled_ = false;
if (beatCallback_) {
beatCallback_(runtime);
}
});
runtimeScheduler_.scheduleWork(
[this, ownerBox = ownerBox_](jsi::Runtime& runtime) {
auto owner = ownerBox->owner.lock();
if (!owner) {
return;
}

isBeatCallbackScheduled_ = false;
if (beatCallback_) {
beatCallback_(runtime);
}
});
}

} // namespace facebook::react
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@

#pragma once

#include <ReactCommon/RuntimeExecutor.h>
#include <atomic>
#include <functional>
#include <memory>

namespace facebook::react {
class RuntimeScheduler;
}

namespace facebook::jsi {
class Runtime;
}
Expand Down Expand Up @@ -56,7 +59,7 @@ class EventBeat {

explicit EventBeat(
std::shared_ptr<OwnerBox> ownerBox,
RuntimeExecutor runtimeExecutor);
RuntimeScheduler& runtimeScheduler);

virtual ~EventBeat() = default;

Expand Down Expand Up @@ -88,7 +91,7 @@ class EventBeat {
mutable std::atomic<bool> isRequested_{false};

private:
RuntimeExecutor runtimeExecutor_;
RuntimeScheduler& runtimeScheduler_;
mutable std::atomic<bool> isBeatCallbackScheduled_{false};
};

Expand Down

0 comments on commit 733825b

Please sign in to comment.