From aae8d6650ec8fcc68aeb1c4c486fa67f2402f2a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rub=C3=A9n=20Norte?= Date: Fri, 27 Nov 2026 03:22:15 -0800 Subject: [PATCH] Reuse IntersectionObserver target ancestry (#60311) Summary: ## Context Explicit-root intersection updates resolve target-to-root ancestry even after resolving the target-to-surface path. This repeats allocations, parent-family locking, and sibling searches. Implicit roots also copy an ancestor list that can be reused directly. ## Changes Derive the explicit-root path from the already resolved target ancestry after verifying root membership, and pass the existing list directly for implicit roots. Preserve clipping and detached/non-ancestor behavior. Add a public root-membership integration case and evaluate deep implicit/explicit roots using the shared benchmark-only baseline; this diff adds no benchmark code. ## Benchmark results Optimized Fantom (native + Hermes bytecode), headless Linux, same CPU affinity. The benchmark-only baseline and final stack use the same base containing landed scrollMargin support. Values are median CPU time per complete invocation from the current runs; setup/assertions are outside timed work, and scaling cases have 100 measured invocations. These are workload-specific measurements, not isolated profiler timings or app-wide speedups. | Benchmark | Baseline CPU | Current CPU | CPU reduction | | --- | --- | --- | --- | | `Scroll 100 targets, depth 40, 1 thresholds, implicit root` | 38.032 ms | 38.851 ms | -2.2% (within noise) | | `Scroll 100 targets, depth 40, 1 thresholds, explicit root` | 46.293 ms | 40.505 ms | 12.5% | The ancestry cases use one threshold and emit no callbacks during timed updates. Times include mounting; the small implicit-root difference is within run-to-run variation. Clipping and scrollMargin semantics are unchanged. Changelog: [General][Changed] - Reduce IntersectionObserver ancestor traversal overhead Reviewed By: javache Differential Revision: D134250309 --- .../intersection/IntersectionObserver.cpp | 14 ++++-- .../__tests__/IntersectionObserver-itest.js | 45 +++++++++++++++++++ 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/packages/react-native/ReactCommon/react/renderer/observers/intersection/IntersectionObserver.cpp b/packages/react-native/ReactCommon/react/renderer/observers/intersection/IntersectionObserver.cpp index 79a5eb97950c..921fe8a388e8 100644 --- a/packages/react-native/ReactCommon/react/renderer/observers/intersection/IntersectionObserver.cpp +++ b/packages/react-native/ReactCommon/react/renderer/observers/intersection/IntersectionObserver.cpp @@ -14,6 +14,7 @@ #include #include #include +#include #include namespace facebook::react { @@ -280,15 +281,20 @@ IntersectionObserver::updateIntersectionObservation( rootMarginBoundingRect, targetBoundingRect, {}, time); } - auto targetToRootAncestors = hasExplicitRoot - ? targetShadowNodeFamily_->getAncestors(*getShadowNode(rootAncestors)) - : targetAncestors; + ShadowNodeFamily::AncestorList targetToRootAncestors; + if (hasExplicitRoot && targetAncestors.size() > rootAncestors.size() && + &targetAncestors[rootAncestors.size()].first.get() == + getShadowNode(rootAncestors).get()) { + targetToRootAncestors.assign( + targetAncestors.begin() + std::ssize(rootAncestors), + targetAncestors.end()); + } auto intersection = computeIntersection( rootBoundingRect, rootClipRect, targetBoundingRect, - targetToRootAncestors, + hasExplicitRoot ? targetToRootAncestors : targetAncestors, hasExplicitRoot, scrollMargins_); diff --git a/packages/react-native/src/private/webapis/intersectionobserver/__tests__/IntersectionObserver-itest.js b/packages/react-native/src/private/webapis/intersectionobserver/__tests__/IntersectionObserver-itest.js index c8e697c23b6e..5ad0daa26e4f 100644 --- a/packages/react-native/src/private/webapis/intersectionobserver/__tests__/IntersectionObserver-itest.js +++ b/packages/react-native/src/private/webapis/intersectionobserver/__tests__/IntersectionObserver-itest.js @@ -5195,6 +5195,51 @@ describe('IntersectionObserver', () => { root.destroy(); }); + it('should resolve explicit roots through the target ancestor path', () => { + const observationRootRef = createRef(); + const targetRef = createRef(); + const unrelatedRootRef = createRef(); + const root = Fantom.createRoot({viewportWidth: 100, viewportHeight: 100}); + Fantom.runTask(() => { + root.render( + <> + + + + + + + + + , + ); + }); + const target = nullthrows(targetRef.current); + const observationRoot = nullthrows(observationRootRef.current); + const cases: ReadonlyArray<[HostInstance, number]> = [ + [observationRoot, 1], + [nullthrows(unrelatedRootRef.current), 0], + [target, 0], + ]; + for (const [candidateRoot, expectedRatio] of cases) { + const callback = jest.fn(); + Fantom.runTask(() => { + observer = new IntersectionObserver(callback, {root: candidateRoot}); + observer.observe(target); + }); + expect(callback).toHaveBeenCalledTimes(1); + const entry = callback.mock.lastCall[0][0]; + expect(entry.target).toBe(target); + expect(entry.intersectionRatio).toBe(expectedRatio); + expect(entry.isIntersecting).toBe(expectedRatio === 1); + Fantom.runTask(() => observer.disconnect()); + } + root.destroy(); + }); + describe('disconnect', () => { it('should do nothing if no targets are observed (not fail)', () => { const callback = jest.fn();