Reduce search range of flexible children
We were traversing all children to only perform calculations/changes to flexible children in order to avoid new allocations during layout. This diff ensures we only visit flexible children during layout calculations if any are present. We accomplish this by keeping a private linked list of flexible children.
This commit is contained in:
84
src/Layout.c
84
src/Layout.c
@@ -636,11 +636,15 @@ static void layoutNodeImpl(css_node_t *node, float parentMaxWidth, css_direction
|
||||
float totalFlexible = 0;
|
||||
int nonFlexibleChildrenCount = 0;
|
||||
|
||||
css_node_t* firstFlexChild = NULL;
|
||||
css_node_t* currentFlexChild = NULL;
|
||||
|
||||
float maxWidth;
|
||||
for (i = startLine; i < childCount; ++i) {
|
||||
child = node->get_child(node->context, i);
|
||||
|
||||
child->next_absolute_child = NULL;
|
||||
child->next_flex_child = NULL;
|
||||
|
||||
// Pre-fill cross axis dimensions when the child is using stretch before
|
||||
// we call the recursive layout pass
|
||||
@@ -694,6 +698,16 @@ static void layoutNodeImpl(css_node_t *node, float parentMaxWidth, css_direction
|
||||
flexibleChildrenCount++;
|
||||
totalFlexible += child->style.flex;
|
||||
|
||||
// Store a private linked list of flexible children so that we can
|
||||
// efficiently traverse them later.
|
||||
if (firstFlexChild == NULL) {
|
||||
firstFlexChild = child;
|
||||
}
|
||||
if (currentFlexChild != NULL) {
|
||||
currentFlexChild->next_flex_child = child;
|
||||
}
|
||||
currentFlexChild = child;
|
||||
|
||||
// Even if we don't know its exact size yet, we already know the padding,
|
||||
// border and margin. We'll use this partial information, which represents
|
||||
// the smallest possible size for the child, to compute the remaining
|
||||
@@ -767,21 +781,20 @@ static void layoutNodeImpl(css_node_t *node, float parentMaxWidth, css_direction
|
||||
float baseMainDim;
|
||||
float boundMainDim;
|
||||
|
||||
// Iterate over every child in the axis. If the flex share of remaining
|
||||
// space doesn't meet min/max bounds, remove this child from flex
|
||||
// calculations.
|
||||
for (i = startLine; i < endLine; ++i) {
|
||||
child = node->get_child(node->context, i);
|
||||
if (isFlex(child)) {
|
||||
baseMainDim = flexibleMainDim * child->style.flex +
|
||||
getPaddingAndBorderAxis(child, mainAxis);
|
||||
boundMainDim = boundAxis(child, mainAxis, baseMainDim);
|
||||
// If the flex share of remaining space doesn't meet min/max bounds,
|
||||
// remove this child from flex calculations.
|
||||
currentFlexChild = firstFlexChild;
|
||||
while (currentFlexChild != NULL) {
|
||||
baseMainDim = flexibleMainDim * currentFlexChild->style.flex +
|
||||
getPaddingAndBorderAxis(currentFlexChild, mainAxis);
|
||||
boundMainDim = boundAxis(currentFlexChild, mainAxis, baseMainDim);
|
||||
|
||||
if (baseMainDim != boundMainDim) {
|
||||
remainingMainDim -= boundMainDim;
|
||||
totalFlexible -= child->style.flex;
|
||||
}
|
||||
if (baseMainDim != boundMainDim) {
|
||||
remainingMainDim -= boundMainDim;
|
||||
totalFlexible -= currentFlexChild->style.flex;
|
||||
}
|
||||
|
||||
currentFlexChild = currentFlexChild->next_flex_child;
|
||||
}
|
||||
flexibleMainDim = remainingMainDim / totalFlexible;
|
||||
|
||||
@@ -790,31 +803,32 @@ static void layoutNodeImpl(css_node_t *node, float parentMaxWidth, css_direction
|
||||
if (flexibleMainDim < 0) {
|
||||
flexibleMainDim = 0;
|
||||
}
|
||||
// We iterate over the full array and only apply the action on flexible
|
||||
// children. This is faster than actually allocating a new array that
|
||||
// contains only flexible children.
|
||||
for (i = startLine; i < endLine; ++i) {
|
||||
child = node->get_child(node->context, i);
|
||||
if (isFlex(child)) {
|
||||
// At this point we know the final size of the element in the main
|
||||
// dimension
|
||||
child->layout.dimensions[dim[mainAxis]] = boundAxis(child, mainAxis,
|
||||
flexibleMainDim * child->style.flex + getPaddingAndBorderAxis(child, mainAxis)
|
||||
);
|
||||
|
||||
maxWidth = CSS_UNDEFINED;
|
||||
if (isDimDefined(node, resolvedRowAxis)) {
|
||||
maxWidth = node->layout.dimensions[dim[resolvedRowAxis]] -
|
||||
paddingAndBorderAxisResolvedRow;
|
||||
} else if (!isMainRowDirection) {
|
||||
maxWidth = parentMaxWidth -
|
||||
getMarginAxis(node, resolvedRowAxis) -
|
||||
paddingAndBorderAxisResolvedRow;
|
||||
}
|
||||
currentFlexChild = firstFlexChild;
|
||||
while (currentFlexChild != NULL) {
|
||||
// At this point we know the final size of the element in the main
|
||||
// dimension
|
||||
currentFlexChild->layout.dimensions[dim[mainAxis]] = boundAxis(currentFlexChild, mainAxis,
|
||||
flexibleMainDim * currentFlexChild->style.flex +
|
||||
getPaddingAndBorderAxis(currentFlexChild, mainAxis)
|
||||
);
|
||||
|
||||
// And we recursively call the layout algorithm for this child
|
||||
layoutNode(child, maxWidth, direction);
|
||||
maxWidth = CSS_UNDEFINED;
|
||||
if (isDimDefined(node, resolvedRowAxis)) {
|
||||
maxWidth = node->layout.dimensions[dim[resolvedRowAxis]] -
|
||||
paddingAndBorderAxisResolvedRow;
|
||||
} else if (!isMainRowDirection) {
|
||||
maxWidth = parentMaxWidth -
|
||||
getMarginAxis(node, resolvedRowAxis) -
|
||||
paddingAndBorderAxisResolvedRow;
|
||||
}
|
||||
|
||||
// And we recursively call the layout algorithm for this child
|
||||
layoutNode(currentFlexChild, maxWidth, direction);
|
||||
|
||||
child = currentFlexChild;
|
||||
currentFlexChild = currentFlexChild->next_flex_child;
|
||||
child->next_flex_child = NULL;
|
||||
}
|
||||
|
||||
// We use justifyContent to figure out how to allocate the remaining
|
||||
|
Reference in New Issue
Block a user