Skip to content

Commit 0ec933a

Browse files
GitHub Workflows Supportmeta-codesync[bot]
authored andcommitted
Walk only the requested axis for a container's min-content size (#2102)
Summary: * Walk only the requested axis for a container's min-content size Why === The min-content walk behind automatic minimum sizes (css-flexbox-1 §4.5) is asked for a container's size on one axis. For every child it computed the size on both of the container's axes, summed one, took the largest of the other, and returned only the one it was asked for. The work on the other axis was wasted: a leaf was measured, and a container walked, on an axis no flex line needed. How === The walk asks each child for the requested axis alone. It sums the children where that is the container's main axis and takes the largest where it is the cross axis. Layout results are unchanged. The memo already kept a leaf to one probe per axis per layout, so the saving is one measure call per leaf, and one walk per container, on the axis nobody asked for. Test Plan ========= `cmake --build tests/build && tests/build/yogatests` passes. `nested_leaf_probed_at_most_once_per_axis_per_layout`, a measured leaf ten containers deep in a shrinking row, accepted up to two probes. It now expects one, and sees 2 without this change. `yarn gentest -h` leaves the tree clean, and `node ./scripts/clang-format.js --check` passes. Changelog: [General][Changed] - Yoga calls a measure function for an automatic minimum size only on the axis it needs * Simplify alongMainAxis check and assert probed axis in test --------- Reviewed By: javache Differential Revision: D135328393
1 parent ac9efe5 commit 0ec933a

1 file changed

Lines changed: 18 additions & 40 deletions

File tree

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

Lines changed: 18 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,13 +1026,11 @@ static MinContentSize measureOrWalkMinContentMainSize(
10261026
}
10271027

10281028
const Direction direction = node->resolveDirection(ownerDirection);
1029-
const FlexDirection nodeMainAxis =
1030-
resolveDirection(node->style().flexDirection(), direction);
1031-
const FlexDirection nodeCrossAxis =
1032-
resolveCrossDirection(nodeMainAxis, direction);
1029+
// Children add up along the container's main axis; across it the largest
1030+
// counts.
1031+
const bool alongMainAxis = isRow(node->style().flexDirection()) == wantRow;
10331032

1034-
float mainTotal = 0.0f;
1035-
float crossMax = 0.0f;
1033+
float minContent = 0.0f;
10361034
bool resolvesAgainstOwnerWidth = node->style().hasPercentPadding();
10371035

10381036
for (size_t i = 0; i < node->getChildCount(); i++) {
@@ -1042,47 +1040,27 @@ static MinContentSize measureOrWalkMinContentMainSize(
10421040
continue;
10431041
}
10441042

1045-
const MinContentSize childMain = computeChildMinContentContribution(
1043+
const MinContentSize child = computeChildMinContentContribution(
10461044
node,
10471045
i,
1048-
nodeMainAxis,
1046+
requestedAxis,
10491047
direction,
10501048
ownerWidth,
10511049
ownerHeight,
10521050
generationCount);
1053-
const MinContentSize childCross = computeChildMinContentContribution(
1054-
node,
1055-
i,
1056-
nodeCrossAxis,
1057-
direction,
1058-
ownerWidth,
1059-
ownerHeight,
1060-
generationCount);
1061-
1062-
mainTotal += childMain.size +
1063-
childStyle.computeMarginForAxis(nodeMainAxis, ownerWidth);
1064-
crossMax = std::max(
1065-
crossMax,
1066-
childCross.size +
1067-
childStyle.computeMarginForAxis(nodeCrossAxis, ownerWidth));
1051+
const float childSize =
1052+
child.size + childStyle.computeMarginForAxis(requestedAxis, ownerWidth);
1053+
minContent = alongMainAxis ? minContent + childSize
1054+
: std::max(minContent, childSize);
10681055
resolvesAgainstOwnerWidth = resolvesAgainstOwnerWidth ||
1069-
childMain.resolvesAgainstOwnerWidth ||
1070-
childCross.resolvesAgainstOwnerWidth || childStyle.hasPercentMargin();
1056+
child.resolvesAgainstOwnerWidth || childStyle.hasPercentMargin();
10711057
}
10721058

1073-
mainTotal += node->style().computeFlexStartPaddingAndBorder(
1074-
nodeMainAxis, direction, ownerWidth) +
1075-
node->style().computeFlexEndPaddingAndBorder(
1076-
nodeMainAxis, direction, ownerWidth);
1077-
crossMax += node->style().computeFlexStartPaddingAndBorder(
1078-
nodeCrossAxis, direction, ownerWidth) +
1059+
minContent += node->style().computeFlexStartPaddingAndBorder(
1060+
requestedAxis, direction, ownerWidth) +
10791061
node->style().computeFlexEndPaddingAndBorder(
1080-
nodeCrossAxis, direction, ownerWidth);
1062+
requestedAxis, direction, ownerWidth);
10811063

1082-
const bool nodeMainIsRow = isRow(nodeMainAxis);
1083-
const float widthMin = nodeMainIsRow ? mainTotal : crossMax;
1084-
const float heightMin = nodeMainIsRow ? crossMax : mainTotal;
1085-
const float minContent = wantRow ? widthMin : heightMin;
10861064
layout.minContent[axisIndex] = minContent;
10871065
layout.minContentOwnerWidth[axisIndex] = ownerWidth;
10881066
layout.setMinContentResolvesAgainstOwnerWidth(
@@ -1201,10 +1179,10 @@ static MinContentSize computeChildMinContentContribution(
12011179
// respond with 0 along their scroll axis.
12021180
// * Empty leaf: return 0.
12031181
// * Container: iterate in-flow children. For each, take its
1204-
// min-content contribution along the container's own main axis (sum
1205-
// into `mainTotal`) and along its cross axis (max into `crossMax`),
1206-
// plus the child's margins. Add the container's own padding and
1207-
// border on both ends of each axis. Project onto `requestedAxis`.
1182+
// min-content contribution along `requestedAxis` plus its margins:
1183+
// the sum of them where that is the container's main axis, the
1184+
// largest where it is its cross axis. Add the container's own
1185+
// padding and border on both ends of the axis.
12081186
//
12091187
// Container-level recursion does no layout writes (no positions, no
12101188
// alignment, no flex distribution); only the descendant leaf measure

0 commit comments

Comments
 (0)