Files
yoga/tests/YGDirtyMarkingTest.cpp
Pritesh Nandgaonkar c5182c4bf5 Used hasDirtyChildren tag for the optimization
Summary:
More fine grained dirty marking

Currently a node's dirty flag propagates to the root of the tree ensuring that when any node is invalidated its whole subtree will be re-calculated. This is often times not needed. There are many properties which only effects a node's children and would not need to propagate all the way to the root such as align-items. Also in cases where the style does change layout it may not need to propagate all the way to the root but can often stop at the nearest position: absolute parent.

This change has the potential of greatly improving performance of re-calculating a tree.

This might require adding a second dirty flag named hasDirtyDescendants ensuring that traversal still works even though a parent is not marked as dirty.

Reviewed By: emilsjolander

Differential Revision: D6134754

fbshipit-source-id: bbcfee14058140b946401de756a3f130de0f51cd
2017-10-31 08:22:29 -07:00

184 lines
6.0 KiB
C++

/**
* Copyright (c) 2014-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
#include <gtest/gtest.h>
#include <yoga/Yoga.h>
TEST(YogaTest, dirty_child_propogation) {
const YGNodeRef root = YGNodeNew();
YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
YGNodeStyleSetHeight(root, 200);
YGNodeStyleSetWidth(root, 500);
const YGNodeRef child0 = YGNodeNew();
YGNodeStyleSetWidth(child0, 30);
YGNodeStyleSetHeight(child0, 30);
YGNodeInsertChild(root, child0, 0);
const YGNodeRef child1 = YGNodeNew();
YGNodeStyleSetPositionType(child1, YGPositionTypeAbsolute);
YGNodeStyleSetPosition(child1, YGEdgeStart, 10);
YGNodeStyleSetPosition(child1, YGEdgeTop, 10);
YGNodeStyleSetWidth(child1, 10);
YGNodeStyleSetHeight(child1, 10);
YGNodeInsertChild(root, child1, 1);
const YGNodeRef child1_child0 = YGNodeNew();
YGNodeStyleSetWidth(child1_child0, 10);
YGNodeStyleSetHeight(child1_child0, 10);
YGNodeInsertChild(child1, child1_child0, 0);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
YGNodeStyleSetWidth(child1_child0, 20);
EXPECT_TRUE(YGNodeIsDirty(child1_child0));
EXPECT_TRUE(YGNodeHasDirtyDescendants(child1));
EXPECT_TRUE(YGNodeIsDirty(child1));
EXPECT_FALSE(YGNodeIsDirty(root));
EXPECT_TRUE(YGNodeHasDirtyDescendants(root));
EXPECT_FALSE(YGNodeIsDirty(child0));
EXPECT_FALSE(YGNodeHasDirtyDescendants(child0));
}
TEST(YogaTest, dirty_propagation) {
const YGNodeRef root = YGNodeNew();
YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
YGNodeStyleSetWidth(root, 100);
YGNodeStyleSetHeight(root, 100);
const YGNodeRef root_child0 = YGNodeNew();
YGNodeStyleSetWidth(root_child0, 50);
YGNodeStyleSetHeight(root_child0, 20);
YGNodeInsertChild(root, root_child0, 0);
const YGNodeRef root_child1 = YGNodeNew();
YGNodeStyleSetWidth(root_child1, 50);
YGNodeStyleSetHeight(root_child1, 20);
YGNodeInsertChild(root, root_child1, 1);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
YGNodeStyleSetWidth(root_child0, 20);
EXPECT_TRUE(YGNodeIsDirty(root_child0));
EXPECT_FALSE(YGNodeIsDirty(root_child1));
EXPECT_TRUE(YGNodeIsDirty(root));
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
EXPECT_FALSE(YGNodeIsDirty(root_child0));
EXPECT_FALSE(YGNodeIsDirty(root_child1));
EXPECT_FALSE(YGNodeIsDirty(root));
YGNodeFreeRecursive(root);
}
TEST(YogaTest, dirty_propagation_only_if_prop_changed) {
const YGNodeRef root = YGNodeNew();
YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
YGNodeStyleSetWidth(root, 100);
YGNodeStyleSetHeight(root, 100);
const YGNodeRef root_child0 = YGNodeNew();
YGNodeStyleSetWidth(root_child0, 50);
YGNodeStyleSetHeight(root_child0, 20);
YGNodeInsertChild(root, root_child0, 0);
const YGNodeRef root_child1 = YGNodeNew();
YGNodeStyleSetWidth(root_child1, 50);
YGNodeStyleSetHeight(root_child1, 20);
YGNodeInsertChild(root, root_child1, 1);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
YGNodeStyleSetWidth(root_child0, 50);
EXPECT_FALSE(YGNodeIsDirty(root_child0));
EXPECT_FALSE(YGNodeIsDirty(root_child1));
EXPECT_FALSE(YGNodeIsDirty(root));
YGNodeFreeRecursive(root);
}
TEST(YogaTest, dirty_mark_all_children_as_dirty_when_display_changes) {
const YGNodeRef root = YGNodeNew();
YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow);
YGNodeStyleSetHeight(root, 100);
const YGNodeRef child0 = YGNodeNew();
YGNodeStyleSetFlexGrow(child0, 1);
const YGNodeRef child1 = YGNodeNew();
YGNodeStyleSetFlexGrow(child1, 1);
const YGNodeRef child1_child0 = YGNodeNew();
const YGNodeRef child1_child0_child0 = YGNodeNew();
YGNodeStyleSetWidth(child1_child0_child0, 8);
YGNodeStyleSetHeight(child1_child0_child0, 16);
YGNodeInsertChild(child1_child0, child1_child0_child0, 0);
YGNodeInsertChild(child1, child1_child0, 0);
YGNodeInsertChild(root, child0, 0);
YGNodeInsertChild(root, child1, 0);
YGNodeStyleSetDisplay(child0, YGDisplayFlex);
YGNodeStyleSetDisplay(child1, YGDisplayNone);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetWidth(child1_child0_child0));
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetHeight(child1_child0_child0));
YGNodeStyleSetDisplay(child0, YGDisplayNone);
YGNodeStyleSetDisplay(child1, YGDisplayFlex);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
ASSERT_FLOAT_EQ(8, YGNodeLayoutGetWidth(child1_child0_child0));
ASSERT_FLOAT_EQ(16, YGNodeLayoutGetHeight(child1_child0_child0));
YGNodeStyleSetDisplay(child0, YGDisplayFlex);
YGNodeStyleSetDisplay(child1, YGDisplayNone);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetWidth(child1_child0_child0));
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetHeight(child1_child0_child0));
YGNodeStyleSetDisplay(child0, YGDisplayNone);
YGNodeStyleSetDisplay(child1, YGDisplayFlex);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
ASSERT_FLOAT_EQ(8, YGNodeLayoutGetWidth(child1_child0_child0));
ASSERT_FLOAT_EQ(16, YGNodeLayoutGetHeight(child1_child0_child0));
YGNodeFreeRecursive(root);
}
TEST(YogaTest, dirty_node_only_if_children_are_actually_removed) {
const YGNodeRef root = YGNodeNew();
YGNodeStyleSetAlignItems(root, YGAlignFlexStart);
YGNodeStyleSetWidth(root, 50);
YGNodeStyleSetHeight(root, 50);
const YGNodeRef child0 = YGNodeNew();
YGNodeStyleSetWidth(child0, 50);
YGNodeStyleSetHeight(child0, 25);
YGNodeInsertChild(root, child0, 0);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
const YGNodeRef child1 = YGNodeNew();
YGNodeRemoveChild(root, child1);
EXPECT_FALSE(YGNodeIsDirty(root));
YGNodeFree(child1);
YGNodeRemoveChild(root, child0);
EXPECT_TRUE(YGNodeIsDirty(root));
YGNodeFree(child0);
YGNodeFreeRecursive(root);
}