Skip to content

Commit afaa09c

Browse files
hoxyqfacebook-github-bot
authored andcommitted
Replace RuntimeSchedulerClock with HighResTimeClock
Summary: # Changelog: [Internal] Replaces `RuntimeSchedulerClock`, which was an alias for `std::chrono::steady_clock` with previously defined single timestamp abstraction. Differential Revision: D74246459
1 parent 8e2e51e commit afaa09c

15 files changed

+58
-91
lines changed

packages/react-native/ReactCommon/react/nativemodule/idlecallbacks/NativeIdleCallbacks.cpp

+3-7
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class IdleTaskRef : public jsi::NativeState {
3636
jsi::Function makeTimeRemainingFunction(
3737
jsi::Runtime& runtime,
3838
std::shared_ptr<RuntimeScheduler> runtimeScheduler,
39-
RuntimeSchedulerTimePoint deadline) {
39+
HighResTimeStamp deadline) {
4040
return jsi::Function::createFromHostFunction(
4141
runtime,
4242
jsi::PropNameID::forAscii(runtime, "timeRemaining"),
@@ -57,11 +57,7 @@ jsi::Function makeTimeRemainingFunction(
5757
auto now = runtimeScheduler->now();
5858

5959
remainingTime = std::max(
60-
static_cast<double>(
61-
std::chrono::duration_cast<std::chrono::milliseconds>(
62-
deadline - now)
63-
.count()),
64-
0.0);
60+
static_cast<double>((deadline - now).toMilliseconds()), 0.0);
6561

6662
if (remainingTime == 0) {
6763
expired = true;
@@ -87,7 +83,7 @@ CallbackHandle NativeIdleCallbacks::requestIdleCallback(
8783

8884
// handle timeout parameter
8985
std::optional<RuntimeSchedulerTimeout> timeout;
90-
std::optional<RuntimeSchedulerTimePoint> expirationTime;
86+
std::optional<HighResTimeStamp> expirationTime;
9187

9288
if (options.has_value() && options.value().timeout.has_value()) {
9389
auto userTimeout = (options.value().timeout.value());

packages/react-native/ReactCommon/react/nativemodule/idlecallbacks/NativeIdleCallbacks.h

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
#pragma once
99

10+
#include <react/timing/primitives.h>
11+
1012
#if __has_include("rncoreJSI.h") // Cmake headers on Android
1113
#include "rncoreJSI.h"
1214
#elif __has_include("FBReactNativeSpecJSI.h") // CocoaPod headers on Apple

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ extern const char RuntimeSchedulerKey[] = "RuntimeScheduler";
2222
namespace {
2323
std::unique_ptr<RuntimeSchedulerBase> getRuntimeSchedulerImplementation(
2424
RuntimeExecutor runtimeExecutor,
25-
std::function<RuntimeSchedulerTimePoint()> now,
25+
std::function<HighResTimeStamp()> now,
2626
RuntimeSchedulerTaskErrorHandler onTaskError) {
2727
if (ReactNativeFeatureFlags::enableBridgelessArchitecture()) {
2828
return std::make_unique<RuntimeScheduler_Modern>(
@@ -37,7 +37,7 @@ std::unique_ptr<RuntimeSchedulerBase> getRuntimeSchedulerImplementation(
3737

3838
RuntimeScheduler::RuntimeScheduler(
3939
RuntimeExecutor runtimeExecutor,
40-
std::function<RuntimeSchedulerTimePoint()> now,
40+
std::function<HighResTimeStamp()> now,
4141
RuntimeSchedulerTaskErrorHandler onTaskError)
4242
: runtimeSchedulerImpl_(getRuntimeSchedulerImplementation(
4343
std::move(runtimeExecutor),
@@ -90,7 +90,7 @@ SchedulerPriority RuntimeScheduler::getCurrentPriorityLevel() const noexcept {
9090
return runtimeSchedulerImpl_->getCurrentPriorityLevel();
9191
}
9292

93-
RuntimeSchedulerTimePoint RuntimeScheduler::now() const noexcept {
93+
HighResTimeStamp RuntimeScheduler::now() const noexcept {
9494
return runtimeSchedulerImpl_->now();
9595
}
9696

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

+4-5
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
#include <ReactCommon/RuntimeExecutor.h>
1111
#include <react/performance/timeline/PerformanceEntryReporter.h>
1212
#include <react/renderer/consistency/ShadowTreeRevisionConsistencyManager.h>
13-
#include <react/renderer/runtimescheduler/RuntimeSchedulerClock.h>
1413
#include <react/renderer/runtimescheduler/SchedulerPriorityUtils.h>
1514
#include <react/renderer/runtimescheduler/Task.h>
15+
#include <react/timing/primitives.h>
1616
#include "RuntimeSchedulerEventTimingDelegate.h"
1717

1818
namespace facebook::react {
@@ -50,7 +50,7 @@ class RuntimeSchedulerBase {
5050
virtual void cancelTask(Task& task) noexcept = 0;
5151
virtual bool getShouldYield() noexcept = 0;
5252
virtual SchedulerPriority getCurrentPriorityLevel() const noexcept = 0;
53-
virtual RuntimeSchedulerTimePoint now() const noexcept = 0;
53+
virtual HighResTimeStamp now() const noexcept = 0;
5454
virtual void callExpiredTasks(jsi::Runtime& runtime) = 0;
5555
virtual void scheduleRenderingUpdate(
5656
SurfaceId surfaceId,
@@ -69,8 +69,7 @@ class RuntimeScheduler final : RuntimeSchedulerBase {
6969
public:
7070
explicit RuntimeScheduler(
7171
RuntimeExecutor runtimeExecutor,
72-
std::function<RuntimeSchedulerTimePoint()> now =
73-
RuntimeSchedulerClock::now,
72+
std::function<HighResTimeStamp()> now = HighResTimeClock::now,
7473
RuntimeSchedulerTaskErrorHandler onTaskError = handleTaskErrorDefault);
7574

7675
/*
@@ -149,7 +148,7 @@ class RuntimeScheduler final : RuntimeSchedulerBase {
149148
*
150149
* Thread synchronization must be enforced externally.
151150
*/
152-
RuntimeSchedulerTimePoint now() const noexcept override;
151+
HighResTimeStamp now() const noexcept override;
153152

154153
/*
155154
* Expired task is a task that should have been already executed. Designed to

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77

88
#include "RuntimeSchedulerBinding.h"
99
#include <ReactCommon/SchedulerPriority.h>
10+
#include <react/timing/primitives.h>
1011
#include "RuntimeScheduler.h"
1112
#include "SchedulerPriorityUtils.h"
1213
#include "primitives.h"
1314

14-
#include <chrono>
1515
#include <memory>
1616
#include <utility>
1717

@@ -144,10 +144,8 @@ jsi::Value RuntimeSchedulerBinding::get(
144144
const jsi::Value*,
145145
size_t) noexcept -> jsi::Value {
146146
auto now = runtimeScheduler_->now();
147-
auto asDouble =
148-
std::chrono::duration<double, std::milli>(now.time_since_epoch())
149-
.count();
150-
return {asDouble};
147+
auto domHighResTimeStamp = now.toDOMHighResTimeStamp();
148+
return {domHighResTimeStamp};
151149
});
152150
}
153151

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

-22
This file was deleted.

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace facebook::react {
1818

1919
RuntimeScheduler_Legacy::RuntimeScheduler_Legacy(
2020
RuntimeExecutor runtimeExecutor,
21-
std::function<RuntimeSchedulerTimePoint()> now,
21+
std::function<HighResTimeStamp()> now,
2222
RuntimeSchedulerTaskErrorHandler onTaskError)
2323
: runtimeExecutor_(std::move(runtimeExecutor)),
2424
now_(std::move(now)),
@@ -113,7 +113,7 @@ SchedulerPriority RuntimeScheduler_Legacy::getCurrentPriorityLevel()
113113
return currentPriority_;
114114
}
115115

116-
RuntimeSchedulerTimePoint RuntimeScheduler_Legacy::now() const noexcept {
116+
HighResTimeStamp RuntimeScheduler_Legacy::now() const noexcept {
117117
return now_();
118118
}
119119

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include <ReactCommon/RuntimeExecutor.h>
1111
#include <react/renderer/consistency/ShadowTreeRevisionConsistencyManager.h>
1212
#include <react/renderer/runtimescheduler/RuntimeScheduler.h>
13-
#include <react/renderer/runtimescheduler/RuntimeSchedulerClock.h>
1413
#include <react/renderer/runtimescheduler/Task.h>
1514
#include <atomic>
1615
#include <memory>
@@ -22,7 +21,7 @@ class RuntimeScheduler_Legacy final : public RuntimeSchedulerBase {
2221
public:
2322
explicit RuntimeScheduler_Legacy(
2423
RuntimeExecutor runtimeExecutor,
25-
std::function<RuntimeSchedulerTimePoint()> now,
24+
std::function<HighResTimeStamp()> now,
2625
RuntimeSchedulerTaskErrorHandler onTaskError);
2726

2827
/*
@@ -109,7 +108,7 @@ class RuntimeScheduler_Legacy final : public RuntimeSchedulerBase {
109108
*
110109
* Thread synchronization must be enforced externally.
111110
*/
112-
RuntimeSchedulerTimePoint now() const noexcept override;
111+
HighResTimeStamp now() const noexcept override;
113112

114113
/*
115114
* Expired task is a task that should have been already executed. Designed to
@@ -169,7 +168,7 @@ class RuntimeScheduler_Legacy final : public RuntimeSchedulerBase {
169168
* Returns a time point representing the current point in time. May be called
170169
* from multiple threads.
171170
*/
172-
std::function<RuntimeSchedulerTimePoint()> now_;
171+
std::function<HighResTimeStamp()> now_;
173172

174173
/*
175174
* Flag indicating if callback on JavaScript queue has been

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

+10-10
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ std::chrono::milliseconds getResolvedTimeoutForIdleTask(
3333

3434
RuntimeScheduler_Modern::RuntimeScheduler_Modern(
3535
RuntimeExecutor runtimeExecutor,
36-
std::function<RuntimeSchedulerTimePoint()> now,
36+
std::function<HighResTimeStamp()> now,
3737
RuntimeSchedulerTaskErrorHandler onTaskError)
3838
: runtimeExecutor_(std::move(runtimeExecutor)),
3939
now_(std::move(now)),
@@ -139,7 +139,7 @@ SchedulerPriority RuntimeScheduler_Modern::getCurrentPriorityLevel()
139139
return currentPriority_;
140140
}
141141

142-
RuntimeSchedulerTimePoint RuntimeScheduler_Modern::now() const noexcept {
142+
HighResTimeStamp RuntimeScheduler_Modern::now() const noexcept {
143143
return now_();
144144
}
145145

@@ -276,7 +276,7 @@ void RuntimeScheduler_Modern::runEventLoop(
276276
}
277277

278278
std::shared_ptr<Task> RuntimeScheduler_Modern::selectTask(
279-
RuntimeSchedulerTimePoint currentTime,
279+
HighResTimeStamp currentTime,
280280
bool onlyExpired) {
281281
// We need a unique lock here because we'll also remove executed tasks from
282282
// the top of the queue.
@@ -305,7 +305,7 @@ std::shared_ptr<Task> RuntimeScheduler_Modern::selectTask(
305305
void RuntimeScheduler_Modern::runEventLoopTick(
306306
jsi::Runtime& runtime,
307307
Task& task,
308-
RuntimeSchedulerTimePoint taskStartTime) {
308+
HighResTimeStamp taskStartTime) {
309309
TraceSection s("RuntimeScheduler::runEventLoopTick");
310310
jsinspector_modern::tracing::EventLoopReporter performanceReporter(
311311
jsinspector_modern::tracing::EventLoopPhase::Task);
@@ -434,24 +434,24 @@ void RuntimeScheduler_Modern::performMicrotaskCheckpoint(
434434

435435
void RuntimeScheduler_Modern::reportLongTasks(
436436
const Task& /*task*/,
437-
RuntimeSchedulerTimePoint startTime,
438-
RuntimeSchedulerTimePoint endTime) {
437+
HighResTimeStamp startTime,
438+
HighResTimeStamp endTime) {
439439
auto reporter = performanceEntryReporter_;
440440
if (reporter == nullptr) {
441441
return;
442442
}
443443

444444
auto checkedDurationMs =
445-
chronoToDOMHighResTimeStamp(longestPeriodWithoutYieldingOpportunity_);
445+
longestPeriodWithoutYieldingOpportunity_.toDOMHighResTimeStamp();
446446
if (checkedDurationMs >= LONG_TASK_DURATION_THRESHOLD_MS) {
447-
auto durationMs = chronoToDOMHighResTimeStamp(endTime - startTime);
448-
auto startTimeMs = chronoToDOMHighResTimeStamp(startTime);
447+
auto durationMs = (endTime - startTime).toDOMHighResTimeStamp();
448+
auto startTimeMs = startTime.toDOMHighResTimeStamp();
449449
reporter->reportLongTask(startTimeMs, durationMs);
450450
}
451451
}
452452

453453
void RuntimeScheduler_Modern::markYieldingOpportunity(
454-
RuntimeSchedulerTimePoint currentTime) {
454+
HighResTimeStamp currentTime) {
455455
auto currentPeriod = currentTime - lastYieldingOpportunity_;
456456
if (currentPeriod > longestPeriodWithoutYieldingOpportunity_) {
457457
longestPeriodWithoutYieldingOpportunity_ = currentPeriod;

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

+10-11
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include <ReactCommon/RuntimeExecutor.h>
1111
#include <react/renderer/consistency/ShadowTreeRevisionConsistencyManager.h>
1212
#include <react/renderer/runtimescheduler/RuntimeScheduler.h>
13-
#include <react/renderer/runtimescheduler/RuntimeSchedulerClock.h>
1413
#include <react/renderer/runtimescheduler/Task.h>
1514
#include <atomic>
1615
#include <memory>
@@ -23,7 +22,7 @@ class RuntimeScheduler_Modern final : public RuntimeSchedulerBase {
2322
public:
2423
explicit RuntimeScheduler_Modern(
2524
RuntimeExecutor runtimeExecutor,
26-
std::function<RuntimeSchedulerTimePoint()> now,
25+
std::function<HighResTimeStamp()> now,
2726
RuntimeSchedulerTaskErrorHandler onTaskError);
2827

2928
/*
@@ -118,7 +117,7 @@ class RuntimeScheduler_Modern final : public RuntimeSchedulerBase {
118117
*
119118
* Can be called from any thread.
120119
*/
121-
RuntimeSchedulerTimePoint now() const noexcept override;
120+
HighResTimeStamp now() const noexcept override;
122121

123122
/*
124123
* Expired task is a task that should have been already executed. Designed to
@@ -162,10 +161,10 @@ class RuntimeScheduler_Modern final : public RuntimeSchedulerBase {
162161
taskQueue_;
163162

164163
Task* currentTask_{};
165-
RuntimeSchedulerTimePoint lastYieldingOpportunity_;
166-
RuntimeSchedulerDuration longestPeriodWithoutYieldingOpportunity_{};
164+
HighResTimeStamp lastYieldingOpportunity_;
165+
HighResTimeDuration longestPeriodWithoutYieldingOpportunity_{};
167166

168-
void markYieldingOpportunity(RuntimeSchedulerTimePoint currentTime);
167+
void markYieldingOpportunity(HighResTimeStamp currentTime);
169168

170169
/**
171170
* This protects the access to `taskQueue_` and `isevent loopScheduled_`.
@@ -179,7 +178,7 @@ class RuntimeScheduler_Modern final : public RuntimeSchedulerBase {
179178
void runEventLoop(jsi::Runtime& runtime, bool onlyExpired);
180179

181180
std::shared_ptr<Task> selectTask(
182-
RuntimeSchedulerTimePoint currentTime,
181+
HighResTimeStamp currentTime,
183182
bool onlyExpired);
184183

185184
void scheduleTask(std::shared_ptr<Task> task);
@@ -193,7 +192,7 @@ class RuntimeScheduler_Modern final : public RuntimeSchedulerBase {
193192
void runEventLoopTick(
194193
jsi::Runtime& runtime,
195194
Task& task,
196-
RuntimeSchedulerTimePoint currentTime);
195+
HighResTimeStamp taskStartTime);
197196

198197
void executeTask(
199198
jsi::Runtime& runtime,
@@ -207,14 +206,14 @@ class RuntimeScheduler_Modern final : public RuntimeSchedulerBase {
207206

208207
void reportLongTasks(
209208
const Task& task,
210-
RuntimeSchedulerTimePoint startTime,
211-
RuntimeSchedulerTimePoint endTime);
209+
HighResTimeStamp startTime,
210+
HighResTimeStamp endTime);
212211

213212
/*
214213
* Returns a time point representing the current point in time. May be called
215214
* from multiple threads.
216215
*/
217-
std::function<RuntimeSchedulerTimePoint()> now_;
216+
std::function<HighResTimeStamp()> now_;
218217

219218
/*
220219
* Flag indicating if callback on JavaScript queue has been

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ namespace facebook::react {
1212
Task::Task(
1313
SchedulerPriority priority,
1414
jsi::Function&& callback,
15-
std::chrono::steady_clock::time_point expirationTime)
15+
HighResTimeStamp expirationTime)
1616
: priority(priority),
1717
callback(std::move(callback)),
1818
expirationTime(expirationTime) {}
1919

2020
Task::Task(
2121
SchedulerPriority priority,
2222
RawCallback&& callback,
23-
std::chrono::steady_clock::time_point expirationTime)
23+
HighResTimeStamp expirationTime)
2424
: priority(priority),
2525
callback(std::move(callback)),
2626
expirationTime(expirationTime) {}

0 commit comments

Comments
 (0)