From 9fbdd91a149f4ae714ec4b377aa891ff7ab5178c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Norte?= Date: Wed, 2 Dec 2026 04:46:00 -0800 Subject: [PATCH] Fix Performance time origin on Apple platforms Summary: `performance.now()` excludes device sleep on Apple platforms, but the epoch offset was sampled from a clock that includes it. That made `performance.timeOrigin + performance.now()` lag wall time by sleep accumulated before the offset was initialized. Compute the offset from the same platform clock as `performance.now()` without changing the boot-relative timestamp contract. Run the existing epoch-mapping test on macOS as well. Fixes https://github.com/react/react-native/issues/60450: sleep before the origin is initialized no longer skews the sum. Sleep after initialization is still excluded. Changelog: [iOS][Fixed] - Use a consistent clock for `performance.timeOrigin` and `performance.now()` on Apple platforms Differential Revision: D135623722 --- .../ReactCommon/react/timing/primitives.h | 32 +++++++++---------- .../__tests__/Performance-itest.js | 26 ++------------- 2 files changed, 18 insertions(+), 40 deletions(-) diff --git a/packages/react-native/ReactCommon/react/timing/primitives.h b/packages/react-native/ReactCommon/react/timing/primitives.h index d691df706674..8354a110df0d 100644 --- a/packages/react-native/ReactCommon/react/timing/primitives.h +++ b/packages/react-native/ReactCommon/react/timing/primitives.h @@ -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__ @@ -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 &getTimeStampProvider() { static std::function 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) diff --git a/packages/react-native/src/private/webapis/performance/__tests__/Performance-itest.js b/packages/react-native/src/private/webapis/performance/__tests__/Performance-itest.js index 8e2ea9654f56..e57e4cd1ec19 100644 --- a/packages/react-native/src/private/webapis/performance/__tests__/Performance-itest.js +++ b/packages/react-native/src/private/webapis/performance/__tests__/Performance-itest.js @@ -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(() => { @@ -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); }); }); });