mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Memoise §4.5 min-content per node and axis per layout (#58833)
Summary: X-link: https://github.com/facebook/litho/pull/1089 X-link: https://github.com/react/yoga/pull/2036 Pull Request resolved: https://github.com/react/react-native/pull/58833 `computeMinContentMainSize` asks every in-flow child of a container for its min-content along both axes, and every flex container's `resolveFlexibleLength` recomputes the automatic minimum size of each item. Nothing was cached, so a leaf with a measure function N containers below a flex item was probed O(2^N) times per call, then again for every ancestor and every layout pass. A text leaf ten containers deep was probed 7,161 times in a single layout. This memoises min-content per node and axis for the current layout generation, following the existing `computedFlexBasisGeneration` pattern: - A leaf memoises its measured min-content, before its own padding and border. That value doesn't depend on the owner, so the measure callback runs at most once per axis per layout. - A container memoises its full min-content along with the `ownerWidth` it was computed against. Percentage padding and margins resolve against that width, and each ancestor probes with its own inner width, so a result is only reused for the same width. Layout results are unchanged; only the number of measure callbacks drops. The canonical Yoga copy and the React Native vendored copy are both updated, and the C++ API snapshots gain the three new `LayoutResults` fields. Changelog: [General][Fixed] - Avoid exponential min-content measurement during automatic minimum sizing of nested flex containers Reviewed By: javache Differential Revision: D123012133 fbshipit-source-id: 58eb649b260f5707cb36d4cd373e4f44788c16a2
This commit is contained in:
1 parent
c4356db573
commit
6e2edef96e
14 files changed
+103
-26
No files matched your search
@@ -850,15 +850,19 @@ static float computeFlexBasisForChildren(
|
||||
// Container-level recursion does no layout writes (no positions, no
|
||||
// alignment, no flex distribution); only the descendant leaf measure
|
||||
// callbacks observe state changes (the same ones a normal layout pass
|
||||
// would invoke). Roughly equivalent to FlexLayout's dedicated
|
||||
// `computeMinContentSize` cost: one measure call per leaf + linear walk
|
||||
// per container.
|
||||
// would invoke).
|
||||
//
|
||||
// Results are memoized per node and axis for the current layout generation
|
||||
// (see `LayoutResults::minContent`): each leaf's measure callback runs at
|
||||
// most once per axis per layout, and each container is walked once per
|
||||
// distinct `ownerWidth`.
|
||||
static float computeMinContentMainSize(
|
||||
yoga::Node* const node,
|
||||
const FlexDirection requestedAxis,
|
||||
const Direction ownerDirection,
|
||||
const float ownerWidth,
|
||||
const float ownerHeight) {
|
||||
const float ownerHeight,
|
||||
const uint32_t generationCount) {
|
||||
const bool wantRow = isRow(requestedAxis);
|
||||
|
||||
// 1. Static value wins for any node (leaf or container). Short-circuits
|
||||
@@ -872,22 +876,29 @@ static float computeMinContentMainSize(
|
||||
return staticMin.unwrap();
|
||||
}
|
||||
|
||||
LayoutResults& layout = node->getLayout();
|
||||
const auto axisIndex = yoga::to_underlying(dimension(requestedAxis));
|
||||
|
||||
if (node->hasMeasureFunc()) {
|
||||
// 2. Dynamic min-content callback if set (for Primitives whose
|
||||
// min-content depends on state). Otherwise fall back to the regular
|
||||
// measure function with `AtMost 0`, which text measurers naturally
|
||||
// answer with longest-word width.
|
||||
const YGSize size = node->hasMinContentMeasureFunc()
|
||||
? node->measureMinContent(
|
||||
wantRow ? 0.0f : YGUndefined,
|
||||
wantRow ? MeasureMode::AtMost : MeasureMode::Undefined,
|
||||
wantRow ? YGUndefined : 0.0f,
|
||||
wantRow ? MeasureMode::Undefined : MeasureMode::AtMost)
|
||||
: node->measure(
|
||||
wantRow ? 0.0f : YGUndefined,
|
||||
wantRow ? MeasureMode::AtMost : MeasureMode::Undefined,
|
||||
wantRow ? YGUndefined : 0.0f,
|
||||
wantRow ? MeasureMode::Undefined : MeasureMode::AtMost);
|
||||
if (layout.minContentGeneration[axisIndex] != generationCount) {
|
||||
// 2. Dynamic min-content callback if set (for Primitives whose
|
||||
// min-content depends on state). Otherwise fall back to the regular
|
||||
// measure function with `AtMost 0`, which text measurers naturally
|
||||
// answer with longest-word width.
|
||||
const YGSize size = node->hasMinContentMeasureFunc()
|
||||
? node->measureMinContent(
|
||||
wantRow ? 0.0f : YGUndefined,
|
||||
wantRow ? MeasureMode::AtMost : MeasureMode::Undefined,
|
||||
wantRow ? YGUndefined : 0.0f,
|
||||
wantRow ? MeasureMode::Undefined : MeasureMode::AtMost)
|
||||
: node->measure(
|
||||
wantRow ? 0.0f : YGUndefined,
|
||||
wantRow ? MeasureMode::AtMost : MeasureMode::Undefined,
|
||||
wantRow ? YGUndefined : 0.0f,
|
||||
wantRow ? MeasureMode::Undefined : MeasureMode::AtMost);
|
||||
layout.minContent[axisIndex] = wantRow ? size.width : size.height;
|
||||
layout.minContentGeneration[axisIndex] = generationCount;
|
||||
}
|
||||
// Add the leaf's own padding and border, like the container branch below.
|
||||
const Direction leafDirection = node->resolveDirection(ownerDirection);
|
||||
const float paddingAndBorder =
|
||||
@@ -895,13 +906,18 @@ static float computeMinContentMainSize(
|
||||
requestedAxis, leafDirection, ownerWidth) +
|
||||
node->style().computeFlexEndPaddingAndBorder(
|
||||
requestedAxis, leafDirection, ownerWidth);
|
||||
return (wantRow ? size.width : size.height) + paddingAndBorder;
|
||||
return layout.minContent[axisIndex] + paddingAndBorder;
|
||||
}
|
||||
|
||||
if (node->getChildCount() == 0) {
|
||||
return 0.0f;
|
||||
}
|
||||
|
||||
if (layout.minContentGeneration[axisIndex] == generationCount &&
|
||||
yoga::inexactEquals(layout.minContentOwnerWidth[axisIndex], ownerWidth)) {
|
||||
return layout.minContent[axisIndex];
|
||||
}
|
||||
|
||||
const Direction direction = node->resolveDirection(ownerDirection);
|
||||
const FlexDirection nodeMainAxis =
|
||||
resolveDirection(node->style().flexDirection(), direction);
|
||||
@@ -919,11 +935,21 @@ static float computeMinContentMainSize(
|
||||
}
|
||||
|
||||
float childMain = computeMinContentMainSize(
|
||||
child, nodeMainAxis, direction, ownerWidth, ownerHeight);
|
||||
child,
|
||||
nodeMainAxis,
|
||||
direction,
|
||||
ownerWidth,
|
||||
ownerHeight,
|
||||
generationCount);
|
||||
childMain += child->style().computeMarginForAxis(nodeMainAxis, ownerWidth);
|
||||
|
||||
float childCross = computeMinContentMainSize(
|
||||
child, nodeCrossAxis, direction, ownerWidth, ownerHeight);
|
||||
child,
|
||||
nodeCrossAxis,
|
||||
direction,
|
||||
ownerWidth,
|
||||
ownerHeight,
|
||||
generationCount);
|
||||
childCross +=
|
||||
child->style().computeMarginForAxis(nodeCrossAxis, ownerWidth);
|
||||
|
||||
@@ -943,7 +969,11 @@ static float computeMinContentMainSize(
|
||||
const bool nodeMainIsRow = isRow(nodeMainAxis);
|
||||
const float widthMin = nodeMainIsRow ? mainTotal : crossMax;
|
||||
const float heightMin = nodeMainIsRow ? crossMax : mainTotal;
|
||||
return wantRow ? widthMin : heightMin;
|
||||
const float minContent = wantRow ? widthMin : heightMin;
|
||||
layout.minContent[axisIndex] = minContent;
|
||||
layout.minContentOwnerWidth[axisIndex] = ownerWidth;
|
||||
layout.minContentGeneration[axisIndex] = generationCount;
|
||||
return minContent;
|
||||
}
|
||||
|
||||
// Computes the CSS Flexbox §4.5 automatic minimum main-axis size for
|
||||
@@ -962,7 +992,8 @@ static FloatOptional computeAutoMinMainSize(
|
||||
const Direction direction,
|
||||
const float ownerMainAxisSize,
|
||||
const float ownerWidth,
|
||||
const float ownerHeight) {
|
||||
const float ownerHeight,
|
||||
const uint32_t generationCount) {
|
||||
if (child->hasErrata(Errata::MinSizeUndefinedInsteadOfAuto)) {
|
||||
return FloatOptional{};
|
||||
}
|
||||
@@ -1007,7 +1038,7 @@ static FloatOptional computeAutoMinMainSize(
|
||||
|
||||
// Content size suggestion: probe via min-content recursion.
|
||||
const FloatOptional contentMain = FloatOptional{computeMinContentMainSize(
|
||||
child, mainAxis, direction, ownerWidth, ownerHeight)};
|
||||
child, mainAxis, direction, ownerWidth, ownerHeight, generationCount)};
|
||||
|
||||
// Combine per §4.5: floor = min(content, specified) when specified is
|
||||
// definite; otherwise floor = min(content, transferred) when transferred
|
||||
@@ -1432,7 +1463,8 @@ static void resolveFlexibleLength(
|
||||
direction,
|
||||
mainAxisOwnerSize,
|
||||
availableInnerWidth,
|
||||
availableInnerHeight);
|
||||
availableInnerHeight,
|
||||
generationCount);
|
||||
}
|
||||
} else {
|
||||
for (auto currentLineChild : flexLine.itemsInFlow) {
|
||||
|
||||
@@ -35,6 +35,15 @@ struct LayoutResults {
|
||||
// means "no auto-min applies."
|
||||
FloatOptional computedAutoMinMainSize = {};
|
||||
|
||||
// Memoized min-content sizes for §4.5, indexed by dimension and valid only
|
||||
// during layout generation `minContentGeneration`. A leaf stores its measured
|
||||
// min-content without padding and border, which doesn't depend on the owner.
|
||||
// A container stores its full min-content, which depends on
|
||||
// `minContentOwnerWidth` through percentage padding and margins.
|
||||
std::array<uint32_t, 2> minContentGeneration = {};
|
||||
std::array<float, 2> minContentOwnerWidth = {{YGUndefined, YGUndefined}};
|
||||
std::array<float, 2> minContent = {{YGUndefined, YGUndefined}};
|
||||
|
||||
// Instead of recomputing the entire layout every single time, we cache some
|
||||
// information to break early when nothing changed
|
||||
uint32_t generationCount = 0;
|
||||
|
||||
@@ -7197,6 +7197,9 @@ struct facebook::yoga::LayoutResults {
|
||||
public float rawDimension(facebook::yoga::Dimension axis) const;
|
||||
public static constexpr int32_t MaxCachedMeasurements;
|
||||
public std::array<facebook::yoga::CachedMeasurement, facebook::yoga::LayoutResults::MaxCachedMeasurements> cachedMeasurements;
|
||||
public std::array<float, 2> minContent;
|
||||
public std::array<float, 2> minContentOwnerWidth;
|
||||
public std::array<uint32_t, 2> minContentGeneration;
|
||||
public uint32_t computedFlexBasisGeneration;
|
||||
public uint32_t configVersion;
|
||||
public uint32_t generationCount;
|
||||
|
||||
@@ -11627,6 +11627,9 @@ struct facebook::yoga::LayoutResults {
|
||||
public float rawDimension(facebook::yoga::Dimension axis) const;
|
||||
public static constexpr int32_t MaxCachedMeasurements;
|
||||
public std::array<facebook::yoga::CachedMeasurement, facebook::yoga::LayoutResults::MaxCachedMeasurements> cachedMeasurements;
|
||||
public std::array<float, 2> minContent;
|
||||
public std::array<float, 2> minContentOwnerWidth;
|
||||
public std::array<uint32_t, 2> minContentGeneration;
|
||||
public uint32_t computedFlexBasisGeneration;
|
||||
public uint32_t configVersion;
|
||||
public uint32_t generationCount;
|
||||
|
||||
@@ -7046,6 +7046,9 @@ struct facebook::yoga::LayoutResults {
|
||||
public float rawDimension(facebook::yoga::Dimension axis) const;
|
||||
public static constexpr int32_t MaxCachedMeasurements;
|
||||
public std::array<facebook::yoga::CachedMeasurement, facebook::yoga::LayoutResults::MaxCachedMeasurements> cachedMeasurements;
|
||||
public std::array<float, 2> minContent;
|
||||
public std::array<float, 2> minContentOwnerWidth;
|
||||
public std::array<uint32_t, 2> minContentGeneration;
|
||||
public uint32_t computedFlexBasisGeneration;
|
||||
public uint32_t configVersion;
|
||||
public uint32_t generationCount;
|
||||
|
||||
@@ -7046,6 +7046,9 @@ struct facebook::yoga::LayoutResults {
|
||||
public float rawDimension(facebook::yoga::Dimension axis) const;
|
||||
public static constexpr int32_t MaxCachedMeasurements;
|
||||
public std::array<facebook::yoga::CachedMeasurement, facebook::yoga::LayoutResults::MaxCachedMeasurements> cachedMeasurements;
|
||||
public std::array<float, 2> minContent;
|
||||
public std::array<float, 2> minContentOwnerWidth;
|
||||
public std::array<uint32_t, 2> minContentGeneration;
|
||||
public uint32_t computedFlexBasisGeneration;
|
||||
public uint32_t configVersion;
|
||||
public uint32_t generationCount;
|
||||
|
||||
@@ -8981,6 +8981,9 @@ struct facebook::yoga::LayoutResults {
|
||||
public float rawDimension(facebook::yoga::Dimension axis) const;
|
||||
public static constexpr int32_t MaxCachedMeasurements;
|
||||
public std::array<facebook::yoga::CachedMeasurement, facebook::yoga::LayoutResults::MaxCachedMeasurements> cachedMeasurements;
|
||||
public std::array<float, 2> minContent;
|
||||
public std::array<float, 2> minContentOwnerWidth;
|
||||
public std::array<uint32_t, 2> minContentGeneration;
|
||||
public uint32_t computedFlexBasisGeneration;
|
||||
public uint32_t configVersion;
|
||||
public uint32_t generationCount;
|
||||
|
||||
@@ -13446,6 +13446,9 @@ struct facebook::yoga::LayoutResults {
|
||||
public float rawDimension(facebook::yoga::Dimension axis) const;
|
||||
public static constexpr int32_t MaxCachedMeasurements;
|
||||
public std::array<facebook::yoga::CachedMeasurement, facebook::yoga::LayoutResults::MaxCachedMeasurements> cachedMeasurements;
|
||||
public std::array<float, 2> minContent;
|
||||
public std::array<float, 2> minContentOwnerWidth;
|
||||
public std::array<uint32_t, 2> minContentGeneration;
|
||||
public uint32_t computedFlexBasisGeneration;
|
||||
public uint32_t configVersion;
|
||||
public uint32_t generationCount;
|
||||
|
||||
@@ -8832,6 +8832,9 @@ struct facebook::yoga::LayoutResults {
|
||||
public float rawDimension(facebook::yoga::Dimension axis) const;
|
||||
public static constexpr int32_t MaxCachedMeasurements;
|
||||
public std::array<facebook::yoga::CachedMeasurement, facebook::yoga::LayoutResults::MaxCachedMeasurements> cachedMeasurements;
|
||||
public std::array<float, 2> minContent;
|
||||
public std::array<float, 2> minContentOwnerWidth;
|
||||
public std::array<uint32_t, 2> minContentGeneration;
|
||||
public uint32_t computedFlexBasisGeneration;
|
||||
public uint32_t configVersion;
|
||||
public uint32_t generationCount;
|
||||
|
||||
@@ -8840,6 +8840,9 @@ struct facebook::yoga::LayoutResults {
|
||||
public float rawDimension(facebook::yoga::Dimension axis) const;
|
||||
public static constexpr int32_t MaxCachedMeasurements;
|
||||
public std::array<facebook::yoga::CachedMeasurement, facebook::yoga::LayoutResults::MaxCachedMeasurements> cachedMeasurements;
|
||||
public std::array<float, 2> minContent;
|
||||
public std::array<float, 2> minContentOwnerWidth;
|
||||
public std::array<uint32_t, 2> minContentGeneration;
|
||||
public uint32_t computedFlexBasisGeneration;
|
||||
public uint32_t configVersion;
|
||||
public uint32_t generationCount;
|
||||
|
||||
@@ -4441,6 +4441,9 @@ struct facebook::yoga::LayoutResults {
|
||||
public float rawDimension(facebook::yoga::Dimension axis) const;
|
||||
public static constexpr int32_t MaxCachedMeasurements;
|
||||
public std::array<facebook::yoga::CachedMeasurement, facebook::yoga::LayoutResults::MaxCachedMeasurements> cachedMeasurements;
|
||||
public std::array<float, 2> minContent;
|
||||
public std::array<float, 2> minContentOwnerWidth;
|
||||
public std::array<uint32_t, 2> minContentGeneration;
|
||||
public uint32_t computedFlexBasisGeneration;
|
||||
public uint32_t configVersion;
|
||||
public uint32_t generationCount;
|
||||
|
||||
@@ -8771,6 +8771,9 @@ struct facebook::yoga::LayoutResults {
|
||||
public float rawDimension(facebook::yoga::Dimension axis) const;
|
||||
public static constexpr int32_t MaxCachedMeasurements;
|
||||
public std::array<facebook::yoga::CachedMeasurement, facebook::yoga::LayoutResults::MaxCachedMeasurements> cachedMeasurements;
|
||||
public std::array<float, 2> minContent;
|
||||
public std::array<float, 2> minContentOwnerWidth;
|
||||
public std::array<uint32_t, 2> minContentGeneration;
|
||||
public uint32_t computedFlexBasisGeneration;
|
||||
public uint32_t configVersion;
|
||||
public uint32_t generationCount;
|
||||
|
||||
@@ -4434,6 +4434,9 @@ struct facebook::yoga::LayoutResults {
|
||||
public float rawDimension(facebook::yoga::Dimension axis) const;
|
||||
public static constexpr int32_t MaxCachedMeasurements;
|
||||
public std::array<facebook::yoga::CachedMeasurement, facebook::yoga::LayoutResults::MaxCachedMeasurements> cachedMeasurements;
|
||||
public std::array<float, 2> minContent;
|
||||
public std::array<float, 2> minContentOwnerWidth;
|
||||
public std::array<uint32_t, 2> minContentGeneration;
|
||||
public uint32_t computedFlexBasisGeneration;
|
||||
public uint32_t configVersion;
|
||||
public uint32_t generationCount;
|
||||
|
||||
@@ -4434,6 +4434,9 @@ struct facebook::yoga::LayoutResults {
|
||||
public float rawDimension(facebook::yoga::Dimension axis) const;
|
||||
public static constexpr int32_t MaxCachedMeasurements;
|
||||
public std::array<facebook::yoga::CachedMeasurement, facebook::yoga::LayoutResults::MaxCachedMeasurements> cachedMeasurements;
|
||||
public std::array<float, 2> minContent;
|
||||
public std::array<float, 2> minContentOwnerWidth;
|
||||
public std::array<uint32_t, 2> minContentGeneration;
|
||||
public uint32_t computedFlexBasisGeneration;
|
||||
public uint32_t configVersion;
|
||||
public uint32_t generationCount;
|
||||
|
||||
Reference in new issue
Block a user