Skip to content

Commit 5c7a21b

Browse files
hoxyqfacebook-github-bot
authored andcommitted
Replace RuntimeSchedulerClock with HighResTimeStamp, HighResDuration (facebook#51251)
Summary: Pull Request resolved: facebook#51251 # Changelog: [Internal] Replaces `RuntimeSchedulerClock`, which was an alias for `std::chrono::steady_clock` with previously defined single timestamp abstraction. Differential Revision: D74246459 Reviewed By: rubennorte
1 parent 88993d0 commit 5c7a21b

16 files changed

+132
-155
lines changed

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

+10-17
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include <react/renderer/runtimescheduler/RuntimeScheduler.h>
1010
#include <react/renderer/runtimescheduler/RuntimeSchedulerBinding.h>
1111
#include <react/renderer/runtimescheduler/Task.h>
12-
#include <chrono>
12+
#include <react/timing/primitives.h>
1313
#include <utility>
1414

1515
#ifdef RN_DISABLE_OSS_PLUGIN_HEADER
@@ -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"),
@@ -55,13 +55,8 @@ jsi::Function makeTimeRemainingFunction(
5555
expired = true;
5656
} else {
5757
auto now = runtimeScheduler->now();
58-
59-
remainingTime = std::max(
60-
static_cast<double>(
61-
std::chrono::duration_cast<std::chrono::milliseconds>(
62-
deadline - now)
63-
.count()),
64-
0.0);
58+
auto diff = deadline - now;
59+
remainingTime = std::max(diff.toDOMHighResTimeStamp(), 0.0);
6560

6661
if (remainingTime == 0) {
6762
expired = true;
@@ -86,15 +81,13 @@ CallbackHandle NativeIdleCallbacks::requestIdleCallback(
8681
auto runtimeScheduler = binding->getRuntimeScheduler();
8782

8883
// handle timeout parameter
89-
std::optional<RuntimeSchedulerTimeout> timeout;
90-
std::optional<RuntimeSchedulerTimePoint> expirationTime;
84+
std::optional<HighResDuration> timeout;
85+
std::optional<HighResTimeStamp> expirationTime;
9186

9287
if (options.has_value() && options.value().timeout.has_value()) {
93-
auto userTimeout = (options.value().timeout.value());
94-
if (userTimeout > 0) {
95-
timeout = std::chrono::duration_cast<std::chrono::milliseconds>(
96-
std::chrono::duration<double, std::milli>(userTimeout));
97-
expirationTime = runtimeScheduler->now() + timeout.value();
88+
HighResDuration userTimeout = options.value().timeout.value();
89+
if (userTimeout > HighResDuration::zero()) {
90+
expirationTime = runtimeScheduler->now() + userTimeout;
9891
}
9992
}
10093

@@ -110,7 +103,7 @@ CallbackHandle NativeIdleCallbacks::requestIdleCallback(
110103
// we interrupt the current one. The general outcome should be the same.
111104

112105
auto executionStartTime = runtimeScheduler->now();
113-
auto deadline = executionStartTime + std::chrono::milliseconds(50);
106+
auto deadline = executionStartTime + HighResDuration::fromMilliseconds(50);
114107
auto didTimeout = expirationTime.has_value()
115108
? executionStartTime > expirationTime
116109
: false;

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ namespace facebook::react {
2020
using CallbackHandle = jsi::Object;
2121

2222
using NativeRequestIdleCallbackOptions =
23-
NativeIdleCallbacksRequestIdleCallbackOptions<std::optional<double>>;
23+
NativeIdleCallbacksRequestIdleCallbackOptions<
24+
std::optional<HighResDuration>>;
2425

2526
template <>
2627
struct Bridging<NativeRequestIdleCallbackOptions>

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

+5-6
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
#include "RuntimeScheduler.h"
99
#include "RuntimeScheduler_Legacy.h"
1010
#include "RuntimeScheduler_Modern.h"
11-
#include "SchedulerPriorityUtils.h"
1211

1312
#include <cxxreact/ErrorUtils.h>
1413
#include <cxxreact/TraceSection.h>
@@ -22,7 +21,7 @@ extern const char RuntimeSchedulerKey[] = "RuntimeScheduler";
2221
namespace {
2322
std::unique_ptr<RuntimeSchedulerBase> getRuntimeSchedulerImplementation(
2423
RuntimeExecutor runtimeExecutor,
25-
std::function<RuntimeSchedulerTimePoint()> now,
24+
std::function<HighResTimeStamp()> now,
2625
RuntimeSchedulerTaskErrorHandler onTaskError) {
2726
if (ReactNativeFeatureFlags::enableBridgelessArchitecture()) {
2827
return std::make_unique<RuntimeScheduler_Modern>(
@@ -37,7 +36,7 @@ std::unique_ptr<RuntimeSchedulerBase> getRuntimeSchedulerImplementation(
3736

3837
RuntimeScheduler::RuntimeScheduler(
3938
RuntimeExecutor runtimeExecutor,
40-
std::function<RuntimeSchedulerTimePoint()> now,
39+
std::function<HighResTimeStamp()> now,
4140
RuntimeSchedulerTaskErrorHandler onTaskError)
4241
: runtimeSchedulerImpl_(getRuntimeSchedulerImplementation(
4342
std::move(runtimeExecutor),
@@ -68,13 +67,13 @@ std::shared_ptr<Task> RuntimeScheduler::scheduleTask(
6867

6968
std::shared_ptr<Task> RuntimeScheduler::scheduleIdleTask(
7069
jsi::Function&& callback,
71-
RuntimeSchedulerTimeout timeout) noexcept {
70+
HighResDuration timeout) noexcept {
7271
return runtimeSchedulerImpl_->scheduleIdleTask(std::move(callback), timeout);
7372
}
7473

7574
std::shared_ptr<Task> RuntimeScheduler::scheduleIdleTask(
7675
RawCallback&& callback,
77-
RuntimeSchedulerTimeout timeout) noexcept {
76+
HighResDuration timeout) noexcept {
7877
return runtimeSchedulerImpl_->scheduleIdleTask(std::move(callback), timeout);
7978
}
8079

@@ -90,7 +89,7 @@ SchedulerPriority RuntimeScheduler::getCurrentPriorityLevel() const noexcept {
9089
return runtimeSchedulerImpl_->getCurrentPriorityLevel();
9190
}
9291

93-
RuntimeSchedulerTimePoint RuntimeScheduler::now() const noexcept {
92+
HighResTimeStamp RuntimeScheduler::now() const noexcept {
9493
return runtimeSchedulerImpl_->now();
9594
}
9695

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

+8-10
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@
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 {
1919

2020
using RuntimeSchedulerRenderingUpdate = std::function<void()>;
21-
using RuntimeSchedulerTimeout = std::chrono::milliseconds;
2221
using SurfaceId = int32_t;
2322

2423
using RuntimeSchedulerTaskErrorHandler =
@@ -41,16 +40,16 @@ class RuntimeSchedulerBase {
4140
RawCallback&& callback) noexcept = 0;
4241
virtual std::shared_ptr<Task> scheduleIdleTask(
4342
jsi::Function&& callback,
44-
RuntimeSchedulerTimeout timeout = timeoutForSchedulerPriority(
43+
HighResDuration timeout = timeoutForSchedulerPriority(
4544
SchedulerPriority::IdlePriority)) noexcept = 0;
4645
virtual std::shared_ptr<Task> scheduleIdleTask(
4746
RawCallback&& callback,
48-
RuntimeSchedulerTimeout timeout = timeoutForSchedulerPriority(
47+
HighResDuration timeout = timeoutForSchedulerPriority(
4948
SchedulerPriority::IdlePriority)) noexcept = 0;
5049
virtual void cancelTask(Task& task) noexcept = 0;
5150
virtual bool getShouldYield() noexcept = 0;
5251
virtual SchedulerPriority getCurrentPriorityLevel() const noexcept = 0;
53-
virtual RuntimeSchedulerTimePoint now() const noexcept = 0;
52+
virtual HighResTimeStamp now() const noexcept = 0;
5453
virtual void callExpiredTasks(jsi::Runtime& runtime) = 0;
5554
virtual void scheduleRenderingUpdate(
5655
SurfaceId surfaceId,
@@ -69,8 +68,7 @@ class RuntimeScheduler final : RuntimeSchedulerBase {
6968
public:
7069
explicit RuntimeScheduler(
7170
RuntimeExecutor runtimeExecutor,
72-
std::function<RuntimeSchedulerTimePoint()> now =
73-
RuntimeSchedulerClock::now,
71+
std::function<HighResTimeStamp()> now = HighResTimeStamp::now,
7472
RuntimeSchedulerTaskErrorHandler onTaskError = handleTaskErrorDefault);
7573

7674
/*
@@ -112,12 +110,12 @@ class RuntimeScheduler final : RuntimeSchedulerBase {
112110

113111
std::shared_ptr<Task> scheduleIdleTask(
114112
jsi::Function&& callback,
115-
RuntimeSchedulerTimeout timeout = timeoutForSchedulerPriority(
113+
HighResDuration timeout = timeoutForSchedulerPriority(
116114
SchedulerPriority::IdlePriority)) noexcept override;
117115

118116
std::shared_ptr<Task> scheduleIdleTask(
119117
RawCallback&& callback,
120-
RuntimeSchedulerTimeout timeout = timeoutForSchedulerPriority(
118+
HighResDuration timeout = timeoutForSchedulerPriority(
121119
SchedulerPriority::IdlePriority)) noexcept override;
122120

123121
/*
@@ -149,7 +147,7 @@ class RuntimeScheduler final : RuntimeSchedulerBase {
149147
*
150148
* Thread synchronization must be enforced externally.
151149
*/
152-
RuntimeSchedulerTimePoint now() const noexcept override;
150+
HighResTimeStamp now() const noexcept override;
153151

154152
/*
155153
* 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

+4-4
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)),
@@ -84,7 +84,7 @@ std::shared_ptr<Task> RuntimeScheduler_Legacy::scheduleTask(
8484

8585
std::shared_ptr<Task> RuntimeScheduler_Legacy::scheduleIdleTask(
8686
jsi::Function&& /*callback*/,
87-
RuntimeSchedulerTimeout /*timeout*/) noexcept {
87+
HighResDuration /*timeout*/) noexcept {
8888
// Idle tasks are not supported on Legacy RuntimeScheduler.
8989
// Because the method is `noexcept`, we return `nullptr` here and handle it
9090
// on the caller side.
@@ -93,7 +93,7 @@ std::shared_ptr<Task> RuntimeScheduler_Legacy::scheduleIdleTask(
9393

9494
std::shared_ptr<Task> RuntimeScheduler_Legacy::scheduleIdleTask(
9595
RawCallback&& /*callback*/,
96-
RuntimeSchedulerTimeout /*timeout*/) noexcept {
96+
HighResDuration /*timeout*/) noexcept {
9797
// Idle tasks are not supported on Legacy RuntimeScheduler.
9898
// Because the method is `noexcept`, we return `nullptr` here and handle it
9999
// on the caller side.
@@ -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

+5-6
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
/*
@@ -68,7 +67,7 @@ class RuntimeScheduler_Legacy final : public RuntimeSchedulerBase {
6867
*/
6968
std::shared_ptr<Task> scheduleIdleTask(
7069
jsi::Function&& callback,
71-
RuntimeSchedulerTimeout timeout = timeoutForSchedulerPriority(
70+
HighResDuration timeout = timeoutForSchedulerPriority(
7271
SchedulerPriority::IdlePriority)) noexcept override;
7372

7473
/*
@@ -77,7 +76,7 @@ class RuntimeScheduler_Legacy final : public RuntimeSchedulerBase {
7776
*/
7877
std::shared_ptr<Task> scheduleIdleTask(
7978
RawCallback&& callback,
80-
RuntimeSchedulerTimeout timeout = timeoutForSchedulerPriority(
79+
HighResDuration timeout = timeoutForSchedulerPriority(
8180
SchedulerPriority::IdlePriority)) noexcept override;
8281

8382
/*
@@ -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

0 commit comments

Comments
 (0)