From 8fccdc53b337d3ad22ef6374a1a21b1fc7444b9e Mon Sep 17 00:00:00 2001 From: Tomasz Zawadzki Date: Mon, 16 Nov 2026 12:24:26 +0100 Subject: [PATCH] Stop walking the shadow tree in UIManager::updateShadowTree once every node is found MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## Summary: With `cxxNativeAnimatedEnabled`, C++ Animated commits animated values into the ShadowTree through `UIManager::updateShadowTree` on frames that change layout props and on the final frame (`NativeAnimatedNodesManagerProvider` → `MergedValueDispatcher`). To find the shadow nodes for the given tags, it walks each surface breadth-first from the root, and it kept walking after every tag had been found. So every call visited every node of the surface, however few views were animating. The walk now stops as soon as `remainingTagToProps` is empty. At that point every requested node and its ancestors are already in `shadowNodesToUpdate` with their depths, so the rest of the tree is never used. The enumeration across surfaces already stops on the same condition; this adds the check inside a surface. `updateShadowTree` is on this path when C++ Animated is on and the shared animation backend is off: an app that turns on `cxxNativeAnimatedEnabled` on 0.88 or 0.89, or the experimental release level on main. The stable release level on main turns on `useSharedAnimatedBackend`, which commits through `AnimationBackend` and does not call `updateShadowTree`. Not in this PR: `shadowNodesToUpdate` is de-duplicated with a linear `find_if`, which is quadratic in the number of updated nodes and their ancestors. ## Changelog: [GENERAL] [CHANGED] - Stop walking the shadow tree in `UIManager::updateShadowTree` once every requested node has been found ## Test Plan: - `yarn fantom 'Animated-itest|AnimatedComposition-itest|TimingAnimation-itest'`: 3 suites, 142 tests passed. Fantom enables `cxxNativeAnimatedEnabled`, and `Animated-itest` runs with `useSharedAnimatedBackend` both off and on; with it off, native-driven animations commit through `updateShadowTree`. ### Benchmarks RNTester release builds of this branch with and without the change, on real phones. Both builds turn on C++ Animated with `useSharedAnimatedBackend` off. The screen has 1,000 static views and 10 views whose opacity is animated with the native driver; every finished animation commits through `updateShadowTree`. Medians of a timer around `updateShadowTree`, 6 rounds of 20 s per scenario, alternating order: | Phone | Animated views early in the tree | Animated views at the end of the tree | |---|---|---| | Pixel 9a | 4,304 → 2,573 µs (−40%), 1,068 → 18 nodes visited | 9,959 → 9,814 µs (−1%), 1,070 nodes visited | | moto g57 power | 7,996 → 6,274 µs (−22%), 1,090 → 28 nodes visited | 26,441 → 26,174 µs (−1%), 1,092 nodes visited | | OPPO CPH2269 | 5,277 → 3,661 µs (−31%), 1,090 → 28 nodes visited | 12,889 → 13,550 µs (+5%), 1,092 nodes visited | | Galaxy A16 | 6,675 → 5,444 µs (−18%), 1,068 → 18 nodes visited | 14,347 → 14,421 µs (+1%), 1,070 nodes visited | - The timer also covers cloning and committing, which this PR does not change. - With the animated views last, both builds visit the same nodes, so the −1% to +5% there is noise. - iPhone numbers are pending. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Differential Revision: D131743138 Pull Request resolved: https://github.com/react/react-native/pull/59978 --- .../react/renderer/uimanager/UIManagerUpdateShadowTree.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/packages/react-native/ReactCommon/react/renderer/uimanager/UIManagerUpdateShadowTree.cpp b/packages/react-native/ReactCommon/react/renderer/uimanager/UIManagerUpdateShadowTree.cpp index 8fb2e50ad31..50e6e545d62 100644 --- a/packages/react-native/ReactCommon/react/renderer/uimanager/UIManagerUpdateShadowTree.cpp +++ b/packages/react-native/ReactCommon/react/renderer/uimanager/UIManagerUpdateShadowTree.cpp @@ -141,6 +141,13 @@ void UIManager::updateShadowTree( // Add all its ancestors to shadowNodesToUpdate list addAncestorsToUpdateList( shadowNode, rootShadowNode, shadowNodesToUpdate); + + // Every node and its ancestors are in the list, so the rest of the + // tree does not need to be visited. + if (remainingTagToProps.empty()) { + deque.clear(); + break; + } } for (const auto& child : shadowNode->getChildren()) { deque.push_back(child);