Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 15 additions & 17 deletions packages/react-native/ReactCommon/react/timing/primitives.h
Original file line number Diff line number Diff line change
Expand Up @@ -320,9 +320,9 @@ class HighResTimeStamp {

static HighResDuration computeUnsafeOriginFromUnixTimeStamp() noexcept
{
auto systemNow = std::chrono::system_clock::now();
auto steadyNow = std::chrono::steady_clock::now();
return HighResDuration(systemNow.time_since_epoch() - steadyNow.time_since_epoch());
const auto systemNow = std::chrono::system_clock::now();
const auto monotonicNow = platformNow();
return HighResDuration(systemNow.time_since_epoch() - monotonicNow.time_since_epoch());
}

#ifdef __APPLE__
Expand All @@ -342,35 +342,33 @@ class HighResTimeStamp {
}
#endif

static std::chrono::steady_clock::time_point platformNow() noexcept
{
#ifdef __APPLE__
return machAbsoluteTimeToSteadyClockTimePoint();
#else
return std::chrono::steady_clock::now();
#endif
}

#ifdef REACT_NATIVE_DEBUG
static std::function<std::chrono::steady_clock::time_point()> &getTimeStampProvider()
{
static std::function<std::chrono::steady_clock::time_point()> timeStampProvider = nullptr;
return timeStampProvider;
}
#endif

static std::chrono::steady_clock::time_point chronoNow()
{
#ifdef REACT_NATIVE_DEBUG
auto &timeStampProvider = getTimeStampProvider();
if (timeStampProvider != nullptr) {
return timeStampProvider();
}
#ifdef __APPLE__
return machAbsoluteTimeToSteadyClockTimePoint();
#else
return std::chrono::steady_clock::now();
#endif
return platformNow();
}
#else
inline static std::chrono::steady_clock::time_point chronoNow()
{
#ifdef __APPLE__
return machAbsoluteTimeToSteadyClockTimePoint();
#else
return std::chrono::steady_clock::now();
#endif
}
#endif
};

inline constexpr HighResDuration operator-(const HighResTimeStamp &lhs, const HighResTimeStamp &rhs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@

import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';

import * as Fantom from '@react-native/fantom';

describe('Performance', () => {
it('does NOT allow creating instances of Performance directly', () => {
expect(() => {
Expand Down Expand Up @@ -51,27 +49,9 @@ describe('Performance', () => {
expect(second).toBe(first);
});

// On macOS, `performance.now()` is backed by a monotonic clock that does
// NOT include time spent while the system is asleep. This causes the
// monotonic time to drift relative to wall time (`Date.now()`) the longer
// the machine has been running, so we can't reliably compare
// `performance.now() + performance.timeOrigin` against `Date.now()` like
// we can on other platforms.
const itIfNotMacOS = Fantom.getHostPlatform() === 'macos' ? it.skip : it;

itIfNotMacOS('allows moving timestamps to Unix epoch', () => {
// We need to truncate timestamps because `Date.now()` only provides
// integer millisecond precision.
const adjustedMonotonicTime = Math.trunc(
performance.now() + performance.timeOrigin,
);
const wallTime = Date.now();
const adjustedMonotonicTimeAfter = Math.trunc(
performance.now() + performance.timeOrigin,
);

expect(wallTime).toBeGreaterThanOrEqual(adjustedMonotonicTime);
expect(adjustedMonotonicTimeAfter).toBeGreaterThanOrEqual(wallTime);
it('maps monotonic timestamps to Unix time on a fresh runtime', () => {
const estimatedWallTime = performance.timeOrigin + performance.now();
expect(Math.abs(Date.now() - estimatedWallTime)).toBeLessThan(100);
});
});
});
Loading