From 996f2a03d598c593074da6c921d96b849ec9e308 Mon Sep 17 00:00:00 2001 From: Lucas Rocha Date: Tue, 8 Sep 2015 15:34:27 +0100 Subject: [PATCH] Merge pre-fill loop into main line loop There's no need to go through all children before starting the main line loop as we'll visit all children in the former loop anyway. This diff merges the pre-fill loop into the main line one to avoid an extraneous traversal on the node's children. --- src/Layout.c | 73 +++++++++---------- src/Layout.js | 73 +++++++++---------- .../com/facebook/csslayout/LayoutEngine.java | 73 +++++++++---------- 3 files changed, 105 insertions(+), 114 deletions(-) diff --git a/src/Layout.c b/src/Layout.c index 51311d8e..be0d7d20 100644 --- a/src/Layout.c +++ b/src/Layout.c @@ -604,44 +604,6 @@ static void layoutNodeImpl(css_node_t *node, float parentMaxWidth, css_direction css_node_t* child; css_flex_direction_t axis; - // Pre-fill some dimensions straight from the parent - for (i = 0; i < childCount; ++i) { - child = node->get_child(node->context, i); - // Pre-fill cross axis dimensions when the child is using stretch before - // we call the recursive layout pass - if (getAlignItem(node, child) == CSS_ALIGN_STRETCH && - child->style.position_type == CSS_POSITION_RELATIVE && - isCrossDimDefined && - !isDimDefined(child, crossAxis)) { - child->layout.dimensions[dim[crossAxis]] = fmaxf( - boundAxis(child, crossAxis, node->layout.dimensions[dim[crossAxis]] - - paddingAndBorderAxisCross - getMarginAxis(child, crossAxis)), - // You never want to go smaller than padding - getPaddingAndBorderAxis(child, crossAxis) - ); - } else if (child->style.position_type == CSS_POSITION_ABSOLUTE) { - // Pre-fill dimensions when using absolute position and both offsets for the axis are defined (either both - // left and right or top and bottom). - for (ii = 0; ii < 2; ii++) { - axis = (ii != 0) ? CSS_FLEX_DIRECTION_ROW : CSS_FLEX_DIRECTION_COLUMN; - if (!isUndefined(node->layout.dimensions[dim[axis]]) && - !isDimDefined(child, axis) && - isPosDefined(child, leading[axis]) && - isPosDefined(child, trailing[axis])) { - child->layout.dimensions[dim[axis]] = fmaxf( - boundAxis(child, axis, node->layout.dimensions[dim[axis]] - - getPaddingAndBorderAxis(node, axis) - - getMarginAxis(child, axis) - - getPosition(child, leading[axis]) - - getPosition(child, trailing[axis])), - // You never want to go smaller than padding - getPaddingAndBorderAxis(child, axis) - ); - } - } - } - } - float definedMainDim = CSS_UNDEFINED; if (isMainDimDefined) { definedMainDim = node->layout.dimensions[dim[mainAxis]] - paddingAndBorderAxisMain; @@ -674,6 +636,41 @@ static void layoutNodeImpl(css_node_t *node, float parentMaxWidth, css_direction float maxWidth; for (i = startLine; i < childCount; ++i) { child = node->get_child(node->context, i); + + // Pre-fill cross axis dimensions when the child is using stretch before + // we call the recursive layout pass + if (getAlignItem(node, child) == CSS_ALIGN_STRETCH && + child->style.position_type == CSS_POSITION_RELATIVE && + isCrossDimDefined && + !isDimDefined(child, crossAxis)) { + child->layout.dimensions[dim[crossAxis]] = fmaxf( + boundAxis(child, crossAxis, node->layout.dimensions[dim[crossAxis]] - + paddingAndBorderAxisCross - getMarginAxis(child, crossAxis)), + // You never want to go smaller than padding + getPaddingAndBorderAxis(child, crossAxis) + ); + } else if (child->style.position_type == CSS_POSITION_ABSOLUTE) { + // Pre-fill dimensions when using absolute position and both offsets for the axis are defined (either both + // left and right or top and bottom). + for (ii = 0; ii < 2; ii++) { + axis = (ii != 0) ? CSS_FLEX_DIRECTION_ROW : CSS_FLEX_DIRECTION_COLUMN; + if (!isUndefined(node->layout.dimensions[dim[axis]]) && + !isDimDefined(child, axis) && + isPosDefined(child, leading[axis]) && + isPosDefined(child, trailing[axis])) { + child->layout.dimensions[dim[axis]] = fmaxf( + boundAxis(child, axis, node->layout.dimensions[dim[axis]] - + getPaddingAndBorderAxis(node, axis) - + getMarginAxis(child, axis) - + getPosition(child, leading[axis]) - + getPosition(child, trailing[axis])), + // You never want to go smaller than padding + getPaddingAndBorderAxis(child, axis) + ); + } + } + } + float nextContentDim = 0; // It only makes sense to consider a child flexible if we have a computed diff --git a/src/Layout.js b/src/Layout.js index 9691767d..957e4455 100755 --- a/src/Layout.js +++ b/src/Layout.js @@ -472,44 +472,6 @@ var computeLayout = (function() { var/*css_node_t**/ child; var/*(c)!css_flex_direction_t*//*(java)!int*/ axis; - // Pre-fill some dimensions straight from the parent - for (i = 0; i < childCount; ++i) { - child = node.children[i]; - // Pre-fill cross axis dimensions when the child is using stretch before - // we call the recursive layout pass - if (getAlignItem(node, child) === CSS_ALIGN_STRETCH && - getPositionType(child) === CSS_POSITION_RELATIVE && - isCrossDimDefined && - !isDimDefined(child, crossAxis)) { - child.layout[dim[crossAxis]] = fmaxf( - boundAxis(child, crossAxis, node.layout[dim[crossAxis]] - - paddingAndBorderAxisCross - getMarginAxis(child, crossAxis)), - // You never want to go smaller than padding - getPaddingAndBorderAxis(child, crossAxis) - ); - } else if (getPositionType(child) === CSS_POSITION_ABSOLUTE) { - // Pre-fill dimensions when using absolute position and both offsets for the axis are defined (either both - // left and right or top and bottom). - for (ii = 0; ii < 2; ii++) { - axis = (ii !== 0) ? CSS_FLEX_DIRECTION_ROW : CSS_FLEX_DIRECTION_COLUMN; - if (!isUndefined(node.layout[dim[axis]]) && - !isDimDefined(child, axis) && - isPosDefined(child, leading[axis]) && - isPosDefined(child, trailing[axis])) { - child.layout[dim[axis]] = fmaxf( - boundAxis(child, axis, node.layout[dim[axis]] - - getPaddingAndBorderAxis(node, axis) - - getMarginAxis(child, axis) - - getPosition(child, leading[axis]) - - getPosition(child, trailing[axis])), - // You never want to go smaller than padding - getPaddingAndBorderAxis(child, axis) - ); - } - } - } - } - var/*float*/ definedMainDim = CSS_UNDEFINED; if (isMainDimDefined) { definedMainDim = node.layout[dim[mainAxis]] - paddingAndBorderAxisMain; @@ -542,6 +504,41 @@ var computeLayout = (function() { var/*float*/ maxWidth; for (i = startLine; i < childCount; ++i) { child = node.children[i]; + + // Pre-fill cross axis dimensions when the child is using stretch before + // we call the recursive layout pass + if (getAlignItem(node, child) === CSS_ALIGN_STRETCH && + getPositionType(child) === CSS_POSITION_RELATIVE && + isCrossDimDefined && + !isDimDefined(child, crossAxis)) { + child.layout[dim[crossAxis]] = fmaxf( + boundAxis(child, crossAxis, node.layout[dim[crossAxis]] - + paddingAndBorderAxisCross - getMarginAxis(child, crossAxis)), + // You never want to go smaller than padding + getPaddingAndBorderAxis(child, crossAxis) + ); + } else if (getPositionType(child) === CSS_POSITION_ABSOLUTE) { + // Pre-fill dimensions when using absolute position and both offsets for the axis are defined (either both + // left and right or top and bottom). + for (ii = 0; ii < 2; ii++) { + axis = (ii !== 0) ? CSS_FLEX_DIRECTION_ROW : CSS_FLEX_DIRECTION_COLUMN; + if (!isUndefined(node.layout[dim[axis]]) && + !isDimDefined(child, axis) && + isPosDefined(child, leading[axis]) && + isPosDefined(child, trailing[axis])) { + child.layout[dim[axis]] = fmaxf( + boundAxis(child, axis, node.layout[dim[axis]] - + getPaddingAndBorderAxis(node, axis) - + getMarginAxis(child, axis) - + getPosition(child, leading[axis]) - + getPosition(child, trailing[axis])), + // You never want to go smaller than padding + getPaddingAndBorderAxis(child, axis) + ); + } + } + } + var/*float*/ nextContentDim = 0; // It only makes sense to consider a child flexible if we have a computed diff --git a/src/java/src/com/facebook/csslayout/LayoutEngine.java b/src/java/src/com/facebook/csslayout/LayoutEngine.java index f75e5795..cc92377d 100644 --- a/src/java/src/com/facebook/csslayout/LayoutEngine.java +++ b/src/java/src/com/facebook/csslayout/LayoutEngine.java @@ -421,44 +421,6 @@ public class LayoutEngine { CSSNode child; int axis; - // Pre-fill some dimensions straight from the parent - for (i = 0; i < childCount; ++i) { - child = node.getChildAt(i); - // Pre-fill cross axis dimensions when the child is using stretch before - // we call the recursive layout pass - if (getAlignItem(node, child) == CSSAlign.STRETCH && - child.style.positionType == CSSPositionType.RELATIVE && - isCrossDimDefined && - !isDimDefined(child, crossAxis)) { - child.layout.dimensions[dim[crossAxis]] = Math.max( - boundAxis(child, crossAxis, node.layout.dimensions[dim[crossAxis]] - - paddingAndBorderAxisCross - getMarginAxis(child, crossAxis)), - // You never want to go smaller than padding - getPaddingAndBorderAxis(child, crossAxis) - ); - } else if (child.style.positionType == CSSPositionType.ABSOLUTE) { - // Pre-fill dimensions when using absolute position and both offsets for the axis are defined (either both - // left and right or top and bottom). - for (ii = 0; ii < 2; ii++) { - axis = (ii != 0) ? CSS_FLEX_DIRECTION_ROW : CSS_FLEX_DIRECTION_COLUMN; - if (!isUndefined(node.layout.dimensions[dim[axis]]) && - !isDimDefined(child, axis) && - isPosDefined(child, leading[axis]) && - isPosDefined(child, trailing[axis])) { - child.layout.dimensions[dim[axis]] = Math.max( - boundAxis(child, axis, node.layout.dimensions[dim[axis]] - - getPaddingAndBorderAxis(node, axis) - - getMarginAxis(child, axis) - - getPosition(child, leading[axis]) - - getPosition(child, trailing[axis])), - // You never want to go smaller than padding - getPaddingAndBorderAxis(child, axis) - ); - } - } - } - } - float definedMainDim = CSSConstants.UNDEFINED; if (isMainDimDefined) { definedMainDim = node.layout.dimensions[dim[mainAxis]] - paddingAndBorderAxisMain; @@ -491,6 +453,41 @@ public class LayoutEngine { float maxWidth; for (i = startLine; i < childCount; ++i) { child = node.getChildAt(i); + + // Pre-fill cross axis dimensions when the child is using stretch before + // we call the recursive layout pass + if (getAlignItem(node, child) == CSSAlign.STRETCH && + child.style.positionType == CSSPositionType.RELATIVE && + isCrossDimDefined && + !isDimDefined(child, crossAxis)) { + child.layout.dimensions[dim[crossAxis]] = Math.max( + boundAxis(child, crossAxis, node.layout.dimensions[dim[crossAxis]] - + paddingAndBorderAxisCross - getMarginAxis(child, crossAxis)), + // You never want to go smaller than padding + getPaddingAndBorderAxis(child, crossAxis) + ); + } else if (child.style.positionType == CSSPositionType.ABSOLUTE) { + // Pre-fill dimensions when using absolute position and both offsets for the axis are defined (either both + // left and right or top and bottom). + for (ii = 0; ii < 2; ii++) { + axis = (ii != 0) ? CSS_FLEX_DIRECTION_ROW : CSS_FLEX_DIRECTION_COLUMN; + if (!isUndefined(node.layout.dimensions[dim[axis]]) && + !isDimDefined(child, axis) && + isPosDefined(child, leading[axis]) && + isPosDefined(child, trailing[axis])) { + child.layout.dimensions[dim[axis]] = Math.max( + boundAxis(child, axis, node.layout.dimensions[dim[axis]] - + getPaddingAndBorderAxis(node, axis) - + getMarginAxis(child, axis) - + getPosition(child, leading[axis]) - + getPosition(child, trailing[axis])), + // You never want to go smaller than padding + getPaddingAndBorderAxis(child, axis) + ); + } + } + } + float nextContentDim = 0; // It only makes sense to consider a child flexible if we have a computed