Repository navigation
Speed up disconnecting the final ResizeObserver - #60308
Merged
Merged
Conversation
Summary: ## Context Disconnecting a `ResizeObserver` currently unobserves every target separately. Native `unobserve()` repeatedly scans the remaining observations, so tearing down a large observation set does quadratic work even when the final native `disconnect()` will clear the same state. ## Changes - When the observer is the sole registered observer, use the existing native disconnect to remove all observations together, then clear its JavaScript target map and registration ID. - Preserve the per-target unobserve path while any other observer remains registered. - Add benchmarks for idle work, unrelated and actual resizes, observer fan-out, final teardown, and sparse trees. - Extend the existing integration suite with many-target hide/removal, disconnect/re-observe, and callback reentrancy cases. ## Benchmark results Final-observer `disconnect()` measured on Linux x86_64 (AMD EPYC 9D64) with optimized native code and Hermes bytecode. Before/after values are the median of the mean CPU latency from three independent runs per variant on the same worker, with 200 measured teardown iterations and 20 warmup iterations per run. | Observed targets | Before | After | Speedup | | --- | --- | --- | --- | | 1 | 5.24 microseconds | 3.69 microseconds | 1.4x | | 100 | 209.01 microseconds | 6.70 microseconds | 31.2x | | 1,000 | 14.775 milliseconds | 38.95 microseconds | 379.3x | Registration setup is outside the timed teardown operation; these measure deregistration, not registration, resize checking, notification dispatch, or phone frame time. Reproduce with `yarn fantom ResizeObserver-benchmark-itest --benchmarks` from the React Native repository root. Changelog: [General][Changed] - Reduce the time spent disconnecting the final `ResizeObserver` with many targets. Differential Revision: D134238255
|
@rubennorte has exported this pull request. If you are a Meta employee, you can view the originating Diff in D134238255. |
javache
approved these changes
Nov 26, 2026
javache
left a comment
Contributor
There was a problem hiding this comment.
Review automatically exported from Phabricator review in Meta.
Collaborator
|
This pull request was successfully merged by @rubennorte in 9743c09 When will my fix make it into a release? | How to file a pick request? |
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
Disconnecting a
ResizeObservercurrently unobserves every target separately. Nativeunobserve()repeatedly scans the remaining observations, so tearing down a large observation set does quadratic work even when the final nativedisconnect()will clear the same state.Changes
Benchmark results
Final-observer
disconnect()measured on Linux x86_64 (AMD EPYC 9D64) with optimized native code and Hermes bytecode. Before/after values are the median of the mean CPU latency from three independent runs per variant on the same worker, with 200 measured teardown iterations and 20 warmup iterations per run.Registration setup is outside the timed teardown operation; these measure deregistration, not registration, resize checking, notification dispatch, or phone frame time.
Reproduce with
yarn fantom ResizeObserver-benchmark-itest --benchmarksfrom the React Native repository root.Changelog:
[General][Changed] - Reduce the time spent disconnecting the final
ResizeObserverwith many targets.Differential Revision: D134238255