From b0874699043c295fb5b2a854d992e66c3fce4cab Mon Sep 17 00:00:00 2001 From: GitHub Workflows Support Date: Wed, 2 Dec 2026 06:17:12 -0800 Subject: [PATCH] Walk only the requested axis for a container's min-content size (#2102) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- .../yoga/yoga/algorithm/CalculateLayout.cpp | 58 ++++++------------- 1 file changed, 18 insertions(+), 40 deletions(-) diff --git a/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp b/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp index 03032cf0535..8d06148f34b 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp @@ -1026,13 +1026,11 @@ static MinContentSize measureOrWalkMinContentMainSize( } const Direction direction = node->resolveDirection(ownerDirection); - const FlexDirection nodeMainAxis = - resolveDirection(node->style().flexDirection(), direction); - const FlexDirection nodeCrossAxis = - resolveCrossDirection(nodeMainAxis, direction); + // Children add up along the container's main axis; across it the largest + // counts. + const bool alongMainAxis = isRow(node->style().flexDirection()) == wantRow; - float mainTotal = 0.0f; - float crossMax = 0.0f; + float minContent = 0.0f; bool resolvesAgainstOwnerWidth = node->style().hasPercentPadding(); for (size_t i = 0; i < node->getChildCount(); i++) { @@ -1042,47 +1040,27 @@ static MinContentSize measureOrWalkMinContentMainSize( continue; } - const MinContentSize childMain = computeChildMinContentContribution( + const MinContentSize child = computeChildMinContentContribution( node, i, - nodeMainAxis, + requestedAxis, direction, ownerWidth, ownerHeight, generationCount); - const MinContentSize childCross = computeChildMinContentContribution( - node, - i, - nodeCrossAxis, - direction, - ownerWidth, - ownerHeight, - generationCount); - - mainTotal += childMain.size + - childStyle.computeMarginForAxis(nodeMainAxis, ownerWidth); - crossMax = std::max( - crossMax, - childCross.size + - childStyle.computeMarginForAxis(nodeCrossAxis, ownerWidth)); + const float childSize = + child.size + childStyle.computeMarginForAxis(requestedAxis, ownerWidth); + minContent = alongMainAxis ? minContent + childSize + : std::max(minContent, childSize); resolvesAgainstOwnerWidth = resolvesAgainstOwnerWidth || - childMain.resolvesAgainstOwnerWidth || - childCross.resolvesAgainstOwnerWidth || childStyle.hasPercentMargin(); + child.resolvesAgainstOwnerWidth || childStyle.hasPercentMargin(); } - mainTotal += node->style().computeFlexStartPaddingAndBorder( - nodeMainAxis, direction, ownerWidth) + - node->style().computeFlexEndPaddingAndBorder( - nodeMainAxis, direction, ownerWidth); - crossMax += node->style().computeFlexStartPaddingAndBorder( - nodeCrossAxis, direction, ownerWidth) + + minContent += node->style().computeFlexStartPaddingAndBorder( + requestedAxis, direction, ownerWidth) + node->style().computeFlexEndPaddingAndBorder( - nodeCrossAxis, direction, ownerWidth); + requestedAxis, direction, ownerWidth); - const bool nodeMainIsRow = isRow(nodeMainAxis); - const float widthMin = nodeMainIsRow ? mainTotal : crossMax; - const float heightMin = nodeMainIsRow ? crossMax : mainTotal; - const float minContent = wantRow ? widthMin : heightMin; layout.minContent[axisIndex] = minContent; layout.minContentOwnerWidth[axisIndex] = ownerWidth; layout.setMinContentResolvesAgainstOwnerWidth( @@ -1201,10 +1179,10 @@ static MinContentSize computeChildMinContentContribution( // respond with 0 along their scroll axis. // * Empty leaf: return 0. // * Container: iterate in-flow children. For each, take its -// min-content contribution along the container's own main axis (sum -// into `mainTotal`) and along its cross axis (max into `crossMax`), -// plus the child's margins. Add the container's own padding and -// border on both ends of each axis. Project onto `requestedAxis`. +// min-content contribution along `requestedAxis` plus its margins: +// the sum of them where that is the container's main axis, the +// largest where it is its cross axis. Add the container's own +// padding and border on both ends of the axis. // // Container-level recursion does no layout writes (no positions, no // alignment, no flex distribution); only the descendant leaf measure