Skip to content

Commit 5ecb5ec

Browse files
rubennortemeta-codesync[bot]
authored andcommitted
Re-apply "Require the NativePerformance module and drop the legacy performance fallback" (#57609)
Summary: Pull Request resolved: #57609 This re-applies a previously landed change that was reverted. It requires the NativePerformance module and removes the legacy `performance` fallback path. `setUpPerformance` previously branched between the modern Performance API setup and a barebones `performance` stub depending on whether the native Performance module was available. The native module is now always present in supported runtimes, so this consolidates everything on the modern setup and removes the dead fallback path. - `NativePerformance` is now resolved via `getEnforcing` instead of a nullable `get`, and its `timeOrigin` method is no longer optional (the native module always implements it). - Removed the legacy `performance` stub and the `global.nativePerformanceNow` fallbacks, along with the now-redundant null checks and the `getCurrentTimeStamp` indirection (native `now`/`timeOrigin` are cached directly). - `setUpPerformance` is kept as a thin alias of the internal setup module so external callers keep working. Changelog: [Internal] Reviewed By: huntie Differential Revision: D112566045
1 parent e640278 commit 5ecb5ec

9 files changed

Lines changed: 33 additions & 90 deletions

File tree

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

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

11-
import setUpPerformanceModern from '../../src/private/setup/setUpPerformanceModern';
12-
import NativePerformance from '../../src/private/webapis/performance/specs/NativePerformance';
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';
1314

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-
}
15+
setUpPerformance();

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

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

packages/react-native/src/private/setup/setUpPerformanceModern.js renamed to packages/react-native/src/private/setup/setUpPerformance.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 setUpPerformanceModern() {
15+
export default function setUpPerformance() {
1616
if (initialized) {
1717
return;
1818
}

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

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

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

2421
export type PerformanceEventTimingJSON = {
2522
...PerformanceEntryJSON,

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

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

3937
export type PerformanceMeasureOptions =
4038
| Readonly<{
@@ -56,13 +54,8 @@ export type PerformanceMeasureOptions =
5654
const ENTRY_TYPES_AVAILABLE_FROM_TIMELINE: ReadonlyArray<PerformanceEntryType> =
5755
['mark', 'measure'];
5856

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;
57+
const {now, reportMark, reportMeasure, getMarkTime, clearMarks, clearMeasures} =
58+
NativePerformance;
6659
let cachedTimeOrigin: ?DOMHighResTimeStamp;
6760

6861
const MARK_OPTIONS_REUSABLE_OBJECT: PerformanceMarkOptions = {
@@ -78,7 +71,7 @@ const MEASURE_OPTIONS_REUSABLE_OBJECT: PerformanceMeasureInit = {
7871
};
7972

8073
const getMarkTimeForMeasure = (markName: string): number => {
81-
const markTime = cachedGetMarkTime(markName);
74+
const markTime = getMarkTime(markName);
8275
if (markTime == null) {
8376
throw new DOMException(
8477
`Failed to execute 'measure' on 'Performance': The mark '${markName}' does not exist.`,
@@ -147,12 +140,7 @@ export default class Performance {
147140
*/
148141
get timeOrigin(): DOMHighResTimeStamp {
149142
if (cachedTimeOrigin == null) {
150-
if (NativePerformance.timeOrigin) {
151-
cachedTimeOrigin = NativePerformance?.timeOrigin();
152-
} else {
153-
// Very naive polyfill.
154-
cachedTimeOrigin = Date.now() - getCurrentTimeStamp();
155-
}
143+
cachedTimeOrigin = NativePerformance.timeOrigin();
156144
}
157145

158146
return cachedTimeOrigin;
@@ -202,7 +190,7 @@ export default class Performance {
202190
);
203191
}
204192
} else {
205-
resolvedStartTime = getCurrentTimeStamp();
193+
resolvedStartTime = now();
206194
}
207195

208196
if (detail !== undefined) {
@@ -219,13 +207,13 @@ export default class Performance {
219207
MARK_OPTIONS_REUSABLE_OBJECT,
220208
);
221209

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

224212
return entry;
225213
}
226214

227215
clearMarks(markName?: string): void {
228-
cachedNativeClearMarks(markName);
216+
clearMarks(markName);
229217
}
230218

231219
measure(
@@ -347,7 +335,7 @@ export default class Performance {
347335
) {
348336
resolvedDuration = resolvedEndTime - resolvedStartTime;
349337
} else {
350-
resolvedDuration = getCurrentTimeStamp() - resolvedStartTime;
338+
resolvedDuration = now() - resolvedStartTime;
351339
}
352340
}
353341

@@ -364,7 +352,7 @@ export default class Performance {
364352
resolvedDuration =
365353
getMarkTimeForMeasure(endMark) - resolvedStartTime;
366354
} else {
367-
resolvedDuration = getCurrentTimeStamp() - resolvedStartTime;
355+
resolvedDuration = now() - resolvedStartTime;
368356
}
369357
break;
370358
}
@@ -375,7 +363,7 @@ export default class Performance {
375363
resolvedDuration =
376364
getMarkTimeForMeasure(endMark) - resolvedStartTime;
377365
} else {
378-
resolvedDuration = getCurrentTimeStamp() - resolvedStartTime;
366+
resolvedDuration = now() - resolvedStartTime;
379367
}
380368
}
381369
}
@@ -385,7 +373,7 @@ export default class Performance {
385373
if (endMark !== undefined) {
386374
resolvedDuration = getMarkTimeForMeasure(endMark) - resolvedStartTime;
387375
} else {
388-
resolvedDuration = getCurrentTimeStamp() - resolvedStartTime;
376+
resolvedDuration = now() - resolvedStartTime;
389377
}
390378
}
391379

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

401389
const entry = new PerformanceMeasure(MEASURE_OPTIONS_REUSABLE_OBJECT);
402390

403-
cachedReportMeasure(
391+
reportMeasure(
404392
resolvedMeasureName,
405393
resolvedStartTime,
406394
resolvedDuration,
@@ -411,14 +399,14 @@ export default class Performance {
411399
}
412400

413401
clearMeasures(measureName?: string): void {
414-
cachedNativeClearMeasures(measureName);
402+
clearMeasures(measureName);
415403
}
416404

417405
/**
418406
* Returns a double, measured in milliseconds.
419407
* https://developer.mozilla.org/en-US/docs/Web/API/Performance/now
420408
*/
421-
now: () => DOMHighResTimeStamp = getCurrentTimeStamp;
409+
now: () => DOMHighResTimeStamp = now;
422410

423411
/**
424412
* 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: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,11 @@ import {
2121
rawToPerformanceEntry,
2222
rawToPerformanceEntryType,
2323
} from './internals/RawPerformanceEntry';
24-
import MaybeNativePerformance from './specs/NativePerformance';
24+
import NativePerformance from './specs/NativePerformance';
2525
import nullthrows from 'nullthrows';
2626

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

29-
const NativePerformance = nullthrows(MaybeNativePerformance);
30-
3129
export class PerformanceObserverEntryList {
3230
#entries: PerformanceEntryList;
3331

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

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

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

2426
export type DetailType =
2527
| unknown
@@ -47,7 +49,7 @@ class PerformanceMarkTemplate extends PerformanceEntry {
4749
constructor(markName: string, markOptions?: PerformanceMarkOptions) {
4850
super('mark', {
4951
name: markName,
50-
startTime: markOptions?.startTime ?? getCurrentTimeStamp(),
52+
startTime: markOptions?.startTime ?? now(),
5153
duration: 0,
5254
});
5355

@@ -73,7 +75,7 @@ export const PerformanceMark: typeof PerformanceMarkTemplate =
7375
) {
7476
this.__entryType = 'mark';
7577
this.__name = markName;
76-
this.__startTime = markOptions?.startTime ?? getCurrentTimeStamp();
78+
this.__startTime = markOptions?.startTime ?? now();
7779
this.__duration = 0;
7880

7981
this.__detail = markOptions?.detail ?? null;

packages/react-native/src/private/webapis/performance/internals/Utilities.js

Lines changed: 0 additions & 27 deletions
This file was deleted.

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

Lines changed: 4 additions & 2 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,4 +107,6 @@ export interface Spec extends TurboModule {
107107
readonly clearEventCountsForTesting: () => void;
108108
}
109109

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

0 commit comments

Comments
 (0)