Allow lazy resolution of edge dimension values (#1453)
Summary: X-link: https://github.com/facebook/react-native/pull/41347 Pull Request resolved: https://github.com/facebook/yoga/pull/1453 This follows the previous patterns used for `Gutters` and `Dimension`, where we hide CompactValue array implementation from `yoga::Style` callers. This allows a single read of a style to only need access to the resolved values of a single edge, vs all edges. This is cheap now because the interface is the representation, but gets expensive if `StyleValuePool` is the actual implementation. This prevents us from needing to resolve nine dimensions, in order to read a single value like `marginLeft`. Doing this, in the new style, also lets us remove `IdxRef` from the API. We unroll the structure dependent parts in the props parsing code, for something more verbose, but also a bit clearer. Changelog: [Internal] Reviewed By: joevilches Differential Revision: D50998164 fbshipit-source-id: 248396f9587e29d62cde05ae7512d8194f60c809
This commit is contained in:
committed by
Facebook GitHub Bot
parent
bb892af3a4
commit
f2c8acad2c
@@ -56,30 +56,29 @@ void Node::print() {
|
||||
}
|
||||
}
|
||||
|
||||
CompactValue Node::computeEdgeValueForRow(
|
||||
const Style::Edges& edges,
|
||||
YGEdge rowEdge,
|
||||
YGEdge edge) {
|
||||
if (edges[rowEdge].isDefined()) {
|
||||
return edges[rowEdge];
|
||||
} else if (edges[edge].isDefined()) {
|
||||
return edges[edge];
|
||||
} else if (edges[YGEdgeHorizontal].isDefined()) {
|
||||
return edges[YGEdgeHorizontal];
|
||||
// TODO: Edge value resolution should be moved to `yoga::Style`
|
||||
template <auto Field>
|
||||
CompactValue Node::computeEdgeValueForRow(YGEdge rowEdge, YGEdge edge) const {
|
||||
if ((style_.*Field)(rowEdge).isDefined()) {
|
||||
return (style_.*Field)(rowEdge);
|
||||
} else if ((style_.*Field)(edge).isDefined()) {
|
||||
return (style_.*Field)(edge);
|
||||
} else if ((style_.*Field)(YGEdgeHorizontal).isDefined()) {
|
||||
return (style_.*Field)(YGEdgeHorizontal);
|
||||
} else {
|
||||
return edges[YGEdgeAll];
|
||||
return (style_.*Field)(YGEdgeAll);
|
||||
}
|
||||
}
|
||||
|
||||
CompactValue Node::computeEdgeValueForColumn(
|
||||
const Style::Edges& edges,
|
||||
YGEdge edge) {
|
||||
if (edges[edge].isDefined()) {
|
||||
return edges[edge];
|
||||
} else if (edges[YGEdgeVertical].isDefined()) {
|
||||
return edges[YGEdgeVertical];
|
||||
// TODO: Edge value resolution should be moved to `yoga::Style`
|
||||
template <auto Field>
|
||||
CompactValue Node::computeEdgeValueForColumn(YGEdge edge) const {
|
||||
if ((style_.*Field)(edge).isDefined()) {
|
||||
return (style_.*Field)(edge);
|
||||
} else if ((style_.*Field)(YGEdgeVertical).isDefined()) {
|
||||
return (style_.*Field)(YGEdgeVertical);
|
||||
} else {
|
||||
return edges[YGEdgeAll];
|
||||
return (style_.*Field)(YGEdgeAll);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,8 +101,8 @@ YGEdge Node::getInlineEndEdgeUsingErrata(
|
||||
bool Node::isFlexStartPositionDefined(FlexDirection axis) const {
|
||||
const YGEdge startEdge = flexStartEdge(axis);
|
||||
auto leadingPosition = isRow(axis)
|
||||
? computeEdgeValueForRow(style_.position(), YGEdgeStart, startEdge)
|
||||
: computeEdgeValueForColumn(style_.position(), startEdge);
|
||||
? computeEdgeValueForRow<&Style::position>(YGEdgeStart, startEdge)
|
||||
: computeEdgeValueForColumn<&Style::position>(startEdge);
|
||||
|
||||
return leadingPosition.isDefined();
|
||||
}
|
||||
@@ -112,8 +111,8 @@ bool Node::isInlineStartPositionDefined(FlexDirection axis, Direction direction)
|
||||
const {
|
||||
const YGEdge startEdge = getInlineStartEdgeUsingErrata(axis, direction);
|
||||
auto leadingPosition = isRow(axis)
|
||||
? computeEdgeValueForRow(style_.position(), YGEdgeStart, startEdge)
|
||||
: computeEdgeValueForColumn(style_.position(), startEdge);
|
||||
? computeEdgeValueForRow<&Style::position>(YGEdgeStart, startEdge)
|
||||
: computeEdgeValueForColumn<&Style::position>(startEdge);
|
||||
|
||||
return leadingPosition.isDefined();
|
||||
}
|
||||
@@ -121,8 +120,8 @@ bool Node::isInlineStartPositionDefined(FlexDirection axis, Direction direction)
|
||||
bool Node::isFlexEndPositionDefined(FlexDirection axis) const {
|
||||
const YGEdge endEdge = flexEndEdge(axis);
|
||||
auto trailingPosition = isRow(axis)
|
||||
? computeEdgeValueForRow(style_.position(), YGEdgeEnd, endEdge)
|
||||
: computeEdgeValueForColumn(style_.position(), endEdge);
|
||||
? computeEdgeValueForRow<&Style::position>(YGEdgeEnd, endEdge)
|
||||
: computeEdgeValueForColumn<&Style::position>(endEdge);
|
||||
|
||||
return !trailingPosition.isUndefined();
|
||||
}
|
||||
@@ -131,8 +130,8 @@ bool Node::isInlineEndPositionDefined(FlexDirection axis, Direction direction)
|
||||
const {
|
||||
const YGEdge endEdge = getInlineEndEdgeUsingErrata(axis, direction);
|
||||
auto trailingPosition = isRow(axis)
|
||||
? computeEdgeValueForRow(style_.position(), YGEdgeEnd, endEdge)
|
||||
: computeEdgeValueForColumn(style_.position(), endEdge);
|
||||
? computeEdgeValueForRow<&Style::position>(YGEdgeEnd, endEdge)
|
||||
: computeEdgeValueForColumn<&Style::position>(endEdge);
|
||||
|
||||
return trailingPosition.isDefined();
|
||||
}
|
||||
@@ -140,8 +139,8 @@ bool Node::isInlineEndPositionDefined(FlexDirection axis, Direction direction)
|
||||
float Node::getFlexStartPosition(FlexDirection axis, float axisSize) const {
|
||||
const YGEdge startEdge = flexStartEdge(axis);
|
||||
auto leadingPosition = isRow(axis)
|
||||
? computeEdgeValueForRow(style_.position(), YGEdgeStart, startEdge)
|
||||
: computeEdgeValueForColumn(style_.position(), startEdge);
|
||||
? computeEdgeValueForRow<&Style::position>(YGEdgeStart, startEdge)
|
||||
: computeEdgeValueForColumn<&Style::position>(startEdge);
|
||||
|
||||
return resolveValue(leadingPosition, axisSize).unwrapOrDefault(0.0f);
|
||||
}
|
||||
@@ -152,8 +151,8 @@ float Node::getInlineStartPosition(
|
||||
float axisSize) const {
|
||||
const YGEdge startEdge = getInlineStartEdgeUsingErrata(axis, direction);
|
||||
auto leadingPosition = isRow(axis)
|
||||
? computeEdgeValueForRow(style_.position(), YGEdgeStart, startEdge)
|
||||
: computeEdgeValueForColumn(style_.position(), startEdge);
|
||||
? computeEdgeValueForRow<&Style::position>(YGEdgeStart, startEdge)
|
||||
: computeEdgeValueForColumn<&Style::position>(startEdge);
|
||||
|
||||
return resolveValue(leadingPosition, axisSize).unwrapOrDefault(0.0f);
|
||||
}
|
||||
@@ -161,8 +160,8 @@ float Node::getInlineStartPosition(
|
||||
float Node::getFlexEndPosition(FlexDirection axis, float axisSize) const {
|
||||
const YGEdge endEdge = flexEndEdge(axis);
|
||||
auto trailingPosition = isRow(axis)
|
||||
? computeEdgeValueForRow(style_.position(), YGEdgeEnd, endEdge)
|
||||
: computeEdgeValueForColumn(style_.position(), endEdge);
|
||||
? computeEdgeValueForRow<&Style::position>(YGEdgeEnd, endEdge)
|
||||
: computeEdgeValueForColumn<&Style::position>(endEdge);
|
||||
|
||||
return resolveValue(trailingPosition, axisSize).unwrapOrDefault(0.0f);
|
||||
}
|
||||
@@ -173,8 +172,8 @@ float Node::getInlineEndPosition(
|
||||
float axisSize) const {
|
||||
const YGEdge endEdge = getInlineEndEdgeUsingErrata(axis, direction);
|
||||
auto trailingPosition = isRow(axis)
|
||||
? computeEdgeValueForRow(style_.position(), YGEdgeEnd, endEdge)
|
||||
: computeEdgeValueForColumn(style_.position(), endEdge);
|
||||
? computeEdgeValueForRow<&Style::position>(YGEdgeEnd, endEdge)
|
||||
: computeEdgeValueForColumn<&Style::position>(endEdge);
|
||||
|
||||
return resolveValue(trailingPosition, axisSize).unwrapOrDefault(0.0f);
|
||||
}
|
||||
@@ -182,8 +181,8 @@ float Node::getInlineEndPosition(
|
||||
float Node::getFlexStartMargin(FlexDirection axis, float widthSize) const {
|
||||
const YGEdge startEdge = flexStartEdge(axis);
|
||||
auto leadingMargin = isRow(axis)
|
||||
? computeEdgeValueForRow(style_.margin(), YGEdgeStart, startEdge)
|
||||
: computeEdgeValueForColumn(style_.margin(), startEdge);
|
||||
? computeEdgeValueForRow<&Style::margin>(YGEdgeStart, startEdge)
|
||||
: computeEdgeValueForColumn<&Style::margin>(startEdge);
|
||||
|
||||
return resolveValue(leadingMargin, widthSize).unwrapOrDefault(0.0f);
|
||||
}
|
||||
@@ -194,8 +193,8 @@ float Node::getInlineStartMargin(
|
||||
float widthSize) const {
|
||||
const YGEdge startEdge = getInlineStartEdgeUsingErrata(axis, direction);
|
||||
auto leadingMargin = isRow(axis)
|
||||
? computeEdgeValueForRow(style_.margin(), YGEdgeStart, startEdge)
|
||||
: computeEdgeValueForColumn(style_.margin(), startEdge);
|
||||
? computeEdgeValueForRow<&Style::margin>(YGEdgeStart, startEdge)
|
||||
: computeEdgeValueForColumn<&Style::margin>(startEdge);
|
||||
|
||||
return resolveValue(leadingMargin, widthSize).unwrapOrDefault(0.0f);
|
||||
}
|
||||
@@ -203,8 +202,8 @@ float Node::getInlineStartMargin(
|
||||
float Node::getFlexEndMargin(FlexDirection axis, float widthSize) const {
|
||||
const YGEdge endEdge = flexEndEdge(axis);
|
||||
auto trailingMargin = isRow(axis)
|
||||
? computeEdgeValueForRow(style_.margin(), YGEdgeEnd, endEdge)
|
||||
: computeEdgeValueForColumn(style_.margin(), endEdge);
|
||||
? computeEdgeValueForRow<&Style::margin>(YGEdgeEnd, endEdge)
|
||||
: computeEdgeValueForColumn<&Style::margin>(endEdge);
|
||||
|
||||
return resolveValue(trailingMargin, widthSize).unwrapOrDefault(0.0f);
|
||||
}
|
||||
@@ -215,8 +214,8 @@ float Node::getInlineEndMargin(
|
||||
float widthSize) const {
|
||||
const YGEdge endEdge = getInlineEndEdgeUsingErrata(axis, direction);
|
||||
auto trailingMargin = isRow(axis)
|
||||
? computeEdgeValueForRow(style_.margin(), YGEdgeEnd, endEdge)
|
||||
: computeEdgeValueForColumn(style_.margin(), endEdge);
|
||||
? computeEdgeValueForRow<&Style::margin>(YGEdgeEnd, endEdge)
|
||||
: computeEdgeValueForColumn<&Style::margin>(endEdge);
|
||||
|
||||
return resolveValue(trailingMargin, widthSize).unwrapOrDefault(0.0f);
|
||||
}
|
||||
@@ -225,8 +224,8 @@ float Node::getInlineStartBorder(FlexDirection axis, Direction direction)
|
||||
const {
|
||||
const YGEdge startEdge = getInlineStartEdgeUsingErrata(axis, direction);
|
||||
YGValue leadingBorder = isRow(axis)
|
||||
? computeEdgeValueForRow(style_.border(), YGEdgeStart, startEdge)
|
||||
: computeEdgeValueForColumn(style_.border(), startEdge);
|
||||
? computeEdgeValueForRow<&Style::border>(YGEdgeStart, startEdge)
|
||||
: computeEdgeValueForColumn<&Style::border>(startEdge);
|
||||
|
||||
return maxOrDefined(leadingBorder.value, 0.0f);
|
||||
}
|
||||
@@ -235,9 +234,9 @@ float Node::getFlexStartBorder(FlexDirection axis, Direction direction) const {
|
||||
const YGEdge leadRelativeFlexItemEdge =
|
||||
flexStartRelativeEdge(axis, direction);
|
||||
YGValue leadingBorder = isRow(axis)
|
||||
? computeEdgeValueForRow(
|
||||
style_.border(), leadRelativeFlexItemEdge, flexStartEdge(axis))
|
||||
: computeEdgeValueForColumn(style_.border(), flexStartEdge(axis));
|
||||
? computeEdgeValueForRow<&Style::border>(
|
||||
leadRelativeFlexItemEdge, flexStartEdge(axis))
|
||||
: computeEdgeValueForColumn<&Style::border>(flexStartEdge(axis));
|
||||
|
||||
return maxOrDefined(leadingBorder.value, 0.0f);
|
||||
}
|
||||
@@ -245,8 +244,8 @@ float Node::getFlexStartBorder(FlexDirection axis, Direction direction) const {
|
||||
float Node::getInlineEndBorder(FlexDirection axis, Direction direction) const {
|
||||
const YGEdge endEdge = getInlineEndEdgeUsingErrata(axis, direction);
|
||||
YGValue trailingBorder = isRow(axis)
|
||||
? computeEdgeValueForRow(style_.border(), YGEdgeEnd, endEdge)
|
||||
: computeEdgeValueForColumn(style_.border(), endEdge);
|
||||
? computeEdgeValueForRow<&Style::border>(YGEdgeEnd, endEdge)
|
||||
: computeEdgeValueForColumn<&Style::border>(endEdge);
|
||||
|
||||
return maxOrDefined(trailingBorder.value, 0.0f);
|
||||
}
|
||||
@@ -254,9 +253,9 @@ float Node::getInlineEndBorder(FlexDirection axis, Direction direction) const {
|
||||
float Node::getFlexEndBorder(FlexDirection axis, Direction direction) const {
|
||||
const YGEdge trailRelativeFlexItemEdge = flexEndRelativeEdge(axis, direction);
|
||||
YGValue trailingBorder = isRow(axis)
|
||||
? computeEdgeValueForRow(
|
||||
style_.border(), trailRelativeFlexItemEdge, flexEndEdge(axis))
|
||||
: computeEdgeValueForColumn(style_.border(), flexEndEdge(axis));
|
||||
? computeEdgeValueForRow<&Style::border>(
|
||||
trailRelativeFlexItemEdge, flexEndEdge(axis))
|
||||
: computeEdgeValueForColumn<&Style::border>(flexEndEdge(axis));
|
||||
|
||||
return maxOrDefined(trailingBorder.value, 0.0f);
|
||||
}
|
||||
@@ -267,8 +266,8 @@ float Node::getInlineStartPadding(
|
||||
float widthSize) const {
|
||||
const YGEdge startEdge = getInlineStartEdgeUsingErrata(axis, direction);
|
||||
auto leadingPadding = isRow(axis)
|
||||
? computeEdgeValueForRow(style_.padding(), YGEdgeStart, startEdge)
|
||||
: computeEdgeValueForColumn(style_.padding(), startEdge);
|
||||
? computeEdgeValueForRow<&Style::padding>(YGEdgeStart, startEdge)
|
||||
: computeEdgeValueForColumn<&Style::padding>(startEdge);
|
||||
|
||||
return maxOrDefined(resolveValue(leadingPadding, widthSize).unwrap(), 0.0f);
|
||||
}
|
||||
@@ -280,9 +279,9 @@ float Node::getFlexStartPadding(
|
||||
const YGEdge leadRelativeFlexItemEdge =
|
||||
flexStartRelativeEdge(axis, direction);
|
||||
auto leadingPadding = isRow(axis)
|
||||
? computeEdgeValueForRow(
|
||||
style_.padding(), leadRelativeFlexItemEdge, flexStartEdge(axis))
|
||||
: computeEdgeValueForColumn(style_.padding(), flexStartEdge(axis));
|
||||
? computeEdgeValueForRow<&Style::padding>(
|
||||
leadRelativeFlexItemEdge, flexStartEdge(axis))
|
||||
: computeEdgeValueForColumn<&Style::padding>(flexStartEdge(axis));
|
||||
|
||||
return maxOrDefined(resolveValue(leadingPadding, widthSize).unwrap(), 0.0f);
|
||||
}
|
||||
@@ -293,8 +292,8 @@ float Node::getInlineEndPadding(
|
||||
float widthSize) const {
|
||||
const YGEdge endEdge = getInlineEndEdgeUsingErrata(axis, direction);
|
||||
auto trailingPadding = isRow(axis)
|
||||
? computeEdgeValueForRow(style_.padding(), YGEdgeEnd, endEdge)
|
||||
: computeEdgeValueForColumn(style_.padding(), endEdge);
|
||||
? computeEdgeValueForRow<&Style::padding>(YGEdgeEnd, endEdge)
|
||||
: computeEdgeValueForColumn<&Style::padding>(endEdge);
|
||||
|
||||
return maxOrDefined(resolveValue(trailingPadding, widthSize).unwrap(), 0.0f);
|
||||
}
|
||||
@@ -305,9 +304,9 @@ float Node::getFlexEndPadding(
|
||||
float widthSize) const {
|
||||
const YGEdge trailRelativeFlexItemEdge = flexEndRelativeEdge(axis, direction);
|
||||
auto trailingPadding = isRow(axis)
|
||||
? computeEdgeValueForRow(
|
||||
style_.padding(), trailRelativeFlexItemEdge, flexEndEdge(axis))
|
||||
: computeEdgeValueForColumn(style_.padding(), flexEndEdge(axis));
|
||||
? computeEdgeValueForRow<&Style::padding>(
|
||||
trailRelativeFlexItemEdge, flexEndEdge(axis))
|
||||
: computeEdgeValueForColumn<&Style::padding>(flexEndEdge(axis));
|
||||
|
||||
return maxOrDefined(resolveValue(trailingPadding, widthSize).unwrap(), 0.0f);
|
||||
}
|
||||
@@ -565,18 +564,18 @@ void Node::setPosition(
|
||||
}
|
||||
|
||||
YGValue Node::getFlexStartMarginValue(FlexDirection axis) const {
|
||||
if (isRow(axis) && style_.margin()[YGEdgeStart].isDefined()) {
|
||||
return style_.margin()[YGEdgeStart];
|
||||
if (isRow(axis) && style_.margin(YGEdgeStart).isDefined()) {
|
||||
return style_.margin(YGEdgeStart);
|
||||
} else {
|
||||
return style_.margin()[flexStartEdge(axis)];
|
||||
return style_.margin(flexStartEdge(axis));
|
||||
}
|
||||
}
|
||||
|
||||
YGValue Node::marginTrailingValue(FlexDirection axis) const {
|
||||
if (isRow(axis) && style_.margin()[YGEdgeEnd].isDefined()) {
|
||||
return style_.margin()[YGEdgeEnd];
|
||||
if (isRow(axis) && style_.margin(YGEdgeEnd).isDefined()) {
|
||||
return style_.margin(YGEdgeEnd);
|
||||
} else {
|
||||
return style_.margin()[flexEndEdge(axis)];
|
||||
return style_.margin(flexEndEdge(axis));
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user