Skip to content

Commit bb508a4

Browse files
rubennortefacebook-github-bot
authored andcommitted
Refactor PerformanceEntry and subclasses to use interfaces for initialization (#53429)
Summary: Pull Request resolved: #53429 Changelog: [internal] This is a refactor of the types in `PerformanceEntry` and subclasses to accept interfaces instead of objects. This allows us to pass down the init object from subclasses to the superclass without having to create intermediate objects. Additionally, this is also more semantically correct, as existing APIs don't need those options to be own properties of the init object. Existing benchmark for Performance doesn't show any significant impact. Reviewed By: rshest Differential Revision: D80800075 fbshipit-source-id: ab439d70f4db9ce60e3089d89ccb105a91e7ef48
1 parent 81f8b0a commit bb508a4

File tree

7 files changed

+80
-81
lines changed

7 files changed

+80
-81
lines changed

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

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
*/
1010

1111
// flowlint unsafe-getters-setters:off
12-
1312
import type {
1413
DOMHighResTimeStamp,
14+
PerformanceEntryInit,
1515
PerformanceEntryJSON,
1616
} from './PerformanceEntry';
1717

@@ -29,25 +29,20 @@ export type PerformanceEventTimingJSON = {
2929
...
3030
};
3131

32+
export interface PerformanceEventTimingInit extends PerformanceEntryInit {
33+
+processingStart?: DOMHighResTimeStamp;
34+
+processingEnd?: DOMHighResTimeStamp;
35+
+interactionId?: number;
36+
}
37+
3238
export class PerformanceEventTiming extends PerformanceEntry {
3339
#processingStart: DOMHighResTimeStamp;
3440
#processingEnd: DOMHighResTimeStamp;
3541
#interactionId: number;
3642

37-
constructor(init: {
38-
name: string,
39-
startTime?: DOMHighResTimeStamp,
40-
duration?: DOMHighResTimeStamp,
41-
processingStart?: DOMHighResTimeStamp,
42-
processingEnd?: DOMHighResTimeStamp,
43-
interactionId?: number,
44-
}) {
45-
super({
46-
name: init.name,
47-
entryType: 'event',
48-
startTime: init.startTime ?? 0,
49-
duration: init.duration ?? 0,
50-
});
43+
constructor(init: PerformanceEventTimingInit) {
44+
super('event', init);
45+
5146
this.#processingStart = init.processingStart ?? 0;
5247
this.#processingEnd = init.processingEnd ?? 0;
5348
this.#interactionId = init.interactionId ?? 0;

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
*/
1010

1111
// flowlint unsafe-getters-setters:off
12-
13-
import type {PerformanceEntryJSON} from './PerformanceEntry';
12+
import type {
13+
PerformanceEntryInit,
14+
PerformanceEntryJSON,
15+
} from './PerformanceEntry';
1416

1517
import {PerformanceEntry} from './PerformanceEntry';
1618

@@ -25,7 +27,13 @@ export class TaskAttributionTiming extends PerformanceEntry {}
2527
const EMPTY_ATTRIBUTION: $ReadOnlyArray<TaskAttributionTiming> =
2628
Object.preventExtensions([]);
2729

30+
export interface PerformanceLongTaskTimingInit extends PerformanceEntryInit {}
31+
2832
export class PerformanceLongTaskTiming extends PerformanceEntry {
33+
constructor(init: PerformanceEntryInit) {
34+
super('longtask', init);
35+
}
36+
2937
get attribution(): $ReadOnlyArray<TaskAttributionTiming> {
3038
return EMPTY_ATTRIBUTION;
3139
}

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,13 @@ const cachedGetMarkTime = NativePerformance.getMarkTime;
6464
const cachedNativeClearMarks = NativePerformance.clearMarks;
6565
const cachedNativeClearMeasures = NativePerformance.clearMeasures;
6666

67-
const MARK_OPTIONS_REUSABLE_OBJECT: {...PerformanceMarkOptions} = {
67+
const MARK_OPTIONS_REUSABLE_OBJECT: PerformanceMarkOptions = {
6868
startTime: 0,
6969
detail: undefined,
7070
};
7171

72-
const MEASURE_OPTIONS_REUSABLE_OBJECT: {...PerformanceMeasureInit} = {
72+
const MEASURE_OPTIONS_REUSABLE_OBJECT: PerformanceMeasureInit = {
73+
name: '',
7374
startTime: 0,
7475
duration: 0,
7576
detail: undefined,
@@ -189,7 +190,9 @@ export default class Performance {
189190
resolvedDetail = structuredClone(detail);
190191
}
191192

193+
// $FlowExpectedError[cannot-write]
192194
MARK_OPTIONS_REUSABLE_OBJECT.startTime = resolvedStartTime;
195+
// $FlowExpectedError[cannot-write]
193196
MARK_OPTIONS_REUSABLE_OBJECT.detail = resolvedDetail;
194197

195198
const entry = new PerformanceMark(
@@ -367,14 +370,16 @@ export default class Performance {
367370
}
368371
}
369372

373+
// $FlowExpectedError[cannot-write]
374+
MEASURE_OPTIONS_REUSABLE_OBJECT.name = resolvedMeasureName;
375+
// $FlowExpectedError[cannot-write]
370376
MEASURE_OPTIONS_REUSABLE_OBJECT.startTime = resolvedStartTime;
377+
// $FlowExpectedError[cannot-write]
371378
MEASURE_OPTIONS_REUSABLE_OBJECT.duration = resolvedDuration;
379+
// $FlowExpectedError[cannot-write]
372380
MEASURE_OPTIONS_REUSABLE_OBJECT.detail = resolvedDetail;
373381

374-
const entry = new PerformanceMeasure(
375-
resolvedMeasureName,
376-
MEASURE_OPTIONS_REUSABLE_OBJECT,
377-
);
382+
const entry = new PerformanceMeasure(MEASURE_OPTIONS_REUSABLE_OBJECT);
378383

379384
cachedReportMeasure(
380385
resolvedMeasureName,

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,24 +28,25 @@ export type PerformanceEntryJSON = {
2828
...
2929
};
3030

31+
export interface PerformanceEntryInit {
32+
+name: string;
33+
+startTime: DOMHighResTimeStamp;
34+
+duration: DOMHighResTimeStamp;
35+
}
36+
3137
export class PerformanceEntry {
3238
// We don't use private fields because they're significantly slower to
3339
// initialize on construction and to access.
3440
// We also need these to be protected so they can be initialized in subclasses
3541
// where we avoid calling `super()` for performance reasons.
36-
__name: string;
3742
__entryType: PerformanceEntryType;
43+
__name: string;
3844
__startTime: DOMHighResTimeStamp;
3945
__duration: DOMHighResTimeStamp;
4046

41-
constructor(init: {
42-
name: string,
43-
entryType: PerformanceEntryType,
44-
startTime: DOMHighResTimeStamp,
45-
duration: DOMHighResTimeStamp,
46-
}) {
47+
constructor(entryType: PerformanceEntryType, init: PerformanceEntryInit) {
48+
this.__entryType = entryType;
4749
this.__name = init.name;
48-
this.__entryType = init.entryType;
4950
this.__startTime = init.startTime;
5051
this.__duration = init.duration;
5152
}

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

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,19 @@ export type PerformanceResourceTimingJSON = {
2929
...
3030
};
3131

32+
export interface PerformanceResourceTimingInit {
33+
+name: string;
34+
+startTime: DOMHighResTimeStamp;
35+
+duration: DOMHighResTimeStamp;
36+
+fetchStart: DOMHighResTimeStamp;
37+
+requestStart: DOMHighResTimeStamp;
38+
+connectStart: DOMHighResTimeStamp;
39+
+connectEnd: DOMHighResTimeStamp;
40+
+responseStart: DOMHighResTimeStamp;
41+
+responseEnd: DOMHighResTimeStamp;
42+
+responseStatus?: number;
43+
}
44+
3245
export class PerformanceResourceTiming extends PerformanceEntry {
3346
#fetchStart: DOMHighResTimeStamp;
3447
#requestStart: DOMHighResTimeStamp;
@@ -38,24 +51,9 @@ export class PerformanceResourceTiming extends PerformanceEntry {
3851
#responseEnd: DOMHighResTimeStamp;
3952
#responseStatus: ?number;
4053

41-
constructor(init: {
42-
name: string,
43-
startTime: DOMHighResTimeStamp,
44-
duration: DOMHighResTimeStamp,
45-
fetchStart: DOMHighResTimeStamp,
46-
requestStart: DOMHighResTimeStamp,
47-
connectStart: DOMHighResTimeStamp,
48-
connectEnd: DOMHighResTimeStamp,
49-
responseStart: DOMHighResTimeStamp,
50-
responseEnd: DOMHighResTimeStamp,
51-
responseStatus?: number,
52-
}) {
53-
super({
54-
name: init.name,
55-
entryType: 'resource',
56-
startTime: init.startTime,
57-
duration: init.duration,
58-
});
54+
constructor(init: PerformanceResourceTimingInit) {
55+
super('resource', init);
56+
5957
this.#fetchStart = init.fetchStart;
6058
this.#requestStart = init.requestStart;
6159
this.#connectStart = init.connectStart;

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

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
*/
1010

1111
// flowlint unsafe-getters-setters:off
12-
13-
import type {DOMHighResTimeStamp} from './PerformanceEntry';
12+
import type {
13+
DOMHighResTimeStamp,
14+
PerformanceEntryInit,
15+
} from './PerformanceEntry';
1416
import type {
1517
ExtensionMarkerPayload,
1618
ExtensionTrackEntryPayload,
@@ -25,18 +27,16 @@ export type DetailType =
2527
// but we'll use it as documentation for how to use the extensibility API.
2628
| {devtools?: ExtensionMarkerPayload | ExtensionTrackEntryPayload, ...};
2729

28-
export type PerformanceMarkOptions = $ReadOnly<{
29-
detail?: DetailType,
30-
startTime?: DOMHighResTimeStamp,
31-
}>;
30+
export interface PerformanceMarkOptions {
31+
+detail?: DetailType;
32+
+startTime?: DOMHighResTimeStamp;
33+
}
3234

3335
export type TimeStampOrName = DOMHighResTimeStamp | string;
3436

35-
export type PerformanceMeasureInit = $ReadOnly<{
36-
detail?: DetailType,
37-
startTime: DOMHighResTimeStamp,
38-
duration: DOMHighResTimeStamp,
39-
}>;
37+
export interface PerformanceMeasureInit extends PerformanceEntryInit {
38+
+detail?: DetailType;
39+
}
4040

4141
class PerformanceMarkTemplate extends PerformanceEntry {
4242
// We don't use private fields because they're significantly slower to
@@ -45,9 +45,8 @@ class PerformanceMarkTemplate extends PerformanceEntry {
4545

4646
// This constructor isn't really used. See `PerformanceMark` below.
4747
constructor(markName: string, markOptions?: PerformanceMarkOptions) {
48-
super({
48+
super('mark', {
4949
name: markName,
50-
entryType: 'mark',
5150
startTime: markOptions?.startTime ?? getCurrentTimeStamp(),
5251
duration: 0,
5352
});
@@ -72,8 +71,8 @@ export const PerformanceMark: typeof PerformanceMarkTemplate =
7271
markName: string,
7372
markOptions?: PerformanceMarkOptions,
7473
) {
75-
this.__name = markName;
7674
this.__entryType = 'mark';
75+
this.__name = markName;
7776
this.__startTime = markOptions?.startTime ?? getCurrentTimeStamp();
7877
this.__duration = 0;
7978

@@ -89,15 +88,10 @@ class PerformanceMeasureTemplate extends PerformanceEntry {
8988
__detail: DetailType;
9089

9190
// This constructor isn't really used. See `PerformanceMeasure` below.
92-
constructor(measureName: string, measureOptions: PerformanceMeasureInit) {
93-
super({
94-
name: measureName,
95-
entryType: 'measure',
96-
startTime: measureOptions.startTime,
97-
duration: measureOptions.duration,
98-
});
91+
constructor(init: PerformanceMeasureInit) {
92+
super('measure', init);
9993

100-
this.__detail = measureOptions?.detail ?? null;
94+
this.__detail = init?.detail ?? null;
10195
}
10296

10397
get detail(): DetailType {
@@ -110,15 +104,14 @@ export const PerformanceMeasure: typeof PerformanceMeasureTemplate =
110104
// $FlowExpectedError[incompatible-type]
111105
function PerformanceMeasure(
112106
this: PerformanceMeasureTemplate,
113-
measureName: string,
114-
measureOptions: PerformanceMeasureInit,
107+
init: PerformanceMeasureInit,
115108
) {
116-
this.__name = measureName;
117109
this.__entryType = 'measure';
118-
this.__startTime = measureOptions.startTime;
119-
this.__duration = measureOptions.duration;
110+
this.__name = init.name;
111+
this.__startTime = init.startTime;
112+
this.__duration = init.duration;
120113

121-
this.__detail = measureOptions.detail ?? null;
114+
this.__detail = init.detail ?? null;
122115
};
123116

124117
// $FlowExpectedError[prop-missing]

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ export function rawToPerformanceEntry(
4444
case RawPerformanceEntryTypeValues.LONGTASK:
4545
return new PerformanceLongTaskTiming({
4646
name: entry.name,
47-
entryType: rawToPerformanceEntryType(entry.entryType),
4847
startTime: entry.startTime,
4948
duration: entry.duration,
5049
});
@@ -53,7 +52,8 @@ export function rawToPerformanceEntry(
5352
startTime: entry.startTime,
5453
});
5554
case RawPerformanceEntryTypeValues.MEASURE:
56-
return new PerformanceMeasure(entry.name, {
55+
return new PerformanceMeasure({
56+
name: entry.name,
5757
startTime: entry.startTime,
5858
duration: entry.duration,
5959
});
@@ -71,9 +71,8 @@ export function rawToPerformanceEntry(
7171
responseStatus: entry.responseStatus,
7272
});
7373
default:
74-
return new PerformanceEntry({
74+
return new PerformanceEntry(rawToPerformanceEntryType(entry.entryType), {
7575
name: entry.name,
76-
entryType: rawToPerformanceEntryType(entry.entryType),
7776
startTime: entry.startTime,
7877
duration: entry.duration,
7978
});

0 commit comments

Comments
 (0)