Skip to content

Commit cb54bcf

Browse files
rubennortefacebook-github-bot
authored andcommitted
Add MutationObserver commit benchmarks (#60267)
Summary: ## Context MutationObserver observation adds work to React commits even when a commit does not change the observed child list. Establish public-API benchmarks before changing the native implementation so that each optimization can be measured independently against the same workloads. ## Changes Add 47 Fantom scenarios covering widths 2, 32, 256 and 1,024; parent props, append, full replacement, reorder and descendant changes; shallow/deep observation; unobserved controls; and multiple observers. Setup, reset renders, assertions and cleanup are outside the measured interval. Each measured update includes reconciliation, observation, layout/mount processing, record conversion and callback delivery. ## Baseline results Optimized native/JS code and Hermes bytecode, with MutationObserver enabled. These are fresh measurements after rebasing: milliseconds of end-to-end update CPU time, aggregated as the median of three process runs' p50 values. Setup, reset renders, assertions and cleanup are outside the measured interval; reconciliation, observation, layout/mount work, record conversion and callback delivery are included. | Workload | Children | Observers | Baseline CPU time (ms) | | --- | ---: | ---: | ---: | | Parent props only, deep observation | 1,024 | 1 | 3.846 | | Append one child | 1,024 | 1 | 6.322 | | Add a grandchild in the last retained child | 1,024 | 1 | 6.271 | | Reverse retained children | 1,024 | 1 | 8.716 | | Replace all children | 1,024 | 1 | 30.210 | | Parent props only, deep observation | 256 | 10 | 1.843 | All 47 scenarios passed in each of three timing runs per variant. Each scenario used at least 100 measured iterations, at least 250 ms measured execution, and 10 warmup iterations. These are synthetic measurements from one Linux worker, not isolated observer timings or device/app latency guarantees. Individual runs and unobserved controls varied; percentages are observed ratios of aggregate medians, not causal estimates or guaranteed speedups. The following optimizations use this benchmark unchanged. Changelog: [Internal] Reviewed By: javache Differential Revision: D134249229
1 parent 1f43abd commit cb54bcf

1 file changed

Lines changed: 156 additions & 0 deletions

File tree

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
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+
* @fantom_flags enableMutationObserverByDefault:true
8+
* @flow strict-local
9+
* @format
10+
*/
11+
12+
import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment';
13+
14+
import type MutationObserverType from '../MutationObserver';
15+
import type MutationRecordType from '../MutationRecord';
16+
import type {Root} from '@react-native/fantom';
17+
import type {HostInstance} from 'react-native';
18+
19+
import * as Fantom from '@react-native/fantom';
20+
import nullthrows from 'nullthrows';
21+
import * as React from 'react';
22+
import {createRef} from 'react';
23+
import {View} from 'react-native';
24+
25+
declare const MutationObserver: Class<MutationObserverType>;
26+
27+
type Workload = 'props' | 'append' | 'replace' | 'reverse' | 'descendant';
28+
type Scenario = Readonly<{
29+
width: number,
30+
workload: Workload,
31+
subtree: boolean,
32+
observers: number,
33+
}>;
34+
35+
const scenarios: Array<Scenario> = [];
36+
for (const width of [2, 32, 256, 1024]) {
37+
for (const workload of [
38+
'props',
39+
'append',
40+
'replace',
41+
'reverse',
42+
'descendant',
43+
] as const) {
44+
scenarios.push({width, workload, subtree: true, observers: 1});
45+
scenarios.push({width, workload, subtree: true, observers: 0});
46+
}
47+
scenarios.push({width, workload: 'props', subtree: false, observers: 1});
48+
}
49+
for (const workload of ['props', 'append', 'descendant'] as const) {
50+
scenarios.push({width: 256, workload, subtree: true, observers: 10});
51+
}
52+
53+
let commit: () => void;
54+
55+
Fantom.unstable_benchmark
56+
.suite('MutationObserver commits', {
57+
minIterations: 100,
58+
minTestExecutionTimeMs: 250,
59+
minWarmupIterations: 10,
60+
})
61+
.test.each(
62+
scenarios,
63+
({width, workload, subtree, observers}) =>
64+
`${workload}, width=${width}, subtree=${String(subtree)}, observers=${observers}`,
65+
() => commit(),
66+
scenario => {
67+
const {width, workload, subtree, observers: observerCount} = scenario;
68+
const parentRef = createRef<HostInstance>();
69+
let root: Root;
70+
let original: React.MixedElement;
71+
let updated: React.MixedElement;
72+
const observers: Array<MutationObserverType> = [];
73+
let callbackCount = 0;
74+
let lastRecords: ReadonlyArray<MutationRecordType> = [];
75+
76+
return {
77+
beforeAll: () => {
78+
root = Fantom.createRoot();
79+
const children = Array.from({length: width}, (_, i) => (
80+
<View key={i} nativeID={`child-${i}`} />
81+
));
82+
original = (
83+
<View ref={parentRef} style={{backgroundColor: 'red'}}>
84+
{children}
85+
</View>
86+
);
87+
let nextChildren: React.Node = children;
88+
if (workload === 'append') {
89+
nextChildren = [...children, <View key="added" nativeID="added" />];
90+
} else if (workload === 'replace') {
91+
nextChildren = Array.from({length: width}, (_, i) => (
92+
<View key={`replacement-${i}`} nativeID={`replacement-${i}`} />
93+
));
94+
} else if (workload === 'reverse') {
95+
nextChildren = [...children].reverse();
96+
} else if (workload === 'descendant') {
97+
nextChildren = [
98+
...children.slice(0, -1),
99+
<View key={width - 1} nativeID={`child-${width - 1}`}>
100+
<View nativeID="descendant" />
101+
</View>,
102+
];
103+
}
104+
updated = (
105+
<View
106+
ref={parentRef}
107+
style={{backgroundColor: workload === 'props' ? 'blue' : 'red'}}>
108+
{nextChildren}
109+
</View>
110+
);
111+
commit = () => Fantom.runTask(() => root.render(updated));
112+
Fantom.runTask(() => root.render(original));
113+
for (let i = 0; i < observerCount; i++) {
114+
const observer = new MutationObserver(records => {
115+
callbackCount++;
116+
lastRecords = records;
117+
});
118+
observer.observe(nullthrows(parentRef.current), {
119+
childList: true,
120+
subtree,
121+
});
122+
observers.push(observer);
123+
}
124+
},
125+
beforeEach: () => {
126+
Fantom.runTask(() => root.render(original));
127+
callbackCount = 0;
128+
lastRecords = [];
129+
},
130+
afterEach: () => {
131+
const changes =
132+
workload === 'append' ||
133+
workload === 'replace' ||
134+
workload === 'descendant';
135+
expect(callbackCount).toBe(changes ? observerCount : 0);
136+
if (changes && observerCount > 0) {
137+
expect(lastRecords.length).toBe(1);
138+
const record = lastRecords[0];
139+
expect(record.type).toBe('childList');
140+
expect(record.addedNodes.length).toBe(
141+
workload === 'replace' ? width : 1,
142+
);
143+
expect(record.removedNodes.length).toBe(
144+
workload === 'replace' ? width : 0,
145+
);
146+
}
147+
},
148+
afterAll: () => {
149+
for (const observer of observers) {
150+
observer.disconnect();
151+
}
152+
root.destroy();
153+
},
154+
};
155+
},
156+
);

0 commit comments

Comments
 (0)