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 Facebook GitHub Bot
parent d16f0f297c
commit d35de2d043
2 changed files with 16 additions and 15 deletions
@@ -71,7 +71,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_);
}
void RuntimeScheduler_Modern::cancelTask(Task& task) noexcept {
@@ -105,9 +105,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);
});
@@ -207,7 +206,7 @@ void RuntimeScheduler_Modern::startWorkLoop(
break;
}
executeTask(runtime, topPriorityTask, currentTime);
executeTask(runtime, *topPriorityTask, currentTime);
}
} catch (jsi::JSError& error) {
handleJSError(runtime, error, true);
@@ -245,19 +244,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;
{
ScopedShadowTreeRevisionLock revisionLock(
@@ -275,6 +274,8 @@ void RuntimeScheduler_Modern::executeTask(
updateRendering();
}
}
currentTask_ = nullptr;
}
/**
@@ -296,16 +297,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);
}
}
@@ -135,7 +135,7 @@ class RuntimeScheduler_Modern final : public RuntimeSchedulerBase {
TaskPriorityComparer>
taskQueue_;
std::shared_ptr<Task> currentTask_;
Task* currentTask_{};
/**
* This protects the access to `taskQueue_` and `isWorkLoopScheduled_`.
@@ -162,12 +162,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();