Repository navigation
Index MutationObserver child matching for wide lists - #60269
Open
rubennorte wants to merge 3 commits into
Open
rubennorte wants to merge 3 commits into
rubennorte wants to merge 3 commits into
Conversation
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. Values are milliseconds of end-to-end CPU time, aggregated as the median of three process runs' p50 values. Each scenario uses at least 100 measured iterations, at least 250 ms measured execution and 10 warmup iterations. | Workload | Children | Observers | Baseline CPU time (ms) | | --- | ---: | ---: | ---: | | Parent props only, deep observation | 1,024 | 1 | 4.814 | | Append one child | 1,024 | 1 | 7.810 | | Add a grandchild in the last retained child | 1,024 | 1 | 8.223 | | Reverse retained children | 1,024 | 1 | 10.935 | | Replace all children | 1,024 | 1 | 38.381 | | Parent props only, deep observation | 256 | 10 | 2.144 | These are synthetic single-worker measurements, not isolated observer timings or device/app latency guarantees. The following optimizations use this benchmark unchanged. Changelog: [Internal] Differential Revision: D134249229
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 Use the unchanged Fantom benchmarks from the preceding change. Optimized native/JS code and Hermes bytecode; milliseconds of end-to-end update CPU time; median of three process runs' p50 values. Setup/reset/assertion/cleanup are outside the timed interval. | Workload | Children | Observers | Baseline (ms) | Shared-vector fast path (ms) | Reduction | | --- | ---: | ---: | ---: | ---: | ---: | | Parent props only, deep observation | 1,024 | 1 | 4.814 | 2.277 | 52.7% | | Parent props only, deep observation | 256 | 10 | 2.144 | 0.623 | 70.9% | All 47 scenarios passed in three runs of this isolated optimization. The append and descendant-update cases still require child matching and showed no meaningful improvement from this fast path alone. Two-child median changes were comparable to unobserved controls. These are synthetic single-worker CPU measurements, not device/app latency guarantees. Changelog: [General][Changed] - Reduce `MutationObserver` overhead for commits with unchanged child vectors Differential Revision: D134249269
Summary: ## Context When observed child vectors differ, MutationObserver searches the complete opposite list for every old and new child. After the shared-vector fast path, this remains quadratic work for appends, reorders and changes to one descendant of a wide observed node. ## Changes - Index new child families when both lists contain at least 16 children, reducing matching to expected linear time with temporary storage proportional to the new list. Keep the original linear searches for smaller lists. - Preserve the first matching new revision for each family, old-child traversal/removal order, new-child addition order, descendant-before-parent records and per-observer deduplication. Matches are not consumed, so repeated-family membership semantics remain unchanged. - Extend the existing public-API regression suite with mixed reorder/descendant/addition/removal cases at widths 15, 16 and 32, in dev and opt modes. ## Benchmark results Use the unchanged Fantom benchmarks from the first change. Optimized native/JS code and Hermes bytecode. Each number is milliseconds of end-to-end update CPU time, aggregated as the median of three process runs' p50 values. The parent already includes the shared-vector fast path; reductions below isolate indexed matching's incremental benefit. | Workload | Children | Observers | Original baseline (ms) | Parent: fast path (ms) | With indexed matching (ms) | Reduction vs. parent | | --- | ---: | ---: | ---: | ---: | ---: | ---: | | Append one child | 1,024 | 1 | 7.810 | 7.631 | 5.693 | 25.4% | | Add a grandchild in the last retained child | 1,024 | 1 | 8.223 | 8.052 | 5.711 | 29.1% | | Reverse retained children | 1,024 | 1 | 10.935 | 11.555 | 9.227 | 20.1% | | Replace all children | 1,024 | 1 | 38.381 | 38.263 | 33.454 | 12.6% | Relative to the original baseline, the complete stack reduces append latency by 27.1% and the descendant-update latency by 30.6% at width 1,024. Property-only improvements belong to the preceding shared-vector optimization, not this index. All 47 scenarios passed across three runs per variant. Each scenario used at least 100 measured iterations, at least 250 ms measured execution and 10 warmup iterations. Setup, reset renders, assertions and cleanup are excluded; reconciliation, observation, layout/mount work, record conversion and callback delivery are included. The unobserved width-1,024 append and descendant controls were essentially unchanged across original and indexed variants (4.966 to 4.968 ms and 5.006 to 5.015 ms). Two-child median movements were approximately 0-3%, comparable to controls; 16 is a conservative cutoff, not a tuned optimum. These are synthetic single-worker CPU measurements, not isolated observer timings or device/app latency guarantees. Changelog: [General][Changed] - Reduce `MutationObserver` child matching overhead for wide observed lists Differential Revision: D134242657
|
@rubennorte has exported this pull request. If you are a Meta employee, you can view the originating Diff in D134242657. |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Summary:
Context
When observed child vectors differ, MutationObserver searches the complete opposite list for every old and new child. After the shared-vector fast path, this remains quadratic work for appends, reorders and changes to one descendant of a wide observed node.
Changes
Benchmark results
Use the unchanged Fantom benchmarks from the first change. Optimized native/JS code and Hermes bytecode. Each number is milliseconds of end-to-end update CPU time, aggregated as the median of three process runs' p50 values. The parent already includes the shared-vector fast path; reductions below isolate indexed matching's incremental benefit.
Relative to the original baseline, the complete stack reduces append latency by 27.1% and the descendant-update latency by 30.6% at width 1,024. Property-only improvements belong to the preceding shared-vector optimization, not this index.
All 47 scenarios passed across three runs per variant. Each scenario used at least 100 measured iterations, at least 250 ms measured execution and 10 warmup iterations. Setup, reset renders, assertions and cleanup are excluded; reconciliation, observation, layout/mount work, record conversion and callback delivery are included. The unobserved width-1,024 append and descendant controls were essentially unchanged across original and indexed variants (4.966 to 4.968 ms and 5.006 to 5.015 ms). Two-child median movements were approximately 0-3%, comparable to controls; 16 is a conservative cutoff, not a tuned optimum. These are synthetic single-worker CPU measurements, not isolated observer timings or device/app latency guarantees.
Changelog:
[General][Changed] - Reduce
MutationObserverchild matching overhead for wide observed listsDifferential Revision: D134242657