Skip to content

Index MutationObserver child matching for wide lists - #60269

Open
rubennorte wants to merge 3 commits into
react:mainfrom
rubennorte:export-D134242657
Open

rubennorte wants to merge 3 commits into
react:mainfrom
rubennorte:export-D134242657

Conversation

@rubennorte

Copy link
Copy Markdown
Member

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

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
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 24, 2026
@meta-codesync

meta-codesync Bot commented Nov 24, 2026

Copy link
Copy Markdown

@rubennorte has exported this pull request. If you are a Meta employee, you can view the originating Diff in D134242657.

@github-actions

Copy link
Copy Markdown

Visual regression

1 visual difference(s) at 09d6127cf8f9. Visual differences need maintainer approval. Review the report, then apply visual-change-approved.

ios switch/basic/toggled: Changed, 346 px (0.014%)

diff

Expected and Captured
Expected Captured
expected captured

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. meta-exported p: Facebook Partner: Facebook Partner

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant