Skip to content

Commit 7825d51

Browse files
hoxyqmeta-codesync[bot]
authored andcommitted
Revert D112116429 (#57591)
Summary: Pull Request resolved: #57591 Changelog: [Internal] Reviewed By: thegreatercurve, lenaic, cortinico Differential Revision: D112522540 fbshipit-source-id: dd1ff3ea751399192a7c9280faaf13ee41d0c071
1 parent 2bc248c commit 7825d51

9 files changed

Lines changed: 90 additions & 33 deletions

File tree

packages/react-native/Libraries/Core/setUpPerformance.js

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,25 @@
88
* @format
99
*/
1010

11-
// Kept as an alias of the internal `setUpPerformance` module for external
12-
// consumers that import this path directly.
13-
import setUpPerformance from '../../src/private/setup/setUpPerformance';
11+
import setUpPerformanceModern from '../../src/private/setup/setUpPerformanceModern';
12+
import NativePerformance from '../../src/private/webapis/performance/specs/NativePerformance';
1413

15-
setUpPerformance();
14+
// In case if the native implementation of the Performance API is available, use it,
15+
// otherwise fall back to the legacy/default one, which only defines 'Performance.now()'
16+
if (NativePerformance) {
17+
setUpPerformanceModern();
18+
} else {
19+
if (!global.performance) {
20+
// $FlowExpectedError[cannot-write]
21+
global.performance = {
22+
mark: () => {},
23+
clearMarks: () => {},
24+
measure: () => {},
25+
clearMeasures: () => {},
26+
now: () => {
27+
const performanceNow = global.nativePerformanceNow || Date.now;
28+
return performanceNow();
29+
},
30+
};
31+
}
32+
}

packages/react-native/src/private/setup/setUpDefaultReactNativeEnvironment.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
let initialized = false;
1212

13-
export default function setUpDefaultReactNativeEnvironment(
13+
export default function setUpDefaltReactNativeEnvironment(
1414
enableDeveloperTools: boolean = true,
1515
) {
1616
if (initialized) {
@@ -21,7 +21,7 @@ export default function setUpDefaultReactNativeEnvironment(
2121

2222
require('../../../Libraries/Core/setUpGlobals');
2323
require('./setUpDOM').default();
24-
require('./setUpPerformance').default();
24+
require('../../../Libraries/Core/setUpPerformance');
2525
require('../../../Libraries/Core/polyfillPromise');
2626
require('../../../Libraries/Core/setUpTimers');
2727
if (__DEV__ && enableDeveloperTools) {

packages/react-native/src/private/setup/setUpPerformance.js renamed to packages/react-native/src/private/setup/setUpPerformanceModern.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {polyfillGlobal} from '../../../Libraries/Utilities/PolyfillFunctions';
1212

1313
let initialized = false;
1414

15-
export default function setUpPerformance() {
15+
export default function setUpPerformanceModern() {
1616
if (initialized) {
1717
return;
1818
}

packages/react-native/src/private/webapis/performance/EventTiming.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ import type {
1616
} from './PerformanceEntry';
1717

1818
import {PerformanceEntry} from './PerformanceEntry';
19-
import NativePerformance from './specs/NativePerformance';
19+
import MaybeNativePerformance from './specs/NativePerformance';
20+
import nullthrows from 'nullthrows';
21+
22+
const NativePerformance = nullthrows(MaybeNativePerformance);
2023

2124
export type PerformanceEventTimingJSON = {
2225
...PerformanceEntryJSON,

packages/react-native/src/private/webapis/performance/Performance.js

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,12 @@ import {
2929
performanceEntryTypeToRaw,
3030
rawToPerformanceEntry,
3131
} from './internals/RawPerformanceEntry';
32+
import {getCurrentTimeStamp} from './internals/Utilities';
3233
import MemoryInfo from './MemoryInfo';
3334
import ReactNativeStartupTiming from './ReactNativeStartupTiming';
34-
import NativePerformance from './specs/NativePerformance';
35+
import MaybeNativePerformance from './specs/NativePerformance';
3536
import {PerformanceMark, PerformanceMeasure} from './UserTiming';
37+
import nullthrows from 'nullthrows';
3638

3739
export type PerformanceMeasureOptions =
3840
| Readonly<{
@@ -54,8 +56,13 @@ export type PerformanceMeasureOptions =
5456
const ENTRY_TYPES_AVAILABLE_FROM_TIMELINE: ReadonlyArray<PerformanceEntryType> =
5557
['mark', 'measure'];
5658

57-
const {now, reportMark, reportMeasure, getMarkTime, clearMarks, clearMeasures} =
58-
NativePerformance;
59+
const NativePerformance = nullthrows(MaybeNativePerformance);
60+
61+
const cachedReportMark = NativePerformance.reportMark;
62+
const cachedReportMeasure = NativePerformance.reportMeasure;
63+
const cachedGetMarkTime = NativePerformance.getMarkTime;
64+
const cachedNativeClearMarks = NativePerformance.clearMarks;
65+
const cachedNativeClearMeasures = NativePerformance.clearMeasures;
5966
let cachedTimeOrigin: ?DOMHighResTimeStamp;
6067

6168
const MARK_OPTIONS_REUSABLE_OBJECT: PerformanceMarkOptions = {
@@ -71,7 +78,7 @@ const MEASURE_OPTIONS_REUSABLE_OBJECT: PerformanceMeasureInit = {
7178
};
7279

7380
const getMarkTimeForMeasure = (markName: string): number => {
74-
const markTime = getMarkTime(markName);
81+
const markTime = cachedGetMarkTime(markName);
7582
if (markTime == null) {
7683
throw new DOMException(
7784
`Failed to execute 'measure' on 'Performance': The mark '${markName}' does not exist.`,
@@ -140,7 +147,12 @@ export default class Performance {
140147
*/
141148
get timeOrigin(): DOMHighResTimeStamp {
142149
if (cachedTimeOrigin == null) {
143-
cachedTimeOrigin = NativePerformance.timeOrigin();
150+
if (NativePerformance.timeOrigin) {
151+
cachedTimeOrigin = NativePerformance?.timeOrigin();
152+
} else {
153+
// Very naive polyfill.
154+
cachedTimeOrigin = Date.now() - getCurrentTimeStamp();
155+
}
144156
}
145157

146158
return cachedTimeOrigin;
@@ -190,7 +202,7 @@ export default class Performance {
190202
);
191203
}
192204
} else {
193-
resolvedStartTime = now();
205+
resolvedStartTime = getCurrentTimeStamp();
194206
}
195207

196208
if (detail !== undefined) {
@@ -207,13 +219,13 @@ export default class Performance {
207219
MARK_OPTIONS_REUSABLE_OBJECT,
208220
);
209221

210-
reportMark(resolvedMarkName, resolvedStartTime, entry);
222+
cachedReportMark(resolvedMarkName, resolvedStartTime, entry);
211223

212224
return entry;
213225
}
214226

215227
clearMarks(markName?: string): void {
216-
clearMarks(markName);
228+
cachedNativeClearMarks(markName);
217229
}
218230

219231
measure(
@@ -335,7 +347,7 @@ export default class Performance {
335347
) {
336348
resolvedDuration = resolvedEndTime - resolvedStartTime;
337349
} else {
338-
resolvedDuration = now() - resolvedStartTime;
350+
resolvedDuration = getCurrentTimeStamp() - resolvedStartTime;
339351
}
340352
}
341353

@@ -352,7 +364,7 @@ export default class Performance {
352364
resolvedDuration =
353365
getMarkTimeForMeasure(endMark) - resolvedStartTime;
354366
} else {
355-
resolvedDuration = now() - resolvedStartTime;
367+
resolvedDuration = getCurrentTimeStamp() - resolvedStartTime;
356368
}
357369
break;
358370
}
@@ -363,7 +375,7 @@ export default class Performance {
363375
resolvedDuration =
364376
getMarkTimeForMeasure(endMark) - resolvedStartTime;
365377
} else {
366-
resolvedDuration = now() - resolvedStartTime;
378+
resolvedDuration = getCurrentTimeStamp() - resolvedStartTime;
367379
}
368380
}
369381
}
@@ -373,7 +385,7 @@ export default class Performance {
373385
if (endMark !== undefined) {
374386
resolvedDuration = getMarkTimeForMeasure(endMark) - resolvedStartTime;
375387
} else {
376-
resolvedDuration = now() - resolvedStartTime;
388+
resolvedDuration = getCurrentTimeStamp() - resolvedStartTime;
377389
}
378390
}
379391

@@ -388,7 +400,7 @@ export default class Performance {
388400

389401
const entry = new PerformanceMeasure(MEASURE_OPTIONS_REUSABLE_OBJECT);
390402

391-
reportMeasure(
403+
cachedReportMeasure(
392404
resolvedMeasureName,
393405
resolvedStartTime,
394406
resolvedDuration,
@@ -399,14 +411,14 @@ export default class Performance {
399411
}
400412

401413
clearMeasures(measureName?: string): void {
402-
clearMeasures(measureName);
414+
cachedNativeClearMeasures(measureName);
403415
}
404416

405417
/**
406418
* Returns a double, measured in milliseconds.
407419
* https://developer.mozilla.org/en-US/docs/Web/API/Performance/now
408420
*/
409-
now: () => DOMHighResTimeStamp = now;
421+
now: () => DOMHighResTimeStamp = getCurrentTimeStamp;
410422

411423
/**
412424
* An extension that allows to get back to JS all currently logged marks/measures

packages/react-native/src/private/webapis/performance/PerformanceObserver.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@ import {
2121
rawToPerformanceEntry,
2222
rawToPerformanceEntryType,
2323
} from './internals/RawPerformanceEntry';
24-
import NativePerformance from './specs/NativePerformance';
24+
import MaybeNativePerformance from './specs/NativePerformance';
2525
import nullthrows from 'nullthrows';
2626

2727
export {PerformanceEntry} from './PerformanceEntry';
2828

29+
const NativePerformance = nullthrows(MaybeNativePerformance);
30+
2931
export class PerformanceObserverEntryList {
3032
#entries: PerformanceEntryList;
3133

packages/react-native/src/private/webapis/performance/UserTiming.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,8 @@ import type {
1818
ExtensionTrackEntryPayload,
1919
} from './UserTimingExtensibility';
2020

21+
import {getCurrentTimeStamp} from './internals/Utilities';
2122
import {PerformanceEntry} from './PerformanceEntry';
22-
import NativePerformance from './specs/NativePerformance';
23-
24-
const {now} = NativePerformance;
2523

2624
export type DetailType =
2725
| unknown
@@ -49,7 +47,7 @@ class PerformanceMarkTemplate extends PerformanceEntry {
4947
constructor(markName: string, markOptions?: PerformanceMarkOptions) {
5048
super('mark', {
5149
name: markName,
52-
startTime: markOptions?.startTime ?? now(),
50+
startTime: markOptions?.startTime ?? getCurrentTimeStamp(),
5351
duration: 0,
5452
});
5553

@@ -75,7 +73,7 @@ export const PerformanceMark: typeof PerformanceMarkTemplate =
7573
) {
7674
this.__entryType = 'mark';
7775
this.__name = markName;
78-
this.__startTime = markOptions?.startTime ?? now();
76+
this.__startTime = markOptions?.startTime ?? getCurrentTimeStamp();
7977
this.__duration = 0;
8078

8179
this.__detail = markOptions?.detail ?? null;
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*
7+
* @flow strict
8+
* @format
9+
*/
10+
11+
import warnOnce from '../../../../../Libraries/Utilities/warnOnce';
12+
import NativePerformance from '../specs/NativePerformance';
13+
14+
export function warnNoNativePerformance() {
15+
warnOnce(
16+
'missing-native-performance',
17+
'Missing native implementation of Performance',
18+
);
19+
}
20+
21+
declare var global: {
22+
// This value is defined directly via JSI, if available.
23+
readonly nativePerformanceNow?: ?() => number,
24+
};
25+
26+
export const getCurrentTimeStamp: () => DOMHighResTimeStamp =
27+
NativePerformance?.now ?? global.nativePerformanceNow ?? (() => Date.now());

packages/react-native/src/private/webapis/performance/specs/NativePerformance.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export type PerformanceObserverInit = {
5757

5858
export interface Spec extends TurboModule {
5959
readonly now: () => number;
60-
readonly timeOrigin: () => number;
60+
readonly timeOrigin?: () => number;
6161

6262
readonly reportMark: (
6363
name: string,
@@ -107,6 +107,4 @@ export interface Spec extends TurboModule {
107107
readonly clearEventCountsForTesting: () => void;
108108
}
109109

110-
export default TurboModuleRegistry.getEnforcing<Spec>(
111-
'NativePerformanceCxx',
112-
) as Spec;
110+
export default TurboModuleRegistry.get<Spec>('NativePerformanceCxx') as ?Spec;

0 commit comments

Comments
 (0)