Small refactor in RuntimeScheduler_Modern to favor references over shared_ptr for non-owning function args (#43852)

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

Changelog: [internal]

Just a small refactor so we rely less on shared pointers within `RuntimeSCheduler_Modern`.

Reviewed By: javache

Differential Revision: D55646389

fbshipit-source-id: d01dcba7b1551d349d21717ba585828ed7fb3259
This commit is contained in:
Rubén Norte
2024-04-09 07:51:03 -07:00
committed by Thibault Malbranche
parent c67fbebeb7
commit d3cdfa45d0
2 changed files with 16 additions and 15 deletions
@@ -104,7 +104,7 @@ bool RuntimeScheduler_Modern::getShouldYield() const noexcept {
std::shared_lock lock(schedulingMutex_);
return syncTaskRequests_ > 0 ||
(!taskQueue_.empty() && taskQueue_.top() != currentTask_);
(!taskQueue_.empty() && taskQueue_.top().get() != currentTask_);
}
bool RuntimeScheduler_Modern::getIsSynchronous() const noexcept {
@@ -144,9 +144,8 @@ void RuntimeScheduler_Modern::executeNowOnTheSameThread(
auto priority = SchedulerPriority::ImmediatePriority;
auto expirationTime =
currentTime + timeoutForSchedulerPriority(priority);
auto task = std::make_shared<Task>(
priority, std::move(callback), expirationTime);
auto task = Task{priority, std::move(callback), expirationTime};
executeTask(runtime, task, currentTime);
isSynchronous_ = false;
@@ -242,7 +241,7 @@ void RuntimeScheduler_Modern::startWorkLoop(
break;
}
executeTask(runtime, topPriorityTask, currentTime);
executeTask(runtime, *topPriorityTask, currentTime);
}
} catch (jsi::JSError& error) {
handleFatalError(runtime, error);
@@ -280,19 +279,19 @@ std::shared_ptr<Task> RuntimeScheduler_Modern::selectTask(
void RuntimeScheduler_Modern::executeTask(
jsi::Runtime& runtime,
const std::shared_ptr<Task>& task,
Task& task,
RuntimeSchedulerTimePoint currentTime) {
auto didUserCallbackTimeout = task->expirationTime <= currentTime;
auto didUserCallbackTimeout = task.expirationTime <= currentTime;
SystraceSection s(
"RuntimeScheduler::executeTask",
"priority",
serialize(task->priority),
serialize(task.priority),
"didUserCallbackTimeout",
didUserCallbackTimeout);
currentTask_ = task;
currentPriority_ = task->priority;
currentTask_ = &task;
currentPriority_ = task.priority;
executeMacrotask(runtime, task, didUserCallbackTimeout);
@@ -305,6 +304,8 @@ void RuntimeScheduler_Modern::executeTask(
// "Update the rendering" step.
updateRendering();
}
currentTask_ = nullptr;
}
/**
@@ -326,16 +327,16 @@ void RuntimeScheduler_Modern::updateRendering() {
void RuntimeScheduler_Modern::executeMacrotask(
jsi::Runtime& runtime,
std::shared_ptr<Task> task,
Task& task,
bool didUserCallbackTimeout) const {
SystraceSection s("RuntimeScheduler::executeMacrotask");
auto result = task->execute(runtime, didUserCallbackTimeout);
auto result = task.execute(runtime, didUserCallbackTimeout);
if (result.isObject() && result.getObject(runtime).isFunction(runtime)) {
// If the task returned a continuation callback, we re-assign it to the task
// and keep the task in the queue.
task->callback = result.getObject(runtime).getFunction(runtime);
task.callback = result.getObject(runtime).getFunction(runtime);
}
}
@@ -139,7 +139,7 @@ class RuntimeScheduler_Modern final : public RuntimeSchedulerBase {
TaskPriorityComparer>
taskQueue_;
std::shared_ptr<Task> currentTask_;
Task* currentTask_{};
/**
* This protects the access to `taskQueue_` and `isWorkLoopScheduled_`.
@@ -168,12 +168,12 @@ class RuntimeScheduler_Modern final : public RuntimeSchedulerBase {
*/
void executeTask(
jsi::Runtime& runtime,
const std::shared_ptr<Task>& task,
Task& task,
RuntimeSchedulerTimePoint currentTime);
void executeMacrotask(
jsi::Runtime& runtime,
std::shared_ptr<Task> task,
Task& task,
bool didUserCallbackTimeout) const;
void updateRendering();