From f4c2cbde0ecbed4815882cda12573619520ca0b5 Mon Sep 17 00:00:00 2001 From: Josh Watzman Date: Mon, 19 Oct 2026 05:59:13 -0700 Subject: [PATCH] Don't break a flex line on floating-point rounding noise Summary: A content-sized wrapping row sizes itself to the max-content sum of its items, then recovers the space available to those items by subtracting its padding and border back off (calculateAvailableInnerDimension). In float32 that add-then-subtract round-trip can land a fraction of an ulp below the sum it came from. For an item with a measure function the measurement cache still returns the max-content measurement in that situation: oldSizeIsMaxContentAndStillFits accepts a cached width up to the inexactEquals epsilon (1e-4) wider than the space now available. calculateFlexLine then compared that basis against availableInnerMainDim with a bare `>`, so the basis the cache had just accepted as fitting read as overflow and the line broke. Because the container had already been sized for a single line, the result was a box one line tall with its items laid out on two, overflowing its own padding and border. In React Native this reproduced on both iOS and Android and was extremely sensitive to the measured text width, since only some values leave a residue inside the window. Make the overflow test tolerant of the same epsilon the cache uses, so the two agree. Items without a measure function are unaffected: they take their exact style width in both passes and never enter that cache path. The regression test sweeps fractional measured widths whose content sum sits just under 128 while the outer width sits just over it, so adding padding and border crosses a float32 binade and the subtraction cannot always recover the original value. Changelog: [Internal] X-link: https://github.com/react/yoga/pull/2022 Differential Revision: D126040642 Pulled By: javache --- .../yoga/yoga/algorithm/FlexLine.cpp | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/react-native/ReactCommon/yoga/yoga/algorithm/FlexLine.cpp b/packages/react-native/ReactCommon/yoga/yoga/algorithm/FlexLine.cpp index dc0a300add24..ab92b8efc370 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/algorithm/FlexLine.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/algorithm/FlexLine.cpp @@ -10,6 +10,7 @@ #include #include #include +#include namespace facebook::yoga { @@ -74,12 +75,26 @@ FlexLine calculateFlexLine( ownerWidth) .unwrap(); + const float requiredMainDim = sizeConsumedIncludingMinConstraint + + flexBasisWithMinAndMaxConstraints + childMarginMainAxis + + childLeadingGapMainAxis; + // If this is a multi-line flow and this item pushes us over the available // size, we've hit the end of the current line. Break out of the loop and // lay out the current line. - if (sizeConsumedIncludingMinConstraint + flexBasisWithMinAndMaxConstraints + - childMarginMainAxis + childLeadingGapMainAxis > - availableInnerMainDim && + // + // The overflow test is tolerant of the same epsilon the measurement cache + // uses when it decides a cached measurement "still fits" (see + // oldSizeIsMaxContentAndStillFits in Cache.cpp). A content-sized wrap + // container adds padding and border to the max-content sum to size itself + // and then subtracts them again to recover availableInnerMainDim, and in + // float32 that round-trip can land a few ulps low. The cache accepts a + // basis up to the epsilon wider than the space now available and hands + // back the max-content measurement unchanged; without the same tolerance + // here, that basis reads as overflow and the line breaks, leaving the + // container sized for one line with its items laid out on two. + if (requiredMainDim > availableInnerMainDim && + !yoga::inexactEquals(requiredMainDim, availableInnerMainDim) && isNodeFlexWrap && !itemsInFlow.empty()) { break; }