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:
@@ -64,6 +64,7 @@ public class CSSNode {
|
||||
public int lineIndex = 0;
|
||||
|
||||
CSSNode nextAbsoluteChild;
|
||||
CSSNode nextFlexChild;
|
||||
|
||||
private @Nullable ArrayList<CSSNode> mChildren;
|
||||
private @Nullable CSSNode mParent;
|
||||
|
@@ -453,11 +453,15 @@ public class LayoutEngine {
|
||||
float totalFlexible = 0;
|
||||
int nonFlexibleChildrenCount = 0;
|
||||
|
||||
CSSNode firstFlexChild = null;
|
||||
CSSNode currentFlexChild = null;
|
||||
|
||||
float maxWidth;
|
||||
for (i = startLine; i < childCount; ++i) {
|
||||
child = node.getChildAt(i);
|
||||
|
||||
child.nextAbsoluteChild = null;
|
||||
child.nextFlexChild = null;
|
||||
|
||||
// Pre-fill cross axis dimensions when the child is using stretch before
|
||||
// we call the recursive layout pass
|
||||
@@ -511,6 +515,16 @@ public class LayoutEngine {
|
||||
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.nextFlexChild = 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
|
||||
@@ -584,21 +598,20 @@ public class LayoutEngine {
|
||||
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.getChildAt(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.nextFlexChild;
|
||||
}
|
||||
flexibleMainDim = remainingMainDim / totalFlexible;
|
||||
|
||||
@@ -607,31 +620,32 @@ public class LayoutEngine {
|
||||
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.getChildAt(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 = CSSConstants.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(layoutContext, child, maxWidth, direction);
|
||||
maxWidth = CSSConstants.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(layoutContext, currentFlexChild, maxWidth, direction);
|
||||
|
||||
child = currentFlexChild;
|
||||
currentFlexChild = currentFlexChild.nextFlexChild;
|
||||
child.nextFlexChild = null;
|
||||
}
|
||||
|
||||
// We use justifyContent to figure out how to allocate the remaining
|
||||
|
Reference in New Issue
Block a user