From e4b50f2a8dff9c6c4306316bf7270f1abaf3a5b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lukas=20W=C3=B6hrl?= Date: Tue, 24 Jan 2017 18:57:07 -0800 Subject: [PATCH] Fix unnecessary measure calls Summary: Fix #334 Closes https://github.com/facebook/yoga/pull/347 Reviewed By: dshahidehpour Differential Revision: D4455438 Pulled By: emilsjolander fbshipit-source-id: 013c89e71757d9048708ec85cbb6af9f33ac1ea6 --- tests/YGMeasureCacheTest.cpp | 41 ++++++++++++++++++++++++++++++++++++ yoga/Yoga.c | 4 ++-- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/tests/YGMeasureCacheTest.cpp b/tests/YGMeasureCacheTest.cpp index ca932b25..683b3ab8 100644 --- a/tests/YGMeasureCacheTest.cpp +++ b/tests/YGMeasureCacheTest.cpp @@ -39,6 +39,22 @@ static YGSize _measureMin(YGNodeRef node, }; } +static YGSize _measure_84_49(YGNodeRef node, + float width, + YGMeasureMode widthMode, + float height, + YGMeasureMode heightMode) { + int *measureCount = (int*) YGNodeGetContext(node); + if (measureCount) { + (*measureCount)++; + } + + return YGSize { + .width = 84.f, + .height = 49.f, + }; +} + TEST(YogaTest, measure_once_single_flexible_child) { const YGNodeRef root = YGNodeNew(); YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow); @@ -131,3 +147,28 @@ TEST(YogaTest, remeasure_with_atmost_computed_width_undefined_height) { YGNodeFreeRecursive(root); } + +TEST(YogaTest, remeasure_with_already_measured_value_smaller_but_still_float_equal) { + int measureCount = 0; + + const YGNodeRef root = YGNodeNew(); + YGNodeStyleSetWidth(root, 288.f); + YGNodeStyleSetHeight(root, 288.f); + YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow); + + const YGNodeRef root_child0 = YGNodeNew(); + YGNodeStyleSetPadding(root_child0, YGEdgeAll, 2.88f); + YGNodeStyleSetFlexDirection(root_child0, YGFlexDirectionRow); + YGNodeInsertChild(root, root_child0, 0); + + const YGNodeRef root_child0_child0 = YGNodeNew(); + YGNodeSetContext(root_child0_child0, &measureCount); + YGNodeSetMeasureFunc(root_child0_child0, _measure_84_49); + YGNodeInsertChild(root_child0, root_child0_child0, 0); + + YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); + + YGNodeFreeRecursive(root); + + ASSERT_EQ(1, measureCount); +} diff --git a/yoga/Yoga.c b/yoga/Yoga.c index 51a9287c..7ef28ed4 100644 --- a/yoga/Yoga.c +++ b/yoga/Yoga.c @@ -2785,7 +2785,7 @@ static inline bool YGMeasureModeOldSizeIsUnspecifiedAndStillFits(YGMeasureMode s YGMeasureMode lastSizeMode, float lastComputedSize) { return sizeMode == YGMeasureModeAtMost && lastSizeMode == YGMeasureModeUndefined && - size >= lastComputedSize; + (size >= lastComputedSize || YGFloatsEqual(size, lastComputedSize)); } static inline bool YGMeasureModeNewMeasureSizeIsStricterAndStillValid(YGMeasureMode sizeMode, @@ -2794,7 +2794,7 @@ static inline bool YGMeasureModeNewMeasureSizeIsStricterAndStillValid(YGMeasureM float lastSize, float lastComputedSize) { return lastSizeMode == YGMeasureModeAtMost && sizeMode == YGMeasureModeAtMost && - lastSize > size && lastComputedSize <= size; + lastSize > size && (lastComputedSize <= size || YGFloatsEqual(size, lastComputedSize)); } bool YGNodeCanUseCachedMeasurement(const YGMeasureMode widthMode,