Skip to content

Commit cc937d5

Browse files
authored
Only compute automatic minimum sizes for flex items that grow or shrink (#2073)
Summary: X-link: react/react-native#59906 X-link: meta/litho#1095 With CSS Flexbox §4.5 automatic minimum sizing enabled, `resolveFlexibleLength` computed every in-flow item's automatic minimum main size up front, for every flex line on every layout pass. The value is only read when an item is bounded while growing or shrinking (`boundAxisWithAutoMin`). So a line with no free space to distribute, or an item that does not flex, paid for a min-content probe whose result was thrown away. For text, each probe scans and measures every word. The floor is now computed the first time an item is bounded on a line and cached on the item until the next line resets it. Layout results are unchanged; items that never grow or shrink no longer run the probe. The resolved flag lives in `LayoutResults`'s existing bitfield, so node size does not grow. Changelog: [General][Changed] - Skip the automatic minimum size probe for flex items that neither grow nor shrink Reviewed By: fbcbl Differential Revision: D130643092 Pull Request resolved: #2073
1 parent 41118bd commit cc937d5

3 files changed

Lines changed: 151 additions & 38 deletions

File tree

‎tests/YGAutoMinSizeTest.cpp‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,87 @@ TEST(YogaAutoMinSize, min_content_reprobed_on_next_layout) {
658658
EXPECT_FLOAT_EQ(40.0f, YGNodeLayoutGetWidth(row.text));
659659
}
660660

661+
// The floor only matters for an item that grows or shrinks, so a line with
662+
// spare room and no growing items never probes its children.
663+
TEST(YogaAutoMinSize, line_with_free_space_skips_min_content_probe) {
664+
YGConfigRef config = makeWebConfig(/*useAutoMinSize=*/true);
665+
YGNodeRef root = YGNodeNewWithConfig(config);
666+
YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow);
667+
YGNodeStyleSetWidth(root, 200);
668+
YGNodeStyleSetHeight(root, 50);
669+
670+
YGNodeRef text = YGNodeNewWithConfig(config);
671+
YGNodeSetMeasureFunc(text, measureWordWrappingText);
672+
YGNodeSetMinContentMeasureFunc(text, measureMinContentWord);
673+
YGNodeStyleSetFlexGrow(text, 0);
674+
YGNodeInsertChild(root, text, 0);
675+
676+
gMinContentCalls = 0;
677+
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
678+
679+
EXPECT_EQ(gMinContentCalls, 0);
680+
EXPECT_FLOAT_EQ(kNaturalWidth, YGNodeLayoutGetWidth(text));
681+
682+
YGNodeFreeRecursive(root);
683+
YGConfigFree(config);
684+
}
685+
686+
// In an overflowing line, only the items that shrink are floored, so an item
687+
// with `flex-shrink: 0` is never probed.
688+
TEST(YogaAutoMinSize, non_shrinking_item_skips_min_content_probe) {
689+
YGConfigRef config = makeWebConfig(/*useAutoMinSize=*/true);
690+
YGNodeRef root = YGNodeNewWithConfig(config);
691+
YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow);
692+
YGNodeStyleSetWidth(root, 100);
693+
YGNodeStyleSetHeight(root, 50);
694+
695+
YGNodeRef text = YGNodeNewWithConfig(config);
696+
YGNodeSetMeasureFunc(text, measureWordWrappingText);
697+
YGNodeSetMinContentMeasureFunc(text, measureMinContentWord);
698+
YGNodeStyleSetFlexBasis(text, kNaturalWidth);
699+
YGNodeStyleSetFlexShrink(text, 0);
700+
YGNodeInsertChild(root, text, 0);
701+
702+
YGNodeRef spacer = YGNodeNewWithConfig(config);
703+
YGNodeStyleSetWidth(spacer, 50);
704+
YGNodeStyleSetFlexShrink(spacer, 1);
705+
YGNodeInsertChild(root, spacer, 1);
706+
707+
gMinContentCalls = 0;
708+
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
709+
710+
EXPECT_EQ(gMinContentCalls, 0);
711+
EXPECT_FLOAT_EQ(kNaturalWidth, YGNodeLayoutGetWidth(text));
712+
EXPECT_FLOAT_EQ(10.0f, YGNodeLayoutGetWidth(spacer));
713+
714+
YGNodeFreeRecursive(root);
715+
YGConfigFree(config);
716+
}
717+
718+
// A column with no definite height has no free space to distribute, so its
719+
// children are never probed.
720+
TEST(YogaAutoMinSize, column_without_definite_height_skips_min_content_probe) {
721+
YGConfigRef config = makeWebConfig(/*useAutoMinSize=*/true);
722+
YGNodeRef root = YGNodeNewWithConfig(config);
723+
YGNodeStyleSetFlexDirection(root, YGFlexDirectionColumn);
724+
YGNodeStyleSetWidth(root, 200);
725+
726+
YGNodeRef text = YGNodeNewWithConfig(config);
727+
YGNodeSetMeasureFunc(text, measureWordWrappingText);
728+
YGNodeSetMinContentMeasureFunc(text, measureMinContentWord);
729+
YGNodeStyleSetFlexShrink(text, 1);
730+
YGNodeInsertChild(root, text, 0);
731+
732+
gMinContentCalls = 0;
733+
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
734+
735+
EXPECT_EQ(gMinContentCalls, 0);
736+
EXPECT_FLOAT_EQ(kLineHeight, YGNodeLayoutGetHeight(text));
737+
738+
YGNodeFreeRecursive(root);
739+
YGConfigFree(config);
740+
}
741+
661742
// Static min-content getter / setter round-trip smoke test.
662743
TEST(YogaAutoMinSize, static_min_content_getter_setter_round_trip) {
663744
YGNodeRef node = YGNodeNew();

‎yoga/algorithm/CalculateLayout.cpp‎

Lines changed: 56 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,19 +1099,39 @@ static FloatOptional computeAutoMinMainSize(
10991099
return floor;
11001100
}
11011101

1102-
// boundAxis with an additional lower bound from `child`'s cached
1103-
// `computedAutoMinMainSize`, applied on the main axis only. Used inside
1104-
// the flex-shrink distribution to honor CSS §4.5 auto-min while preserving
1105-
// the existing min/max/padding-and-border clamping.
1102+
// boundAxis with an additional lower bound from `child`'s CSS §4.5 automatic
1103+
// minimum main size, applied on the main axis only. Used inside the flex
1104+
// distribution to honor auto-min while preserving the existing
1105+
// min/max/padding-and-border clamping.
1106+
//
1107+
// The floor is computed on first use and cached on the child until
1108+
// `resolveFlexibleLength` resets it for the next line, so only items that
1109+
// actually grow or shrink run the min-content probe.
11061110
static float boundAxisWithAutoMin(
1107-
const yoga::Node* const child,
1111+
yoga::Node* const child,
11081112
const FlexDirection axis,
11091113
const Direction direction,
11101114
const float value,
11111115
const float axisSize,
1112-
const float widthSize) {
1113-
float bounded = boundAxis(child, axis, direction, value, axisSize, widthSize);
1114-
const FloatOptional autoMin = child->getLayout().computedAutoMinMainSize;
1116+
const float ownerMainAxisSize,
1117+
const float ownerWidth,
1118+
const float ownerHeight,
1119+
const uint32_t generationCount) {
1120+
float bounded =
1121+
boundAxis(child, axis, direction, value, axisSize, ownerWidth);
1122+
LayoutResults& layout = child->getLayout();
1123+
if (!layout.computedAutoMinMainSizeResolved()) {
1124+
layout.computedAutoMinMainSize = computeAutoMinMainSize(
1125+
child,
1126+
axis,
1127+
direction,
1128+
ownerMainAxisSize,
1129+
ownerWidth,
1130+
ownerHeight,
1131+
generationCount);
1132+
layout.setComputedAutoMinMainSizeResolved(true);
1133+
}
1134+
const FloatOptional autoMin = layout.computedAutoMinMainSize;
11151135
if (autoMin.isDefined() && bounded < autoMin.unwrap()) {
11161136
bounded = autoMin.unwrap();
11171137
}
@@ -1193,7 +1213,10 @@ static float distributeFreeSpaceSecondPass(
11931213
direction,
11941214
childSize,
11951215
availableInnerMainDim,
1196-
availableInnerWidth);
1216+
mainAxisOwnerSize,
1217+
availableInnerWidth,
1218+
availableInnerHeight,
1219+
generationCount);
11971220
}
11981221
} else if (
11991222
yoga::isDefined(flexLine.layout.remainingFreeSpace) &&
@@ -1210,7 +1233,10 @@ static float distributeFreeSpaceSecondPass(
12101233
flexLine.layout.remainingFreeSpace /
12111234
flexLine.layout.totalFlexGrowFactors * flexGrowFactor,
12121235
availableInnerMainDim,
1213-
availableInnerWidth);
1236+
mainAxisOwnerSize,
1237+
availableInnerWidth,
1238+
availableInnerHeight,
1239+
generationCount);
12141240
}
12151241
}
12161242

@@ -1340,7 +1366,9 @@ static void distributeFreeSpaceFirstPass(
13401366
const float ownerWidth,
13411367
const float mainAxisOwnerSize,
13421368
const float availableInnerMainDim,
1343-
const float availableInnerWidth) {
1369+
const float availableInnerWidth,
1370+
const float availableInnerHeight,
1371+
const uint32_t generationCount) {
13441372
float flexShrinkScaledFactor = 0;
13451373
float flexGrowFactor = 0;
13461374
float baseMainSize = 0;
@@ -1394,7 +1422,10 @@ static void distributeFreeSpaceFirstPass(
13941422
direction,
13951423
baseMainSize,
13961424
availableInnerMainDim,
1397-
availableInnerWidth);
1425+
mainAxisOwnerSize,
1426+
availableInnerWidth,
1427+
availableInnerHeight,
1428+
generationCount);
13981429
if (yoga::isDefined(baseMainSize) && yoga::isDefined(boundMainSize) &&
13991430
baseMainSize != boundMainSize) {
14001431
// By excluding this item's size and flex factor from remaining, this
@@ -1484,27 +1515,16 @@ static void resolveFlexibleLength(
14841515
const uint32_t generationCount) {
14851516
const float originalFreeSpace = flexLine.layout.remainingFreeSpace;
14861517

1487-
// CSS Flexbox §4.5: compute each item's automatic minimum main-axis size
1488-
// up front so the bounding helpers below can floor shrunk values.
1489-
// computeAutoMinMainSize returns Undefined when the feature is off or an
1490-
// explicit `min-{w,h}` already pins the floor, in which case the cached
1491-
// value is also Undefined and `boundAxisWithAutoMin` reduces to `boundAxis`.
1492-
if (!node->hasErrata(Errata::MinSizeUndefinedInsteadOfAuto)) {
1493-
for (auto currentLineChild : flexLine.itemsInFlow) {
1494-
currentLineChild->getLayout().computedAutoMinMainSize =
1495-
computeAutoMinMainSize(
1496-
currentLineChild,
1497-
mainAxis,
1498-
direction,
1499-
mainAxisOwnerSize,
1500-
availableInnerWidth,
1501-
availableInnerHeight,
1502-
generationCount);
1503-
}
1504-
} else {
1505-
for (auto currentLineChild : flexLine.itemsInFlow) {
1506-
currentLineChild->getLayout().computedAutoMinMainSize = FloatOptional{};
1507-
}
1518+
// CSS Flexbox §4.5: `boundAxisWithAutoMin` computes each item's automatic
1519+
// minimum main size the first time the item flexes on this line. With the
1520+
// feature off, the floor starts out resolved as Undefined and the helper
1521+
// reduces to `boundAxis`.
1522+
const bool autoMinResolved =
1523+
node->hasErrata(Errata::MinSizeUndefinedInsteadOfAuto);
1524+
for (auto currentLineChild : flexLine.itemsInFlow) {
1525+
LayoutResults& childLayout = currentLineChild->getLayout();
1526+
childLayout.computedAutoMinMainSize = FloatOptional{};
1527+
childLayout.setComputedAutoMinMainSizeResolved(autoMinResolved);
15081528
}
15091529

15101530
// First pass: detect the flex items whose min/max constraints trigger
@@ -1516,7 +1536,9 @@ static void resolveFlexibleLength(
15161536
ownerWidth,
15171537
mainAxisOwnerSize,
15181538
availableInnerMainDim,
1519-
availableInnerWidth);
1539+
availableInnerWidth,
1540+
availableInnerHeight,
1541+
generationCount);
15201542

15211543
// Second pass: resolve the sizes of the flexible items
15221544
const float distributedFreeSpace = distributeFreeSpaceSecondPass(

‎yoga/node/LayoutResults.h‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ struct LayoutResults {
2828
FloatOptional computedFlexBasis = {};
2929

3030
// Per-flex-item floor along the main axis derived from CSS Flexbox §4.5
31-
// automatic minimum sizing. Set by `resolveFlexibleLength` when the parent's
32-
// config does NOT carry the `MinSizeUndefinedInsteadOfAuto` errata and the
33-
// item has no explicit main-axis `min-{width,height}`. Read by the
34-
// shrink/bound machinery to keep items at least this large. `Undefined`
31+
// automatic minimum sizing, applied when the parent's config does NOT carry
32+
// the `MinSizeUndefinedInsteadOfAuto` errata and the item has no explicit
33+
// main-axis `min-{width,height}`. Computed on first use by the shrink/grow
34+
// bound machinery and valid while `computedAutoMinMainSizeResolved()` is
35+
// set; `resolveFlexibleLength` resets both for each flex line. `Undefined`
3536
// means "no auto-min applies."
3637
FloatOptional computedAutoMinMainSize = {};
3738

@@ -71,6 +72,14 @@ struct LayoutResults {
7172
hadOverflow_ = hadOverflow;
7273
}
7374

75+
bool computedAutoMinMainSizeResolved() const {
76+
return computedAutoMinMainSizeResolved_;
77+
}
78+
79+
void setComputedAutoMinMainSizeResolved(bool resolved) {
80+
computedAutoMinMainSizeResolved_ = resolved;
81+
}
82+
7483
float dimension(Dimension axis) const {
7584
return dimensions_[yoga::to_underlying(axis)];
7685
}
@@ -132,6 +141,7 @@ struct LayoutResults {
132141
private:
133142
Direction direction_ : bitCount<Direction>() = Direction::Inherit;
134143
bool hadOverflow_ : 1 = false;
144+
bool computedAutoMinMainSizeResolved_ : 1 = false;
135145

136146
std::array<float, 2> dimensions_ = {{YGUndefined, YGUndefined}};
137147
std::array<float, 2> measuredDimensions_ = {{YGUndefined, YGUndefined}};

0 commit comments

Comments
 (0)