Skip to content

Commit 579f821

Browse files
hoxyqhannojg
authored andcommitted
Replace DOMHighResTimeStamp alias in ReactCommon with new abstractions (facebook#51512)
Summary: Pull Request resolved: facebook#51512 Pull Request resolved: facebook#50585 Replaces `DOMHighResTimeStamp` alias completely in `ReactCommon` with `HighResTimeStamp`. `DOMHighResTimeStamp` as a type is now expected to be used only in JavaScript. I didn't update places where we explcitly use `std::chrono::high_resolution_clock`, since it is platform-specific and there is no guarantee that `std::chrono::high_resolution_clock` == `std::chrono::steady_clock`. Also, places that are isolated and not part of the Web Performance APIs, such as Telemetry for Fabric, are not updates as part of this diff. Although these subsystems are also using `std::chrono::steady_clock` as a low-level representation, they are not sharing it with other parts of the React Native core. Reviewed By: rubennorte Differential Revision: D75185613 fbshipit-source-id: 889719368de163e6f529689df6cc16d816fde66c
1 parent 37b7d48 commit 579f821

38 files changed

+1021
-323
lines changed

packages/react-native/ReactCommon/cxxreact/JSExecutor.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
1111

1212
#include <folly/Conv.h>
1313
#include <jsinspector-modern/ReactCdp.h>
14-
#include <react/timing/primitives.h>
1514

16-
#include <chrono>
15+
#include <array>
1716

1817
namespace facebook::react {
1918

@@ -26,8 +25,8 @@ std::string JSExecutor::getSyntheticBundlePath(
2625
return folly::to<std::string>("seg-", bundleId, ".js");
2726
}
2827

29-
double JSExecutor::performanceNow() {
30-
return chronoToDOMHighResTimeStamp(std::chrono::steady_clock::now());
28+
HighResTimeStamp JSExecutor::performanceNow() {
29+
return HighResTimeStamp::now();
3130
}
3231

3332
jsinspector_modern::RuntimeTargetDelegate&

packages/react-native/ReactCommon/cxxreact/JSExecutor.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <folly/dynamic.h>
1515
#include <jsinspector-modern/InspectorInterfaces.h>
1616
#include <jsinspector-modern/ReactCdp.h>
17+
#include <react/timing/primitives.h>
1718

1819
#ifndef RN_EXPORT
1920
#define RN_EXPORT __attribute__((visibility("default")))
@@ -138,7 +139,7 @@ class RN_EXPORT JSExecutor {
138139
uint32_t bundleId,
139140
const std::string& bundlePath);
140141

141-
static double performanceNow();
142+
static HighResTimeStamp performanceNow();
142143

143144
/**
144145
* Get a reference to the \c RuntimeTargetDelegate owned (or implemented) by

packages/react-native/ReactCommon/jsiexecutor/jsireact/JSIExecutor.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,9 @@ void bindNativePerformanceNow(Runtime& runtime) {
557557
[](jsi::Runtime& runtime,
558558
const jsi::Value&,
559559
const jsi::Value* args,
560-
size_t count) { return Value(JSExecutor::performanceNow()); }));
560+
size_t /*count*/) {
561+
return JSExecutor::performanceNow().toDOMHighResTimeStamp();
562+
}));
561563
}
562564

563565
} // namespace facebook::react

packages/react-native/ReactCommon/jsinspector-modern/network/NetworkReporter.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ void NetworkReporter::reportRequestStart(
7878
int encodedDataLength,
7979
const std::optional<ResponseInfo>& redirectResponse) {
8080
if (ReactNativeFeatureFlags::enableResourceTimingAPI()) {
81-
double now = PerformanceEntryReporter::getInstance()->getCurrentTimeStamp();
81+
auto now = PerformanceEntryReporter::getInstance()->getCurrentTimeStamp();
8282

8383
// All builds: Annotate PerformanceResourceTiming metadata
8484
{
@@ -127,7 +127,7 @@ void NetworkReporter::reportRequestStart(
127127

128128
void NetworkReporter::reportConnectionTiming(const std::string& requestId) {
129129
if (ReactNativeFeatureFlags::enableResourceTimingAPI()) {
130-
double now = PerformanceEntryReporter::getInstance()->getCurrentTimeStamp();
130+
auto now = PerformanceEntryReporter::getInstance()->getCurrentTimeStamp();
131131

132132
// All builds: Annotate PerformanceResourceTiming metadata
133133
{
@@ -168,7 +168,7 @@ void NetworkReporter::reportResponseStart(
168168
const ResponseInfo& responseInfo,
169169
int encodedDataLength) {
170170
if (ReactNativeFeatureFlags::enableResourceTimingAPI()) {
171-
double now = PerformanceEntryReporter::getInstance()->getCurrentTimeStamp();
171+
auto now = PerformanceEntryReporter::getInstance()->getCurrentTimeStamp();
172172

173173
// All builds: Annotate PerformanceResourceTiming metadata
174174
{
@@ -205,7 +205,7 @@ void NetworkReporter::reportResponseStart(
205205

206206
void NetworkReporter::reportDataReceived(const std::string& requestId) {
207207
if (ReactNativeFeatureFlags::enableResourceTimingAPI()) {
208-
double now = PerformanceEntryReporter::getInstance()->getCurrentTimeStamp();
208+
auto now = PerformanceEntryReporter::getInstance()->getCurrentTimeStamp();
209209

210210
// All builds: Annotate PerformanceResourceTiming metadata
211211
{
@@ -233,7 +233,7 @@ void NetworkReporter::reportResponseEnd(
233233
const std::string& requestId,
234234
int encodedDataLength) {
235235
if (ReactNativeFeatureFlags::enableResourceTimingAPI()) {
236-
double now = PerformanceEntryReporter::getInstance()->getCurrentTimeStamp();
236+
auto now = PerformanceEntryReporter::getInstance()->getCurrentTimeStamp();
237237

238238
// All builds: Report PerformanceResourceTiming event
239239
{

packages/react-native/ReactCommon/jsinspector-modern/network/NetworkReporter.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ using FrontendChannel = std::function<void(std::string_view messageJson)>;
3535
*/
3636
struct ResourceTimingData {
3737
std::string url;
38-
DOMHighResTimeStamp fetchStart;
39-
DOMHighResTimeStamp requestStart;
40-
std::optional<DOMHighResTimeStamp> connectStart;
41-
std::optional<DOMHighResTimeStamp> connectEnd;
42-
std::optional<DOMHighResTimeStamp> responseStart;
38+
HighResTimeStamp fetchStart;
39+
HighResTimeStamp requestStart;
40+
std::optional<HighResTimeStamp> connectStart;
41+
std::optional<HighResTimeStamp> connectEnd;
42+
std::optional<HighResTimeStamp> responseStart;
4343
std::optional<int> responseStatus;
4444
};
4545

packages/react-native/ReactCommon/jsinspector-modern/tracing/EventLoopReporter.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "EventLoopReporter.h"
99

1010
#if defined(REACT_NATIVE_DEBUGGER_ENABLED)
11+
#include <react/timing/primitives.h>
1112
#include "PerformanceTracer.h"
1213
#endif
1314

packages/react-native/ReactCommon/jsinspector-modern/tracing/PerformanceTracer.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
#include "PerformanceTracer.h"
99
#include "Timing.h"
1010

11+
#include <oscompat/OSCompat.h>
12+
#include <react/timing/primitives.h>
13+
1114
#include <folly/json.h>
1215

1316
#include <mutex>

packages/react-native/ReactCommon/jsinspector-modern/tracing/PerformanceTracer.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
#include <react/timing/primitives.h>
1313

14+
#include <react/timing/primitives.h>
15+
1416
#include <folly/dynamic.h>
1517
#include <functional>
1618
#include <optional>

packages/react-native/ReactCommon/react/bridging/tests/BridgingTest.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,4 +766,22 @@ TEST_F(BridgingTest, dynamicTest) {
766766
EXPECT_TRUE(undefinedFromJsResult.isNull());
767767
}
768768

769+
TEST_F(BridgingTest, highResTimeStampTest) {
770+
HighResTimeStamp timestamp = HighResTimeStamp::now();
771+
EXPECT_EQ(
772+
timestamp,
773+
bridging::fromJs<HighResTimeStamp>(
774+
rt, bridging::toJs(rt, timestamp), invoker));
775+
776+
auto duration = HighResDuration::fromNanoseconds(1);
777+
EXPECT_EQ(
778+
duration,
779+
bridging::fromJs<HighResDuration>(
780+
rt, bridging::toJs(rt, duration), invoker));
781+
782+
EXPECT_EQ(1.0, bridging::toJs(rt, HighResDuration::fromNanoseconds(1e6)));
783+
EXPECT_EQ(
784+
1.000001, bridging::toJs(rt, HighResDuration::fromNanoseconds(1e6 + 1)));
785+
}
786+
769787
} // namespace facebook::react

packages/react-native/ReactCommon/react/nativemodule/intersectionobserver/NativeIntersectionObserver.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ using NativeIntersectionObserverEntry =
4949
// isIntersectingAboveThresholds
5050
bool,
5151
// time
52-
double>;
52+
HighResTimeStamp>;
5353

5454
template <>
5555
struct Bridging<NativeIntersectionObserverEntry>

0 commit comments

Comments
 (0)