Skip to content

Commit b160320

Browse files
authored
Only compute automatic minimum sizes for flex items that grow or shrink (#59906)
Summary: X-link: react/yoga#2073 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: #59906
1 parent f45900a commit b160320

14 files changed

Lines changed: 94 additions & 38 deletions

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

Lines changed: 56 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,19 +1103,39 @@ static FloatOptional computeAutoMinMainSize(
11031103
return floor;
11041104
}
11051105

1106-
// boundAxis with an additional lower bound from `child`'s cached
1107-
// `computedAutoMinMainSize`, applied on the main axis only. Used inside
1108-
// the flex-shrink distribution to honor CSS §4.5 auto-min while preserving
1109-
// the existing min/max/padding-and-border clamping.
1106+
// boundAxis with an additional lower bound from `child`'s CSS §4.5 automatic
1107+
// minimum main size, applied on the main axis only. Used inside the flex
1108+
// distribution to honor auto-min while preserving the existing
1109+
// min/max/padding-and-border clamping.
1110+
//
1111+
// The floor is computed on first use and cached on the child until
1112+
// `resolveFlexibleLength` resets it for the next line, so only items that
1113+
// actually grow or shrink run the min-content probe.
11101114
static float boundAxisWithAutoMin(
1111-
const yoga::Node* const child,
1115+
yoga::Node* const child,
11121116
const FlexDirection axis,
11131117
const Direction direction,
11141118
const float value,
11151119
const float axisSize,
1116-
const float widthSize) {
1117-
float bounded = boundAxis(child, axis, direction, value, axisSize, widthSize);
1118-
const FloatOptional autoMin = child->getLayout().computedAutoMinMainSize;
1120+
const float ownerMainAxisSize,
1121+
const float ownerWidth,
1122+
const float ownerHeight,
1123+
const uint32_t generationCount) {
1124+
float bounded =
1125+
boundAxis(child, axis, direction, value, axisSize, ownerWidth);
1126+
LayoutResults& layout = child->getLayout();
1127+
if (!layout.computedAutoMinMainSizeResolved()) {
1128+
layout.computedAutoMinMainSize = computeAutoMinMainSize(
1129+
child,
1130+
axis,
1131+
direction,
1132+
ownerMainAxisSize,
1133+
ownerWidth,
1134+
ownerHeight,
1135+
generationCount);
1136+
layout.setComputedAutoMinMainSizeResolved(true);
1137+
}
1138+
const FloatOptional autoMin = layout.computedAutoMinMainSize;
11191139
if (autoMin.isDefined() && bounded < autoMin.unwrap()) {
11201140
bounded = autoMin.unwrap();
11211141
}
@@ -1197,7 +1217,10 @@ static float distributeFreeSpaceSecondPass(
11971217
direction,
11981218
childSize,
11991219
availableInnerMainDim,
1200-
availableInnerWidth);
1220+
mainAxisOwnerSize,
1221+
availableInnerWidth,
1222+
availableInnerHeight,
1223+
generationCount);
12011224
}
12021225
} else if (
12031226
yoga::isDefined(flexLine.layout.remainingFreeSpace) &&
@@ -1214,7 +1237,10 @@ static float distributeFreeSpaceSecondPass(
12141237
flexLine.layout.remainingFreeSpace /
12151238
flexLine.layout.totalFlexGrowFactors * flexGrowFactor,
12161239
availableInnerMainDim,
1217-
availableInnerWidth);
1240+
mainAxisOwnerSize,
1241+
availableInnerWidth,
1242+
availableInnerHeight,
1243+
generationCount);
12181244
}
12191245
}
12201246

@@ -1344,7 +1370,9 @@ static void distributeFreeSpaceFirstPass(
13441370
const float ownerWidth,
13451371
const float mainAxisOwnerSize,
13461372
const float availableInnerMainDim,
1347-
const float availableInnerWidth) {
1373+
const float availableInnerWidth,
1374+
const float availableInnerHeight,
1375+
const uint32_t generationCount) {
13481376
float flexShrinkScaledFactor = 0;
13491377
float flexGrowFactor = 0;
13501378
float baseMainSize = 0;
@@ -1398,7 +1426,10 @@ static void distributeFreeSpaceFirstPass(
13981426
direction,
13991427
baseMainSize,
14001428
availableInnerMainDim,
1401-
availableInnerWidth);
1429+
mainAxisOwnerSize,
1430+
availableInnerWidth,
1431+
availableInnerHeight,
1432+
generationCount);
14021433
if (yoga::isDefined(baseMainSize) && yoga::isDefined(boundMainSize) &&
14031434
baseMainSize != boundMainSize) {
14041435
// By excluding this item's size and flex factor from remaining, this
@@ -1488,27 +1519,16 @@ static void resolveFlexibleLength(
14881519
const uint32_t generationCount) {
14891520
const float originalFreeSpace = flexLine.layout.remainingFreeSpace;
14901521

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

15141534
// First pass: detect the flex items whose min/max constraints trigger
@@ -1520,7 +1540,9 @@ static void resolveFlexibleLength(
15201540
ownerWidth,
15211541
mainAxisOwnerSize,
15221542
availableInnerMainDim,
1523-
availableInnerWidth);
1543+
availableInnerWidth,
1544+
availableInnerHeight,
1545+
generationCount);
15241546

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

‎packages/react-native/ReactCommon/yoga/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}};

‎scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7208,6 +7208,7 @@ struct facebook::yoga::LayoutData {
72087208
}
72097209

72107210
struct facebook::yoga::LayoutResults {
7211+
public bool computedAutoMinMainSizeResolved() const;
72117212
public bool hadOverflow() const;
72127213
public bool operator==(facebook::yoga::LayoutResults layout) const;
72137214
public facebook::yoga::CachedMeasurement cachedLayout;
@@ -7232,6 +7233,7 @@ struct facebook::yoga::LayoutResults {
72327233
public uint32_t generationCount;
72337234
public uint32_t nextCachedMeasurementsIndex;
72347235
public void setBorder(facebook::yoga::PhysicalEdge physicalEdge, float dimension);
7236+
public void setComputedAutoMinMainSizeResolved(bool resolved);
72357237
public void setDimension(facebook::yoga::Dimension axis, float dimension);
72367238
public void setDirection(facebook::yoga::Direction direction);
72377239
public void setHadOverflow(bool hadOverflow);

‎scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11667,6 +11667,7 @@ struct facebook::yoga::LayoutData {
1166711667
}
1166811668

1166911669
struct facebook::yoga::LayoutResults {
11670+
public bool computedAutoMinMainSizeResolved() const;
1167011671
public bool hadOverflow() const;
1167111672
public bool operator==(facebook::yoga::LayoutResults layout) const;
1167211673
public facebook::yoga::CachedMeasurement cachedLayout;
@@ -11691,6 +11692,7 @@ struct facebook::yoga::LayoutResults {
1169111692
public uint32_t generationCount;
1169211693
public uint32_t nextCachedMeasurementsIndex;
1169311694
public void setBorder(facebook::yoga::PhysicalEdge physicalEdge, float dimension);
11695+
public void setComputedAutoMinMainSizeResolved(bool resolved);
1169411696
public void setDimension(facebook::yoga::Dimension axis, float dimension);
1169511697
public void setDirection(facebook::yoga::Direction direction);
1169611698
public void setHadOverflow(bool hadOverflow);

‎scripts/cxx-api/api-snapshots/ReactAndroidNewarchCxx.api‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7057,6 +7057,7 @@ struct facebook::yoga::LayoutData {
70577057
}
70587058

70597059
struct facebook::yoga::LayoutResults {
7060+
public bool computedAutoMinMainSizeResolved() const;
70607061
public bool hadOverflow() const;
70617062
public bool operator==(facebook::yoga::LayoutResults layout) const;
70627063
public facebook::yoga::CachedMeasurement cachedLayout;
@@ -7081,6 +7082,7 @@ struct facebook::yoga::LayoutResults {
70817082
public uint32_t generationCount;
70827083
public uint32_t nextCachedMeasurementsIndex;
70837084
public void setBorder(facebook::yoga::PhysicalEdge physicalEdge, float dimension);
7085+
public void setComputedAutoMinMainSizeResolved(bool resolved);
70847086
public void setDimension(facebook::yoga::Dimension axis, float dimension);
70857087
public void setDirection(facebook::yoga::Direction direction);
70867088
public void setHadOverflow(bool hadOverflow);

‎scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7057,6 +7057,7 @@ struct facebook::yoga::LayoutData {
70577057
}
70587058

70597059
struct facebook::yoga::LayoutResults {
7060+
public bool computedAutoMinMainSizeResolved() const;
70607061
public bool hadOverflow() const;
70617062
public bool operator==(facebook::yoga::LayoutResults layout) const;
70627063
public facebook::yoga::CachedMeasurement cachedLayout;
@@ -7081,6 +7082,7 @@ struct facebook::yoga::LayoutResults {
70817082
public uint32_t generationCount;
70827083
public uint32_t nextCachedMeasurementsIndex;
70837084
public void setBorder(facebook::yoga::PhysicalEdge physicalEdge, float dimension);
7085+
public void setComputedAutoMinMainSizeResolved(bool resolved);
70847086
public void setDimension(facebook::yoga::Dimension axis, float dimension);
70857087
public void setDirection(facebook::yoga::Direction direction);
70867088
public void setHadOverflow(bool hadOverflow);

‎scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8914,6 +8914,7 @@ struct facebook::yoga::LayoutData {
89148914
}
89158915

89168916
struct facebook::yoga::LayoutResults {
8917+
public bool computedAutoMinMainSizeResolved() const;
89178918
public bool hadOverflow() const;
89188919
public bool operator==(facebook::yoga::LayoutResults layout) const;
89198920
public facebook::yoga::CachedMeasurement cachedLayout;
@@ -8938,6 +8939,7 @@ struct facebook::yoga::LayoutResults {
89388939
public uint32_t generationCount;
89398940
public uint32_t nextCachedMeasurementsIndex;
89408941
public void setBorder(facebook::yoga::PhysicalEdge physicalEdge, float dimension);
8942+
public void setComputedAutoMinMainSizeResolved(bool resolved);
89418943
public void setDimension(facebook::yoga::Dimension axis, float dimension);
89428944
public void setDirection(facebook::yoga::Direction direction);
89438945
public void setHadOverflow(bool hadOverflow);

‎scripts/cxx-api/api-snapshots/ReactAppleFrameworksCxx.api‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13399,6 +13399,7 @@ struct facebook::yoga::LayoutData {
1339913399
}
1340013400

1340113401
struct facebook::yoga::LayoutResults {
13402+
public bool computedAutoMinMainSizeResolved() const;
1340213403
public bool hadOverflow() const;
1340313404
public bool operator==(facebook::yoga::LayoutResults layout) const;
1340413405
public facebook::yoga::CachedMeasurement cachedLayout;
@@ -13423,6 +13424,7 @@ struct facebook::yoga::LayoutResults {
1342313424
public uint32_t generationCount;
1342413425
public uint32_t nextCachedMeasurementsIndex;
1342513426
public void setBorder(facebook::yoga::PhysicalEdge physicalEdge, float dimension);
13427+
public void setComputedAutoMinMainSizeResolved(bool resolved);
1342613428
public void setDimension(facebook::yoga::Dimension axis, float dimension);
1342713429
public void setDirection(facebook::yoga::Direction direction);
1342813430
public void setHadOverflow(bool hadOverflow);

‎scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8766,6 +8766,7 @@ struct facebook::yoga::LayoutData {
87668766
}
87678767

87688768
struct facebook::yoga::LayoutResults {
8769+
public bool computedAutoMinMainSizeResolved() const;
87698770
public bool hadOverflow() const;
87708771
public bool operator==(facebook::yoga::LayoutResults layout) const;
87718772
public facebook::yoga::CachedMeasurement cachedLayout;
@@ -8790,6 +8791,7 @@ struct facebook::yoga::LayoutResults {
87908791
public uint32_t generationCount;
87918792
public uint32_t nextCachedMeasurementsIndex;
87928793
public void setBorder(facebook::yoga::PhysicalEdge physicalEdge, float dimension);
8794+
public void setComputedAutoMinMainSizeResolved(bool resolved);
87938795
public void setDimension(facebook::yoga::Dimension axis, float dimension);
87948796
public void setDirection(facebook::yoga::Direction direction);
87958797
public void setHadOverflow(bool hadOverflow);

‎scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8773,6 +8773,7 @@ struct facebook::yoga::LayoutData {
87738773
}
87748774

87758775
struct facebook::yoga::LayoutResults {
8776+
public bool computedAutoMinMainSizeResolved() const;
87768777
public bool hadOverflow() const;
87778778
public bool operator==(facebook::yoga::LayoutResults layout) const;
87788779
public facebook::yoga::CachedMeasurement cachedLayout;
@@ -8797,6 +8798,7 @@ struct facebook::yoga::LayoutResults {
87978798
public uint32_t generationCount;
87988799
public uint32_t nextCachedMeasurementsIndex;
87998800
public void setBorder(facebook::yoga::PhysicalEdge physicalEdge, float dimension);
8801+
public void setComputedAutoMinMainSizeResolved(bool resolved);
88008802
public void setDimension(facebook::yoga::Dimension axis, float dimension);
88018803
public void setDirection(facebook::yoga::Direction direction);
88028804
public void setHadOverflow(bool hadOverflow);

0 commit comments

Comments
 (0)