Skip to content

Commit 41118bd

Browse files
shubh73intergalacticspacehighwayclaude
authored
Only clone the display: none children that need zeroing out (#2058)
Follow-up to #2055, from review. `zeroOutLayoutRecursively` cloned all of a node's unowned children as soon as one of them needed zeroing out. It now clones only that child, through a new `Node::cloneChildIfNeeded(index)` that `cloneChildrenIfNeeded` also uses. Co-authored-by: nishan <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 0747590 commit 41118bd

4 files changed

Lines changed: 26 additions & 24 deletions

File tree

‎tests/YGPersistentNodeCloningTest.cpp‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -358,8 +358,9 @@ TEST_F(
358358
std::make_shared<NodeWrapper>(*root, std::vector{hiddenPrime});
359359

360360
std::vector<NodeWrapper*> nodesCloned;
361-
// We should only need to clone the shared siblings on the path to "New",
362-
// "C" and "D", but not the zeroed out subtree of "D"
361+
// Only "New" needs zeroing out, and its ancestors are new revisions that own
362+
// it. The shared siblings on its path, "C" and "D", are already zeroed out,
363+
// so nothing should be cloned
363364
onClone = [&](YGNodeConstRef oldNode,
364365
YGNodeConstRef /*owner*/,
365366
size_t /*childIndex*/) {
@@ -369,7 +370,7 @@ TEST_F(
369370
YGNodeCalculateLayout(
370371
rootPrime->node, YGUndefined, YGUndefined, YGDirectionLTR);
371372

372-
EXPECT_EQ(nodesCloned, (std::vector<NodeWrapper*>{d.get(), c.get()}));
373+
EXPECT_TRUE(nodesCloned.empty());
373374
expectZeroedOut(newNode->node);
374375
expectZeroedOut(e->node);
375376
}

‎yoga/algorithm/CalculateLayout.cpp‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -693,19 +693,15 @@ void zeroOutLayoutRecursively(yoga::Node* const node) {
693693
// A display: none subtree is zeroed out on every layout pass of its parent.
694694
// Most of it is usually still zeroed out from an earlier pass, but owned by
695695
// 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.
696+
// clone and recurse into the children whose subtree needs it.
697697
//
698698
// This relies on the embedder leaving `hasNewLayout` set under display: none
699699
// nodes. Children that are skipped keep their owner from the earlier tree.
700-
bool didCloneChildren = false;
701700
for (size_t i = 0; i < node->getChildCount(); i++) {
702701
if (isLayoutZeroedOutRecursively(node->getChild(i))) {
703702
continue;
704703
}
705-
if (!didCloneChildren) {
706-
node->cloneChildrenIfNeeded();
707-
didCloneChildren = true;
708-
}
704+
node->cloneChildIfNeeded(i);
709705
zeroOutLayoutRecursively(node->getChild(i));
710706
}
711707
}

‎yoga/node/Node.cpp‎

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -418,22 +418,26 @@ void Node::clearChildren() {
418418
// Other Methods
419419

420420
void Node::cloneChildrenIfNeeded() {
421-
size_t i = 0;
422-
for (Node*& child : children_) {
423-
if (child->getOwner() != this) {
424-
child = resolveRef(config_->cloneNode(child, this, i));
425-
child->setOwner(this);
421+
for (size_t i = 0; i < children_.size(); i++) {
422+
cloneChildIfNeeded(i);
423+
}
424+
}
426425

427-
if (child->style().display() == Display::Contents) [[unlikely]] {
428-
// The contents node's children are treated as children of the
429-
// contents node's parent for layout purposes, so they need
430-
// to be cloned as well.
431-
child->cloneChildrenIfNeeded();
432-
} else if (child->hasContentsChildren()) [[unlikely]] {
433-
child->cloneContentsChildrenIfNeeded();
434-
}
435-
}
436-
i += 1;
426+
void Node::cloneChildIfNeeded(size_t index) {
427+
Node*& child = children_[index];
428+
if (child->getOwner() == this) {
429+
return;
430+
}
431+
child = resolveRef(config_->cloneNode(child, this, index));
432+
child->setOwner(this);
433+
434+
if (child->style().display() == Display::Contents) [[unlikely]] {
435+
// The contents node's children are treated as children of the
436+
// contents node's parent for layout purposes, so they need
437+
// to be cloned as well.
438+
child->cloneChildrenIfNeeded();
439+
} else if (child->hasContentsChildren()) [[unlikely]] {
440+
child->cloneContentsChildrenIfNeeded();
437441
}
438442
}
439443

‎yoga/node/Node.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ class YG_EXPORT Node : public ::YGNode {
317317
void removeChild(size_t index);
318318

319319
void cloneChildrenIfNeeded();
320+
void cloneChildIfNeeded(size_t index);
320321
void cloneContentsChildrenIfNeeded();
321322
void markDirtyAndPropagate();
322323
float resolveFlexGrow() const;

0 commit comments

Comments
 (0)