Skip to content

Commit 4bf3477

Browse files
javachemeta-codesync[bot]
authored andcommitted
Avoid redundant dynamic_pointer_cast in YogaLayoutableShadowNode clone (#57019)
Summary: Pull Request resolved: #57019 The clone constructor of `YogaLayoutableShadowNode` rebuilt the internal `yogaLayoutableChildren_` vector on every clone by walking every child with `dynamic_pointer_cast`. When the cloned node inherits its children list from the source (`!fragment.children`) the result is identical to the source's vector, so copy it directly instead — no RTTI required. Also `reserve()` the vector to its upper bound at the top of `updateYogaChildren()` so the rebuild on the new-children path no longer reallocates as it grows. Both changes are pure CPU-time optimizations on a hot path that shows up heavily in animation-driven tree clones; behaviour is unchanged. Changelog: [Internal] Reviewed By: christophpurrer, lenaic Differential Revision: D106287171 fbshipit-source-id: 33efd873fe4e24d91cea69fde650139bc8a89e1e
1 parent aea8785 commit 4bf3477

1 file changed

Lines changed: 14 additions & 5 deletions

File tree

packages/react-native/ReactCommon/react/renderer/components/view/YogaLayoutableShadowNode.cpp

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,11 +114,19 @@ YogaLayoutableShadowNode::YogaLayoutableShadowNode(
114114
"Yoga node must inherit dirty flag.");
115115
#endif
116116
if (!getTraits().check(ShadowNodeTraits::Trait::LeafYogaNode)) {
117-
for (auto& child : getChildren()) {
118-
if (auto layoutableChild =
119-
std::dynamic_pointer_cast<const YogaLayoutableShadowNode>(
120-
child)) {
121-
yogaLayoutableChildren_.push_back(std::move(layoutableChild));
117+
if (!fragment.children) {
118+
// Children unchanged - copy the filtered list directly from the source
119+
// node, avoiding expensive dynamic_pointer_cast on every child.
120+
yogaLayoutableChildren_ =
121+
static_cast<const YogaLayoutableShadowNode&>(sourceShadowNode)
122+
.yogaLayoutableChildren_;
123+
} else {
124+
for (auto& child : getChildren()) {
125+
if (auto layoutableChild =
126+
std::dynamic_pointer_cast<const YogaLayoutableShadowNode>(
127+
child)) {
128+
yogaLayoutableChildren_.push_back(std::move(layoutableChild));
129+
}
122130
}
123131
}
124132
}
@@ -349,6 +357,7 @@ void YogaLayoutableShadowNode::updateYogaChildren() {
349357

350358
yogaNode_.setChildren({});
351359
yogaLayoutableChildren_.clear();
360+
yogaLayoutableChildren_.reserve(getChildren().size());
352361

353362
for (size_t i = 0; i < getChildren().size(); i++) {
354363
if (auto yogaLayoutableChild =

0 commit comments

Comments
 (0)