Skip to content

Commit 37b33c3

Browse files
rubennorteTitozzz
authored andcommitted
Small refactor in RuntimeScheduler_Modern to favor references over shared_ptr for non-owning function args (#43852)
Summary: Pull Request resolved: #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
1 parent 988bf16 commit 37b33c3

2 files changed

Lines changed: 16 additions & 15 deletions

File tree

packages/react-native/ReactCommon/react/renderer/runtimescheduler/RuntimeScheduler_Modern.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ bool RuntimeScheduler_Modern::getShouldYield() const noexcept {
104104
std::shared_lock lock(schedulingMutex_);
105105

106106
return syncTaskRequests_ > 0 ||
107-
(!taskQueue_.empty() && taskQueue_.top() != currentTask_);
107+
(!taskQueue_.empty() && taskQueue_.top().get() != currentTask_);
108108
}
109109

110110
bool RuntimeScheduler_Modern::getIsSynchronous() const noexcept {
@@ -144,9 +144,8 @@ void RuntimeScheduler_Modern::executeNowOnTheSameThread(
144144
auto priority = SchedulerPriority::ImmediatePriority;
145145
auto expirationTime =
146146
currentTime + timeoutForSchedulerPriority(priority);
147-
auto task = std::make_shared<Task>(
148-
priority, std::move(callback), expirationTime);
149147

148+
auto task = Task{priority, std::move(callback), expirationTime};
150149
executeTask(runtime, task, currentTime);
151150

152151
isSynchronous_ = false;
@@ -242,7 +241,7 @@ void RuntimeScheduler_Modern::startWorkLoop(
242241
break;
243242
}
244243

245-
executeTask(runtime, topPriorityTask, currentTime);
244+
executeTask(runtime, *topPriorityTask, currentTime);
246245
}
247246
} catch (jsi::JSError& error) {
248247
handleFatalError(runtime, error);
@@ -280,19 +279,19 @@ std::shared_ptr<Task> RuntimeScheduler_Modern::selectTask(
280279

281280
void RuntimeScheduler_Modern::executeTask(
282281
jsi::Runtime& runtime,
283-
const std::shared_ptr<Task>& task,
282+
Task& task,
284283
RuntimeSchedulerTimePoint currentTime) {
285-
auto didUserCallbackTimeout = task->expirationTime <= currentTime;
284+
auto didUserCallbackTimeout = task.expirationTime <= currentTime;
286285

287286
SystraceSection s(
288287
"RuntimeScheduler::executeTask",
289288
"priority",
290-
serialize(task->priority),
289+
serialize(task.priority),
291290
"didUserCallbackTimeout",
292291
didUserCallbackTimeout);
293292

294-
currentTask_ = task;
295-
currentPriority_ = task->priority;
293+
currentTask_ = &task;
294+
currentPriority_ = task.priority;
296295

297296
executeMacrotask(runtime, task, didUserCallbackTimeout);
298297

@@ -305,6 +304,8 @@ void RuntimeScheduler_Modern::executeTask(
305304
// "Update the rendering" step.
306305
updateRendering();
307306
}
307+
308+
currentTask_ = nullptr;
308309
}
309310

310311
/**
@@ -326,16 +327,16 @@ void RuntimeScheduler_Modern::updateRendering() {
326327

327328
void RuntimeScheduler_Modern::executeMacrotask(
328329
jsi::Runtime& runtime,
329-
std::shared_ptr<Task> task,
330+
Task& task,
330331
bool didUserCallbackTimeout) const {
331332
SystraceSection s("RuntimeScheduler::executeMacrotask");
332333

333-
auto result = task->execute(runtime, didUserCallbackTimeout);
334+
auto result = task.execute(runtime, didUserCallbackTimeout);
334335

335336
if (result.isObject() && result.getObject(runtime).isFunction(runtime)) {
336337
// If the task returned a continuation callback, we re-assign it to the task
337338
// and keep the task in the queue.
338-
task->callback = result.getObject(runtime).getFunction(runtime);
339+
task.callback = result.getObject(runtime).getFunction(runtime);
339340
}
340341
}
341342

packages/react-native/ReactCommon/react/renderer/runtimescheduler/RuntimeScheduler_Modern.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ class RuntimeScheduler_Modern final : public RuntimeSchedulerBase {
139139
TaskPriorityComparer>
140140
taskQueue_;
141141

142-
std::shared_ptr<Task> currentTask_;
142+
Task* currentTask_{};
143143

144144
/**
145145
* This protects the access to `taskQueue_` and `isWorkLoopScheduled_`.
@@ -168,12 +168,12 @@ class RuntimeScheduler_Modern final : public RuntimeSchedulerBase {
168168
*/
169169
void executeTask(
170170
jsi::Runtime& runtime,
171-
const std::shared_ptr<Task>& task,
171+
Task& task,
172172
RuntimeSchedulerTimePoint currentTime);
173173

174174
void executeMacrotask(
175175
jsi::Runtime& runtime,
176-
std::shared_ptr<Task> task,
176+
Task& task,
177177
bool didUserCallbackTimeout) const;
178178

179179
void updateRendering();

0 commit comments

Comments
 (0)