Skip to content

Commit daf96d4

Browse files
javacheOpen Source Bot
andauthored
Don't clone zeroed out display: none subtrees on every layout pass (#2055) (#59777)
Co-authored-by: Open Source Bot <generatedunixname499836121@meta.com>
1 parent 3d6462e commit daf96d4

1 file changed

Lines changed: 38 additions & 3 deletions

File tree

‎packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp‎

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -663,15 +663,50 @@ static bool measureNodeWithFixedSize(
663663
return false;
664664
}
665665

666+
static bool isLayoutZeroedOutRecursively(const yoga::Node* const node) {
667+
if (!node->getHasNewLayout()) {
668+
return false;
669+
}
670+
// `configVersion` only validates the layout cache, which is empty after
671+
// zeroing out. Embedders set it again when they give a cloned node a config.
672+
LayoutResults zeroedOutLayout;
673+
zeroedOutLayout.setDimension(Dimension::Width, 0);
674+
zeroedOutLayout.setDimension(Dimension::Height, 0);
675+
zeroedOutLayout.configVersion = node->getLayout().configVersion;
676+
if (!(node->getLayout() == zeroedOutLayout)) {
677+
return false;
678+
}
679+
for (const auto child : node->getChildren()) {
680+
if (!isLayoutZeroedOutRecursively(child)) {
681+
return false;
682+
}
683+
}
684+
return true;
685+
}
686+
666687
void zeroOutLayoutRecursively(yoga::Node* const node) {
667688
node->getLayout() = {};
668689
node->setLayoutDimension(0, Dimension::Width);
669690
node->setLayoutDimension(0, Dimension::Height);
670691
node->setHasNewLayout(true);
671692

672-
node->cloneChildrenIfNeeded();
673-
for (const auto child : node->getChildren()) {
674-
zeroOutLayoutRecursively(child);
693+
// A display: none subtree is zeroed out on every layout pass of its parent.
694+
// Most of it is usually still zeroed out from an earlier pass, but owned by
695+
// an earlier tree, so cloning it to zero it out again changes nothing. Only
696+
// clone the children, and recurse, when part of the subtree needs it.
697+
//
698+
// This relies on the embedder leaving `hasNewLayout` set under display: none
699+
// nodes. Children that are skipped keep their owner from the earlier tree.
700+
bool didCloneChildren = false;
701+
for (size_t i = 0; i < node->getChildCount(); i++) {
702+
if (isLayoutZeroedOutRecursively(node->getChild(i))) {
703+
continue;
704+
}
705+
if (!didCloneChildren) {
706+
node->cloneChildrenIfNeeded();
707+
didCloneChildren = true;
708+
}
709+
zeroOutLayoutRecursively(node->getChild(i));
675710
}
676711
}
677712

0 commit comments

Comments
 (0)