From af45ed4354ee8396e91528cf60a749eead3bf6ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Norte?= Date: Thu, 26 Nov 2026 06:22:29 -0800 Subject: [PATCH 1/2] 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 comparative measurements were collected at the preceding stable snapshot with the same observer implementations and benchmark fixtures: 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 --- .../MutationObserver-benchmark-itest.js | 156 ++++++++++++++++++ 1 file changed, 156 insertions(+) create mode 100644 packages/react-native/src/private/webapis/mutationobserver/__tests__/MutationObserver-benchmark-itest.js diff --git a/packages/react-native/src/private/webapis/mutationobserver/__tests__/MutationObserver-benchmark-itest.js b/packages/react-native/src/private/webapis/mutationobserver/__tests__/MutationObserver-benchmark-itest.js new file mode 100644 index 000000000000..5d12d11ffd20 --- /dev/null +++ b/packages/react-native/src/private/webapis/mutationobserver/__tests__/MutationObserver-benchmark-itest.js @@ -0,0 +1,156 @@ +/** + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + * + * @fantom_flags enableMutationObserverByDefault:true + * @flow strict-local + * @format + */ + +import '@react-native/fantom/src/setUpDefaultReactNativeEnvironment'; + +import type MutationObserverType from '../MutationObserver'; +import type MutationRecordType from '../MutationRecord'; +import type {Root} from '@react-native/fantom'; +import type {HostInstance} from 'react-native'; + +import * as Fantom from '@react-native/fantom'; +import nullthrows from 'nullthrows'; +import * as React from 'react'; +import {createRef} from 'react'; +import {View} from 'react-native'; + +declare const MutationObserver: Class; + +type Workload = 'props' | 'append' | 'replace' | 'reverse' | 'descendant'; +type Scenario = Readonly<{ + width: number, + workload: Workload, + subtree: boolean, + observers: number, +}>; + +const scenarios: Array = []; +for (const width of [2, 32, 256, 1024]) { + for (const workload of [ + 'props', + 'append', + 'replace', + 'reverse', + 'descendant', + ] as const) { + scenarios.push({width, workload, subtree: true, observers: 1}); + scenarios.push({width, workload, subtree: true, observers: 0}); + } + scenarios.push({width, workload: 'props', subtree: false, observers: 1}); +} +for (const workload of ['props', 'append', 'descendant'] as const) { + scenarios.push({width: 256, workload, subtree: true, observers: 10}); +} + +let commit: () => void; + +Fantom.unstable_benchmark + .suite('MutationObserver commits', { + minIterations: 100, + minTestExecutionTimeMs: 250, + minWarmupIterations: 10, + }) + .test.each( + scenarios, + ({width, workload, subtree, observers}) => + `${workload}, width=${width}, subtree=${String(subtree)}, observers=${observers}`, + () => commit(), + scenario => { + const {width, workload, subtree, observers: observerCount} = scenario; + const parentRef = createRef(); + let root: Root; + let original: React.MixedElement; + let updated: React.MixedElement; + const observers: Array = []; + let callbackCount = 0; + let lastRecords: ReadonlyArray = []; + + return { + beforeAll: () => { + root = Fantom.createRoot(); + const children = Array.from({length: width}, (_, i) => ( + + )); + original = ( + + {children} + + ); + let nextChildren: React.Node = children; + if (workload === 'append') { + nextChildren = [...children, ]; + } else if (workload === 'replace') { + nextChildren = Array.from({length: width}, (_, i) => ( + + )); + } else if (workload === 'reverse') { + nextChildren = [...children].reverse(); + } else if (workload === 'descendant') { + nextChildren = [ + ...children.slice(0, -1), + + + , + ]; + } + updated = ( + + {nextChildren} + + ); + commit = () => Fantom.runTask(() => root.render(updated)); + Fantom.runTask(() => root.render(original)); + for (let i = 0; i < observerCount; i++) { + const observer = new MutationObserver(records => { + callbackCount++; + lastRecords = records; + }); + observer.observe(nullthrows(parentRef.current), { + childList: true, + subtree, + }); + observers.push(observer); + } + }, + beforeEach: () => { + Fantom.runTask(() => root.render(original)); + callbackCount = 0; + lastRecords = []; + }, + afterEach: () => { + const changes = + workload === 'append' || + workload === 'replace' || + workload === 'descendant'; + expect(callbackCount).toBe(changes ? observerCount : 0); + if (changes && observerCount > 0) { + expect(lastRecords.length).toBe(1); + const record = lastRecords[0]; + expect(record.type).toBe('childList'); + expect(record.addedNodes.length).toBe( + workload === 'replace' ? width : 1, + ); + expect(record.removedNodes.length).toBe( + workload === 'replace' ? width : 0, + ); + } + }, + afterAll: () => { + for (const observer of observers) { + observer.disconnect(); + } + root.destroy(); + }, + }; + }, + ); From c327037d0c064195cd5ce5d73b2cb41d73d81671 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Norte?= Date: Thu, 26 Nov 2026 06:22:29 -0800 Subject: [PATCH 2/2] Skip MutationObserver diffing for shared child vectors (#60268) Summary: ## Context Cloning a parent for a property-only update can leave its child-vector object unchanged. MutationObserver currently copies both vectors and compares their child families anyway, creating allocation/reference-counting work and quadratic searches even though none of the children changed. ## Changes Borrow the child vectors by const reference and return early when the vector objects are identical. Compare vector-object addresses, not their data pointers. This preserves deep-observation behavior: changes to a descendant require a different child revision along the path, so identical child-vector storage cannot hide a descendant mutation. ## Benchmark results Optimized native/JS code and Hermes bytecode, with MutationObserver enabled. These comparative measurements were collected at the preceding stable snapshot with the same observer implementations and benchmark fixtures: 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 (ms) | Shared-vector fast path (ms) | Observed reduction | | --- | ---: | ---: | ---: | ---: | ---: | | Parent props only, deep observation | 1,024 | 1 | 3.846 | 2.076 | 46.0% | | Parent props only, deep observation | 256 | 10 | 1.843 | 0.519 | 71.9% | 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. This fast path only skips child matching when child-vector storage is shared. No append benefit is claimed: the width-1,024 fast-path-only append case measured 6.322 to 7.829 ms, while its unobserved control also varied (phase medians 4.094 to 4.431 ms; fast-phase run p50s 4.101, 5.189 and 4.431 ms). The observed slowdown is not isolated as a causal effect of this change. Changelog: [General][Changed] - Reduce `MutationObserver` overhead for commits with unchanged child vectors Reviewed By: javache Differential Revision: D134249269 --- .../renderer/observers/mutation/MutationObserver.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/react-native/ReactCommon/react/renderer/observers/mutation/MutationObserver.cpp b/packages/react-native/ReactCommon/react/renderer/observers/mutation/MutationObserver.cpp index 146bf6b7b141..2bc1036375d1 100644 --- a/packages/react-native/ReactCommon/react/renderer/observers/mutation/MutationObserver.cpp +++ b/packages/react-native/ReactCommon/react/renderer/observers/mutation/MutationObserver.cpp @@ -143,8 +143,12 @@ void MutationObserver::recordMutationsInSubtrees( processedNodes.insert(oldNode.get()); - auto oldChildren = oldNode->getChildren(); - auto newChildren = newNode->getChildren(); + const auto& oldChildren = oldNode->getChildren(); + const auto& newChildren = newNode->getChildren(); + + if (&oldChildren == &newChildren) { + return; + } std::vector> addedNodes; std::vector> removedNodes;