Skip to content

Commit 7230b63

Browse files
authored
Skip copying the culling context in the differ when view culling is off
## Summary: In its two per-child loops (slicing a node's children, and walking matched child pairs), the differ calls `CullingContext::adjustCullingContextIfNeeded` for every child and reads `ReactNativeFeatureFlags::enableViewCulling()` each time. Each call builds a `ShadowViewNodePair` with a copy of the child's `ShadowView` (props, state and event emitter `shared_ptr`s) and returns a new `CullingContext` (a `Rect` and a `Transform` with its operations vector). Without view culling, the result is always an unchanged copy of the parent's context. View culling is off by default, so every commit pays for this on every child the differ visits. This change reads the flag once per call and, when culling is off, passes the parent's context to the children instead. With culling on, the same calls run in the same order as before. The other `adjustCullingContextIfNeeded` calls are in rarer paths (flattening, unflattening, deletions) and are left as they are. I found this while profiling typing in a `TextInput` on Android: each keystroke commits a state update, and the differ walks the whole screen. On a low-end phone the copies were about 5% of the differ's time. ## Changelog: [GENERAL] [CHANGED] - The differ no longer copies its view culling context for every child when view culling is off ## Test Plan: - `yarn fantom` with the tester rebuilt from this change: - `ScrollView-viewCulling-itest.js` (runs with `enableViewCulling:true`): 44 passed. - `TextInput` suites 39 passed, `ScrollView` suites 86 passed, `Mounting-itest.js` 56 passed. - Full suite: 3316 passed. The other failures are not related to this change: snapshot printing in my checkout's Jest setup (LogBox and `expect` snapshots), and the known flaky tester crash in one suite (`polyfillArrayES2023Methods-itest.js`), which passes on rerun. - `clang-format` clean (`node scripts/clang-format.js --check`). - **On a device.** RNTester release build on a Galaxy A16 (Android 16), one APK with both versions behind a local switch, typing into a `TextInput` on the TextInput example page for 12 s per run, call stacks sampled at 1 kHz, 5 rounds alternating old/new. JS thread samples spent in `adjustCullingContextIfNeeded` and the flag reads: | | before | this PR | |---|--:|--:| | per 12 s run | 13 / 9 / 12 / 12 / 12 | 2 / 1 / 1 / 2 / 0 | That is about 1.3–1.5% of the JS thread while typing (the differ itself is about 30% of it). Total differ time moves by more than that between runs, so I am not claiming more. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Differential Revision: D128599926 Pull Request resolved: #59677
1 parent 3feed51 commit 7230b63

2 files changed

Lines changed: 33 additions & 9 deletions

File tree

‎packages/react-native/ReactCommon/react/renderer/mounting/Differentiator.cpp‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <react/debug/react_native_assert.h>
1212
#include <react/featureflags/ReactNativeFeatureFlags.h>
1313
#include <algorithm>
14+
#include <optional>
1415
#include <unordered_map>
1516
#include "internal/CullingContext.h"
1617
#include "internal/ShadowViewNodePair.h"
@@ -897,6 +898,9 @@ void calculateShadowViewMutations(
897898
return;
898899
}
899900

901+
const bool isViewCullingEnabled =
902+
ReactNativeFeatureFlags::enableViewCulling();
903+
900904
size_t index = 0;
901905

902906
// Lists of mutations
@@ -946,10 +950,20 @@ void calculateShadowViewMutations(
946950
oldChildPair.shadowView, newChildPair.shadowView, parentTag));
947951
}
948952

949-
auto adjustedOldCullingContext =
950-
oldCullingContext.adjustCullingContextIfNeeded(oldChildPair);
951-
auto adjustedNewCullingContext =
952-
newCullingContext.adjustCullingContextIfNeeded(newChildPair);
953+
// Without view culling, adjustCullingContextIfNeeded doesn't change the
954+
// contexts, so skip the copies.
955+
std::optional<CullingContext> adjustedOld;
956+
std::optional<CullingContext> adjustedNew;
957+
if (isViewCullingEnabled) {
958+
adjustedOld =
959+
oldCullingContext.adjustCullingContextIfNeeded(oldChildPair);
960+
adjustedNew =
961+
newCullingContext.adjustCullingContextIfNeeded(newChildPair);
962+
}
963+
const auto& adjustedOldCullingContext =
964+
adjustedOld ? *adjustedOld : oldCullingContext;
965+
const auto& adjustedNewCullingContext =
966+
adjustedNew ? *adjustedNew : newCullingContext;
953967

954968
// Recursively update tree if ShadowNode pointers are not equal
955969
if (!oldChildPair.flattened &&

‎packages/react-native/ReactCommon/react/renderer/mounting/internal/sliceChildShadowNodeViewPairs.cpp‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "sliceChildShadowNodeViewPairs.h"
99
#include <react/featureflags/ReactNativeFeatureFlags.h>
1010
#include <react/renderer/core/LayoutableShadowNode.h>
11+
#include <optional>
1112

1213
#include "ShadowViewNodePair.h"
1314

@@ -144,14 +145,16 @@ void sliceChildShadowNodeViewPairsRecursively(
144145
Point layoutOffset,
145146
const ShadowNode& shadowNode,
146147
const CullingContext& cullingContext) {
148+
const bool isViewCullingEnabled =
149+
ReactNativeFeatureFlags::enableViewCulling();
147150
for (const auto& sharedChildShadowNode : shadowNode.getChildren()) {
148151
auto& childShadowNode = *sharedChildShadowNode;
149152
if (isHiddenWhenSlicing(childShadowNode)) {
150153
continue;
151154
}
152155
auto shadowView = ShadowView(childShadowNode);
153156

154-
if (ReactNativeFeatureFlags::enableViewCulling()) {
157+
if (isViewCullingEnabled) {
155158
auto isViewCullable =
156159
!shadowView.traits.check(
157160
ShadowNodeTraits::Trait::Unstable_uncullableView) &&
@@ -181,8 +184,15 @@ void sliceChildShadowNodeViewPairsRecursively(
181184
}
182185
}
183186

184-
auto cullingContextCopy = cullingContext.adjustCullingContextIfNeeded(
185-
{.shadowView = shadowView, .shadowNode = &childShadowNode});
187+
// Without view culling, adjustCullingContextIfNeeded doesn't change the
188+
// context, so skip the copy.
189+
std::optional<CullingContext> adjustedCullingContext;
190+
if (isViewCullingEnabled) {
191+
adjustedCullingContext = cullingContext.adjustCullingContextIfNeeded(
192+
{.shadowView = shadowView, .shadowNode = &childShadowNode});
193+
}
194+
const auto& childCullingContext =
195+
adjustedCullingContext ? *adjustedCullingContext : cullingContext;
186196

187197
auto childSlice = sliceChild(
188198
shadowNode, childShadowNode, std::move(shadowView), layoutOffset);
@@ -200,7 +210,7 @@ void sliceChildShadowNodeViewPairsRecursively(
200210
scope,
201211
childSlice.origin,
202212
childShadowNode,
203-
cullingContextCopy);
213+
childCullingContext);
204214
} else {
205215
size_t pairListSize = pairList.size();
206216
sliceChildShadowNodeViewPairsRecursively(
@@ -209,7 +219,7 @@ void sliceChildShadowNodeViewPairsRecursively(
209219
scope,
210220
childSlice.origin,
211221
childShadowNode,
212-
cullingContextCopy);
222+
childCullingContext);
213223
}
214224
}
215225
}

0 commit comments

Comments
 (0)