From 58e284f1753603ad583eb5d4c76128513828f558 Mon Sep 17 00:00:00 2001 From: MaksimGalois Date: Mon, 25 May 2026 15:29:47 +0800 Subject: [PATCH] fix: incorrect cache validation logic causing cache false hit --- tests/YGMeasureCacheTest.cpp | 24 ++++++++++++++++++++++++ yoga/algorithm/Cache.cpp | 4 ++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/tests/YGMeasureCacheTest.cpp b/tests/YGMeasureCacheTest.cpp index 850c17e6d7..2d87f8fb04 100644 --- a/tests/YGMeasureCacheTest.cpp +++ b/tests/YGMeasureCacheTest.cpp @@ -176,3 +176,27 @@ TEST( ASSERT_EQ(1, measureCount); } + +TEST(YogaTest, remeasure_when_container_grows_with_child_margin) { + const YGNodeRef root = YGNodeNew(); + YGNodeStyleSetWidth(root, 10.f); + YGNodeStyleSetHeight(root, 10.f); + + const YGNodeRef child = YGNodeNew(); + int measureCount = 0; + YGNodeSetContext(child, &measureCount); + YGNodeSetMeasureFunc(child, _measureMin); + YGNodeStyleSetMargin(child, YGEdgeBottom, 5.f); + YGNodeInsertChild(root, child, 0); + + YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); + ASSERT_EQ(1, measureCount); + ASSERT_FLOAT_EQ(5.0f, YGNodeLayoutGetHeight(child)); + + YGNodeStyleSetHeight(root, 15.0f); + YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); + ASSERT_EQ(2, measureCount); + ASSERT_FLOAT_EQ(10.0f, YGNodeLayoutGetHeight(child)); + + YGNodeFreeRecursive(root); +} diff --git a/yoga/algorithm/Cache.cpp b/yoga/algorithm/Cache.cpp index 9154fc6ce9..02b75177fd 100644 --- a/yoga/algorithm/Cache.cpp +++ b/yoga/algorithm/Cache.cpp @@ -97,7 +97,7 @@ bool canUseCachedMeasurement( widthMode, availableWidth - marginRow, lastWidthMode, - lastAvailableWidth, + lastAvailableWidth - marginRow, lastComputedWidth); const bool heightIsCompatible = hasSameHeightSpec || @@ -112,7 +112,7 @@ bool canUseCachedMeasurement( newSizeIsStricterAndStillValid(heightMode, availableHeight - marginColumn, lastHeightMode, - lastAvailableHeight, + lastAvailableHeight - marginColumn, lastComputedHeight); return widthIsCompatible && heightIsCompatible;