Skip to content

Commit 98e424c

Browse files
authored
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 Pull Request resolved: #60311
1 parent 5279b1b commit 98e424c

2 files changed

Lines changed: 55 additions & 4 deletions

File tree

‎packages/react-native/ReactCommon/react/renderer/observers/intersection/IntersectionObserver.cpp‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <react/renderer/css/CSSPercentage.h>
1515
#include <react/renderer/css/CSSValueParser.h>
1616
#include <react/renderer/graphics/RectangleEdges.h>
17+
#include <iterator>
1718
#include <utility>
1819

1920
namespace facebook::react {
@@ -280,15 +281,20 @@ IntersectionObserver::updateIntersectionObservation(
280281
rootMarginBoundingRect, targetBoundingRect, {}, time);
281282
}
282283

283-
auto targetToRootAncestors = hasExplicitRoot
284-
? targetShadowNodeFamily_->getAncestors(*getShadowNode(rootAncestors))
285-
: targetAncestors;
284+
ShadowNodeFamily::AncestorList targetToRootAncestors;
285+
if (hasExplicitRoot && targetAncestors.size() > rootAncestors.size() &&
286+
&targetAncestors[rootAncestors.size()].first.get() ==
287+
getShadowNode(rootAncestors).get()) {
288+
targetToRootAncestors.assign(
289+
targetAncestors.begin() + std::ssize(rootAncestors),
290+
targetAncestors.end());
291+
}
286292

287293
auto intersection = computeIntersection(
288294
rootBoundingRect,
289295
rootClipRect,
290296
targetBoundingRect,
291-
targetToRootAncestors,
297+
hasExplicitRoot ? targetToRootAncestors : targetAncestors,
292298
hasExplicitRoot,
293299
scrollMargins_);
294300

‎packages/react-native/src/private/webapis/intersectionobserver/__tests__/IntersectionObserver-itest.js‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5195,6 +5195,51 @@ describe('IntersectionObserver', () => {
51955195
root.destroy();
51965196
});
51975197

5198+
it('should resolve explicit roots through the target ancestor path', () => {
5199+
const observationRootRef = createRef<HostInstance>();
5200+
const targetRef = createRef<HostInstance>();
5201+
const unrelatedRootRef = createRef<HostInstance>();
5202+
const root = Fantom.createRoot({viewportWidth: 100, viewportHeight: 100});
5203+
Fantom.runTask(() => {
5204+
root.render(
5205+
<>
5206+
<View ref={observationRootRef} style={{width: 100, height: 100}}>
5207+
<View>
5208+
<View>
5209+
<View ref={targetRef} style={{width: 20, height: 20}} />
5210+
</View>
5211+
</View>
5212+
</View>
5213+
<View
5214+
ref={unrelatedRootRef}
5215+
style={{position: 'absolute', width: 100, height: 100}}
5216+
/>
5217+
</>,
5218+
);
5219+
});
5220+
const target = nullthrows(targetRef.current);
5221+
const observationRoot = nullthrows(observationRootRef.current);
5222+
const cases: ReadonlyArray<[HostInstance, number]> = [
5223+
[observationRoot, 1],
5224+
[nullthrows(unrelatedRootRef.current), 0],
5225+
[target, 0],
5226+
];
5227+
for (const [candidateRoot, expectedRatio] of cases) {
5228+
const callback = jest.fn();
5229+
Fantom.runTask(() => {
5230+
observer = new IntersectionObserver(callback, {root: candidateRoot});
5231+
observer.observe(target);
5232+
});
5233+
expect(callback).toHaveBeenCalledTimes(1);
5234+
const entry = callback.mock.lastCall[0][0];
5235+
expect(entry.target).toBe(target);
5236+
expect(entry.intersectionRatio).toBe(expectedRatio);
5237+
expect(entry.isIntersecting).toBe(expectedRatio === 1);
5238+
Fantom.runTask(() => observer.disconnect());
5239+
}
5240+
root.destroy();
5241+
});
5242+
51985243
describe('disconnect', () => {
51995244
it('should do nothing if no targets are observed (not fail)', () => {
52005245
const callback = jest.fn();

0 commit comments

Comments
 (0)