From ccca5d7a296f7e9eb49641370ed6c58b209478bd Mon Sep 17 00:00:00 2001 From: Pieter De Baets Date: Mon, 19 Oct 2026 13:49:50 -0700 Subject: [PATCH] Key cached layouts on the owner size a node's percentages resolve against (#2042) Summary: * Test that percentage padding and min sizes follow their owner on relayout * Key cached layouts on the owner size a node's percentages resolve against A clean node's cached layout and measurements are keyed on its available size and sizing modes. Its percentage padding, margin, width, min-width and max-width resolve against the owner's width (and its percentage height bounds against the owner's height), which the key does not record, so a node of fixed size keeps the layout its percentages produced under its owner's previous size. Each cache entry now records the owner size it was computed against, and a lookup for a node whose style resolves a percentage against the owner's width or height requires that size to match. Fixes #1626 Changelog: [Internal] bypass-github-export-checks Reviewed By: christophpurrer Differential Revision: D125706276 fbshipit-source-id: 7d4151abdd298be1620d908632e17e207b76deb9 --- .../yoga/yoga/algorithm/CalculateLayout.cpp | 33 ++++++++++++++++--- .../yoga/yoga/node/CachedMeasurement.h | 11 +++++++ .../ReactCommon/yoga/yoga/style/Style.h | 26 +++++++++++++++ .../api-snapshots/ReactAndroidDebugCxx.api | 4 +++ .../ReactAndroidFrameworksCxx.api | 4 +++ .../api-snapshots/ReactAndroidNewarchCxx.api | 4 +++ .../api-snapshots/ReactAndroidReleaseCxx.api | 4 +++ .../api-snapshots/ReactAppleDebugCxx.api | 4 +++ .../api-snapshots/ReactAppleFrameworksCxx.api | 4 +++ .../api-snapshots/ReactAppleNewarchCxx.api | 4 +++ .../api-snapshots/ReactAppleReleaseCxx.api | 4 +++ .../api-snapshots/ReactCommonDebugCxx.api | 4 +++ .../ReactCommonFrameworksCxx.api | 4 +++ .../api-snapshots/ReactCommonNewarchCxx.api | 4 +++ .../api-snapshots/ReactCommonReleaseCxx.api | 4 +++ 15 files changed, 114 insertions(+), 4 deletions(-) diff --git a/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp b/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp index a3a70f895a0..15277dfd464 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp +++ b/packages/react-native/ReactCommon/yoga/yoga/algorithm/CalculateLayout.cpp @@ -2704,6 +2704,19 @@ static void calculateLayoutImpl( // Input parameters are the same as calculateLayoutImpl (see above) // Return parameter is true if layout was performed, false if skipped // +// A cached result holds only for the owner size it was computed against when +// the node's style resolves a percentage against that size. +static bool isCachedForOwnerSize( + const yoga::Node* const node, + const CachedMeasurement& cachedMeasurement, + const float ownerWidth, + const float ownerHeight) { + return (!node->style().resolvesAgainstOwnerWidth() || + yoga::inexactEquals(cachedMeasurement.ownerWidth, ownerWidth)) && + (!node->style().resolvesAgainstOwnerHeight() || + yoga::inexactEquals(cachedMeasurement.ownerHeight, ownerHeight)); +} + bool calculateLayoutInternal( yoga::Node* const node, const float availableWidth, @@ -2736,6 +2749,8 @@ bool calculateLayoutInternal( layout->cachedLayout.heightSizingMode = SizingMode::MaxContent; layout->cachedLayout.computedWidth = -1; layout->cachedLayout.computedHeight = -1; + layout->cachedLayout.ownerWidth = -1; + layout->cachedLayout.ownerHeight = -1; } CachedMeasurement* cachedResults = nullptr; @@ -2768,7 +2783,9 @@ bool calculateLayoutInternal( layout->cachedLayout.computedHeight, marginAxisRow, marginAxisColumn, - node->getConfig())) { + node->getConfig()) && + isCachedForOwnerSize( + node, layout->cachedLayout, ownerWidth, ownerHeight)) { cachedResults = &layout->cachedLayout; } else { // Try to use the measurement cache. @@ -2786,7 +2803,9 @@ bool calculateLayoutInternal( layout->cachedMeasurements[i].computedHeight, marginAxisRow, marginAxisColumn, - node->getConfig())) { + node->getConfig()) && + isCachedForOwnerSize( + node, layout->cachedMeasurements[i], ownerWidth, ownerHeight)) { cachedResults = &layout->cachedMeasurements[i]; break; } @@ -2798,7 +2817,9 @@ bool calculateLayoutInternal( yoga::inexactEquals( layout->cachedLayout.availableHeight, availableHeight) && layout->cachedLayout.widthSizingMode == widthSizingMode && - layout->cachedLayout.heightSizingMode == heightSizingMode) { + layout->cachedLayout.heightSizingMode == heightSizingMode && + isCachedForOwnerSize( + node, layout->cachedLayout, ownerWidth, ownerHeight)) { cachedResults = &layout->cachedLayout; } } else { @@ -2808,7 +2829,9 @@ bool calculateLayoutInternal( yoga::inexactEquals( layout->cachedMeasurements[i].availableHeight, availableHeight) && layout->cachedMeasurements[i].widthSizingMode == widthSizingMode && - layout->cachedMeasurements[i].heightSizingMode == heightSizingMode) { + layout->cachedMeasurements[i].heightSizingMode == heightSizingMode && + isCachedForOwnerSize( + node, layout->cachedMeasurements[i], ownerWidth, ownerHeight)) { cachedResults = &layout->cachedMeasurements[i]; break; } @@ -2871,6 +2894,8 @@ bool calculateLayoutInternal( layout->measuredDimension(Dimension::Width); newCacheEntry->computedHeight = layout->measuredDimension(Dimension::Height); + newCacheEntry->ownerWidth = ownerWidth; + newCacheEntry->ownerHeight = ownerHeight; } } diff --git a/packages/react-native/ReactCommon/yoga/yoga/node/CachedMeasurement.h b/packages/react-native/ReactCommon/yoga/yoga/node/CachedMeasurement.h index 9c76280acf7..4cee330f70e 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/node/CachedMeasurement.h +++ b/packages/react-native/ReactCommon/yoga/yoga/node/CachedMeasurement.h @@ -25,6 +25,9 @@ struct CachedMeasurement { float computedWidth{-1}; float computedHeight{-1}; + float ownerWidth{-1}; + float ownerHeight{-1}; + bool operator==(CachedMeasurement measurement) const { bool isEqual = widthSizingMode == measurement.widthSizingMode && heightSizingMode == measurement.heightSizingMode; @@ -45,6 +48,14 @@ struct CachedMeasurement { !yoga::isUndefined(measurement.computedHeight)) { isEqual = isEqual && computedHeight == measurement.computedHeight; } + if (!yoga::isUndefined(ownerWidth) || + !yoga::isUndefined(measurement.ownerWidth)) { + isEqual = isEqual && ownerWidth == measurement.ownerWidth; + } + if (!yoga::isUndefined(ownerHeight) || + !yoga::isUndefined(measurement.ownerHeight)) { + isEqual = isEqual && ownerHeight == measurement.ownerHeight; + } return isEqual; } diff --git a/packages/react-native/ReactCommon/yoga/yoga/style/Style.h b/packages/react-native/ReactCommon/yoga/yoga/style/Style.h index a25d3a28ebe..9ce453b7fc3 100644 --- a/packages/react-native/ReactCommon/yoga/yoga/style/Style.h +++ b/packages/react-native/ReactCommon/yoga/yoga/style/Style.h @@ -7,6 +7,7 @@ #pragma once +#include #include #include #include @@ -341,6 +342,22 @@ class YG_EXPORT Style { : FloatOptional{0.0}); } + // Percentage margins and paddings resolve against the owner's width on both + // axes, and a percentage size or size bound against the owner's size on its + // own axis. + bool resolvesAgainstOwnerWidth() const { + return anyIsPercent(margin_) || anyIsPercent(padding_) || + dimensions_[yoga::to_underlying(Dimension::Width)].isPercent() || + minDimensions_[yoga::to_underlying(Dimension::Width)].isPercent() || + maxDimensions_[yoga::to_underlying(Dimension::Width)].isPercent(); + } + + bool resolvesAgainstOwnerHeight() const { + return dimensions_[yoga::to_underlying(Dimension::Height)].isPercent() || + minDimensions_[yoga::to_underlying(Dimension::Height)].isPercent() || + maxDimensions_[yoga::to_underlying(Dimension::Height)].isPercent(); + } + FloatOptional aspectRatio() const { return pool_.getNumber(aspectRatio_); } @@ -677,6 +694,15 @@ class YG_EXPORT Style { using Edges = std::array()>; using Gutters = std::array()>; + template + static inline bool anyIsPercent( + const std::array& handles) { + return std::any_of( + handles.begin(), handles.end(), [](const StyleValueHandle& handle) { + return handle.isPercent(); + }); + } + static inline bool numbersEqual( const StyleValueHandle& lhsHandle, const StyleValuePool& lhsPool, diff --git a/scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api b/scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api index acadaa2d53d..815efad8a90 100644 --- a/scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api @@ -6686,6 +6686,8 @@ class facebook::yoga::Style { public bool isInlineStartPositionAuto(facebook::yoga::FlexDirection axis, facebook::yoga::Direction direction) const; public bool isInlineStartPositionDefined(facebook::yoga::FlexDirection axis, facebook::yoga::Direction direction) const; public bool operator==(const facebook::yoga::Style& other) const; + public bool resolvesAgainstOwnerHeight() const; + public bool resolvesAgainstOwnerWidth() const; public bool verticalInsetsDefined() const; public const facebook::yoga::GridLine& gridColumnEnd() const; public const facebook::yoga::GridLine& gridColumnStart() const; @@ -7048,6 +7050,8 @@ struct facebook::yoga::CachedMeasurement { public float availableWidth; public float computedHeight; public float computedWidth; + public float ownerHeight; + public float ownerWidth; } struct facebook::yoga::Event { diff --git a/scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api b/scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api index 28264aedf3a..c33b628014f 100644 --- a/scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api @@ -11116,6 +11116,8 @@ class facebook::yoga::Style { public bool isInlineStartPositionAuto(facebook::yoga::FlexDirection axis, facebook::yoga::Direction direction) const; public bool isInlineStartPositionDefined(facebook::yoga::FlexDirection axis, facebook::yoga::Direction direction) const; public bool operator==(const facebook::yoga::Style& other) const; + public bool resolvesAgainstOwnerHeight() const; + public bool resolvesAgainstOwnerWidth() const; public bool verticalInsetsDefined() const; public const facebook::yoga::GridLine& gridColumnEnd() const; public const facebook::yoga::GridLine& gridColumnStart() const; @@ -11478,6 +11480,8 @@ struct facebook::yoga::CachedMeasurement { public float availableWidth; public float computedHeight; public float computedWidth; + public float ownerHeight; + public float ownerWidth; } struct facebook::yoga::Event { diff --git a/scripts/cxx-api/api-snapshots/ReactAndroidNewarchCxx.api b/scripts/cxx-api/api-snapshots/ReactAndroidNewarchCxx.api index 9226c155ead..349e6e4de87 100644 --- a/scripts/cxx-api/api-snapshots/ReactAndroidNewarchCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAndroidNewarchCxx.api @@ -6535,6 +6535,8 @@ class facebook::yoga::Style { public bool isInlineStartPositionAuto(facebook::yoga::FlexDirection axis, facebook::yoga::Direction direction) const; public bool isInlineStartPositionDefined(facebook::yoga::FlexDirection axis, facebook::yoga::Direction direction) const; public bool operator==(const facebook::yoga::Style& other) const; + public bool resolvesAgainstOwnerHeight() const; + public bool resolvesAgainstOwnerWidth() const; public bool verticalInsetsDefined() const; public const facebook::yoga::GridLine& gridColumnEnd() const; public const facebook::yoga::GridLine& gridColumnStart() const; @@ -6897,6 +6899,8 @@ struct facebook::yoga::CachedMeasurement { public float availableWidth; public float computedHeight; public float computedWidth; + public float ownerHeight; + public float ownerWidth; } struct facebook::yoga::Event { diff --git a/scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api b/scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api index 9226c155ead..349e6e4de87 100644 --- a/scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api @@ -6535,6 +6535,8 @@ class facebook::yoga::Style { public bool isInlineStartPositionAuto(facebook::yoga::FlexDirection axis, facebook::yoga::Direction direction) const; public bool isInlineStartPositionDefined(facebook::yoga::FlexDirection axis, facebook::yoga::Direction direction) const; public bool operator==(const facebook::yoga::Style& other) const; + public bool resolvesAgainstOwnerHeight() const; + public bool resolvesAgainstOwnerWidth() const; public bool verticalInsetsDefined() const; public const facebook::yoga::GridLine& gridColumnEnd() const; public const facebook::yoga::GridLine& gridColumnStart() const; @@ -6897,6 +6899,8 @@ struct facebook::yoga::CachedMeasurement { public float availableWidth; public float computedHeight; public float computedWidth; + public float ownerHeight; + public float ownerWidth; } struct facebook::yoga::Event { diff --git a/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api index 2fff899b856..483efc9b46d 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api @@ -8470,6 +8470,8 @@ class facebook::yoga::Style { public bool isInlineStartPositionAuto(facebook::yoga::FlexDirection axis, facebook::yoga::Direction direction) const; public bool isInlineStartPositionDefined(facebook::yoga::FlexDirection axis, facebook::yoga::Direction direction) const; public bool operator==(const facebook::yoga::Style& other) const; + public bool resolvesAgainstOwnerHeight() const; + public bool resolvesAgainstOwnerWidth() const; public bool verticalInsetsDefined() const; public const facebook::yoga::GridLine& gridColumnEnd() const; public const facebook::yoga::GridLine& gridColumnStart() const; @@ -8832,6 +8834,8 @@ struct facebook::yoga::CachedMeasurement { public float availableWidth; public float computedHeight; public float computedWidth; + public float ownerHeight; + public float ownerWidth; } struct facebook::yoga::Event { diff --git a/scripts/cxx-api/api-snapshots/ReactAppleFrameworksCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleFrameworksCxx.api index 9a63dc8e97e..b1f98e0f87f 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleFrameworksCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleFrameworksCxx.api @@ -12935,6 +12935,8 @@ class facebook::yoga::Style { public bool isInlineStartPositionAuto(facebook::yoga::FlexDirection axis, facebook::yoga::Direction direction) const; public bool isInlineStartPositionDefined(facebook::yoga::FlexDirection axis, facebook::yoga::Direction direction) const; public bool operator==(const facebook::yoga::Style& other) const; + public bool resolvesAgainstOwnerHeight() const; + public bool resolvesAgainstOwnerWidth() const; public bool verticalInsetsDefined() const; public const facebook::yoga::GridLine& gridColumnEnd() const; public const facebook::yoga::GridLine& gridColumnStart() const; @@ -13297,6 +13299,8 @@ struct facebook::yoga::CachedMeasurement { public float availableWidth; public float computedHeight; public float computedWidth; + public float ownerHeight; + public float ownerWidth; } struct facebook::yoga::Event { diff --git a/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api index 4b2b18e6599..e9ea4c1f053 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api @@ -8321,6 +8321,8 @@ class facebook::yoga::Style { public bool isInlineStartPositionAuto(facebook::yoga::FlexDirection axis, facebook::yoga::Direction direction) const; public bool isInlineStartPositionDefined(facebook::yoga::FlexDirection axis, facebook::yoga::Direction direction) const; public bool operator==(const facebook::yoga::Style& other) const; + public bool resolvesAgainstOwnerHeight() const; + public bool resolvesAgainstOwnerWidth() const; public bool verticalInsetsDefined() const; public const facebook::yoga::GridLine& gridColumnEnd() const; public const facebook::yoga::GridLine& gridColumnStart() const; @@ -8683,6 +8685,8 @@ struct facebook::yoga::CachedMeasurement { public float availableWidth; public float computedHeight; public float computedWidth; + public float ownerHeight; + public float ownerWidth; } struct facebook::yoga::Event { diff --git a/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api b/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api index dc9ad1496d1..57197975be4 100644 --- a/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api @@ -8329,6 +8329,8 @@ class facebook::yoga::Style { public bool isInlineStartPositionAuto(facebook::yoga::FlexDirection axis, facebook::yoga::Direction direction) const; public bool isInlineStartPositionDefined(facebook::yoga::FlexDirection axis, facebook::yoga::Direction direction) const; public bool operator==(const facebook::yoga::Style& other) const; + public bool resolvesAgainstOwnerHeight() const; + public bool resolvesAgainstOwnerWidth() const; public bool verticalInsetsDefined() const; public const facebook::yoga::GridLine& gridColumnEnd() const; public const facebook::yoga::GridLine& gridColumnStart() const; @@ -8691,6 +8693,8 @@ struct facebook::yoga::CachedMeasurement { public float availableWidth; public float computedHeight; public float computedWidth; + public float ownerHeight; + public float ownerWidth; } struct facebook::yoga::Event { diff --git a/scripts/cxx-api/api-snapshots/ReactCommonDebugCxx.api b/scripts/cxx-api/api-snapshots/ReactCommonDebugCxx.api index 5a6e9ec1abb..b8735f0528a 100644 --- a/scripts/cxx-api/api-snapshots/ReactCommonDebugCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactCommonDebugCxx.api @@ -3930,6 +3930,8 @@ class facebook::yoga::Style { public bool isInlineStartPositionAuto(facebook::yoga::FlexDirection axis, facebook::yoga::Direction direction) const; public bool isInlineStartPositionDefined(facebook::yoga::FlexDirection axis, facebook::yoga::Direction direction) const; public bool operator==(const facebook::yoga::Style& other) const; + public bool resolvesAgainstOwnerHeight() const; + public bool resolvesAgainstOwnerWidth() const; public bool verticalInsetsDefined() const; public const facebook::yoga::GridLine& gridColumnEnd() const; public const facebook::yoga::GridLine& gridColumnStart() const; @@ -4292,6 +4294,8 @@ struct facebook::yoga::CachedMeasurement { public float availableWidth; public float computedHeight; public float computedWidth; + public float ownerHeight; + public float ownerWidth; } struct facebook::yoga::Event { diff --git a/scripts/cxx-api/api-snapshots/ReactCommonFrameworksCxx.api b/scripts/cxx-api/api-snapshots/ReactCommonFrameworksCxx.api index 905c30385cd..7933bdb1a43 100644 --- a/scripts/cxx-api/api-snapshots/ReactCommonFrameworksCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactCommonFrameworksCxx.api @@ -8260,6 +8260,8 @@ class facebook::yoga::Style { public bool isInlineStartPositionAuto(facebook::yoga::FlexDirection axis, facebook::yoga::Direction direction) const; public bool isInlineStartPositionDefined(facebook::yoga::FlexDirection axis, facebook::yoga::Direction direction) const; public bool operator==(const facebook::yoga::Style& other) const; + public bool resolvesAgainstOwnerHeight() const; + public bool resolvesAgainstOwnerWidth() const; public bool verticalInsetsDefined() const; public const facebook::yoga::GridLine& gridColumnEnd() const; public const facebook::yoga::GridLine& gridColumnStart() const; @@ -8622,6 +8624,8 @@ struct facebook::yoga::CachedMeasurement { public float availableWidth; public float computedHeight; public float computedWidth; + public float ownerHeight; + public float ownerWidth; } struct facebook::yoga::Event { diff --git a/scripts/cxx-api/api-snapshots/ReactCommonNewarchCxx.api b/scripts/cxx-api/api-snapshots/ReactCommonNewarchCxx.api index 4558e79f17d..6b1ebcc6d52 100644 --- a/scripts/cxx-api/api-snapshots/ReactCommonNewarchCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactCommonNewarchCxx.api @@ -3923,6 +3923,8 @@ class facebook::yoga::Style { public bool isInlineStartPositionAuto(facebook::yoga::FlexDirection axis, facebook::yoga::Direction direction) const; public bool isInlineStartPositionDefined(facebook::yoga::FlexDirection axis, facebook::yoga::Direction direction) const; public bool operator==(const facebook::yoga::Style& other) const; + public bool resolvesAgainstOwnerHeight() const; + public bool resolvesAgainstOwnerWidth() const; public bool verticalInsetsDefined() const; public const facebook::yoga::GridLine& gridColumnEnd() const; public const facebook::yoga::GridLine& gridColumnStart() const; @@ -4285,6 +4287,8 @@ struct facebook::yoga::CachedMeasurement { public float availableWidth; public float computedHeight; public float computedWidth; + public float ownerHeight; + public float ownerWidth; } struct facebook::yoga::Event { diff --git a/scripts/cxx-api/api-snapshots/ReactCommonReleaseCxx.api b/scripts/cxx-api/api-snapshots/ReactCommonReleaseCxx.api index 4558e79f17d..6b1ebcc6d52 100644 --- a/scripts/cxx-api/api-snapshots/ReactCommonReleaseCxx.api +++ b/scripts/cxx-api/api-snapshots/ReactCommonReleaseCxx.api @@ -3923,6 +3923,8 @@ class facebook::yoga::Style { public bool isInlineStartPositionAuto(facebook::yoga::FlexDirection axis, facebook::yoga::Direction direction) const; public bool isInlineStartPositionDefined(facebook::yoga::FlexDirection axis, facebook::yoga::Direction direction) const; public bool operator==(const facebook::yoga::Style& other) const; + public bool resolvesAgainstOwnerHeight() const; + public bool resolvesAgainstOwnerWidth() const; public bool verticalInsetsDefined() const; public const facebook::yoga::GridLine& gridColumnEnd() const; public const facebook::yoga::GridLine& gridColumnStart() const; @@ -4285,6 +4287,8 @@ struct facebook::yoga::CachedMeasurement { public float availableWidth; public float computedHeight; public float computedWidth; + public float ownerHeight; + public float ownerWidth; } struct facebook::yoga::Event {