Make CompactValue internal detail of yoga::Style (#1492)

Summary:
X-link: https://github.com/facebook/react-native/pull/41776
Pull Request resolved: https://github.com/facebook/yoga/pull/1492

# Summary

In preparation to replace `CompactValue`, this fully encapsulates it as an implementation detail of `yoga::Style`.

The internal API now always operates on `Style::Length`, converted to `YGValue` at the public API boundary.

In the next step, we can plug in a new representation within `Style`, which should enable 64 bit values, and lower memory usage.

# Test Plan

1. Existing tests (inc for style, invalidation, CompactValue) pass
2. Check that constexpr `yoga::isinf()` produces same assembly under Clang as `std::isinf()`
3. Fabric Android builds
4. Yoga benchmark does style reads

# Performance

Checking whether a style is defined, then reading after, is a hot path, and we are doubling any space style lengths take in the stack (but not long-term on the node). After a naive move, on one system, the Yoga benchmark creating, laying out, and destroying a tree, ran about 8-10%  slower in the "Huge nested flex" example. We are converting in many more cases instead of doing undefined check, but operating on accessed style values no longer needs to do the conversion multiple times.

I changed the `CompactValue` conversion to YGValue/StyleLength path to check for undefined as the common case (since we always convert, instead of calling `isUndefined` directly on CompactValue. That seemed to get the difference down to ~5-6% when I was playing with it then. We can optimistically make some of this up with ValuePool giving better locality, and fix this more holistically if we reduce edge and value resolution.

On another machine where I tested this, the new revision went the opposite direction, and was about 5% faster, so this isn't really a cut and dry regression, but we see different characteristics than before.

# Changelog
[Internal]

Reviewed By: rozele

Differential Revision: D51775346

fbshipit-source-id: c618af41b4882b4a227c917fcad07375806faf78
This commit is contained in:
Nick Gerleman
2023-12-19 13:38:40 -08:00
committed by Facebook GitHub Bot
parent 2caa8ac8cb
commit 192016a0a8
12 changed files with 468 additions and 464 deletions

View File

@@ -687,9 +687,9 @@ static float distributeFreeSpaceSecondPass(
sizingModeCrossDim == SizingMode::StretchFit &&
!(isNodeFlexWrap && mainAxisOverflows) &&
resolveChildAlignment(node, currentLineChild) == Align::Stretch &&
currentLineChild->getFlexStartMarginValue(crossAxis).unit !=
YGUnitAuto &&
currentLineChild->marginTrailingValue(crossAxis).unit != YGUnitAuto) {
currentLineChild->getFlexStartMarginValue(crossAxis).unit() !=
Unit::Auto &&
currentLineChild->marginTrailingValue(crossAxis).unit() != Unit::Auto) {
childCrossSize = availableInnerCrossDim;
childCrossSizingMode = SizingMode::StretchFit;
} else if (!currentLineChild->styleDefinesDimension(
@@ -706,8 +706,8 @@ static float distributeFreeSpaceSecondPass(
.unwrap() +
marginCross;
const bool isLoosePercentageMeasurement =
currentLineChild->getResolvedDimension(dimension(crossAxis)).unit ==
YGUnitPercent &&
currentLineChild->getResolvedDimension(dimension(crossAxis)).unit() ==
Unit::Percent &&
sizingModeCrossDim != SizingMode::StretchFit;
childCrossSizingMode =
yoga::isUndefined(childCrossSize) || isLoosePercentageMeasurement
@@ -733,9 +733,9 @@ static float distributeFreeSpaceSecondPass(
const bool requiresStretchLayout = !currentLineChild->styleDefinesDimension(
crossAxis, availableInnerCrossDim) &&
resolveChildAlignment(node, currentLineChild) == Align::Stretch &&
currentLineChild->getFlexStartMarginValue(crossAxis).unit !=
YGUnitAuto &&
currentLineChild->marginTrailingValue(crossAxis).unit != YGUnitAuto;
currentLineChild->getFlexStartMarginValue(crossAxis).unit() !=
Unit::Auto &&
currentLineChild->marginTrailingValue(crossAxis).unit() != Unit::Auto;
const float childWidth = isMainAxisRow ? childMainSize : childCrossSize;
const float childHeight = !isMainAxisRow ? childMainSize : childCrossSize;
@@ -982,10 +982,10 @@ static void justifyMainAxis(
for (size_t i = startOfLineIndex; i < flexLine.endOfLineIndex; i++) {
auto child = node->getChild(i);
if (child->getStyle().positionType() != PositionType::Absolute) {
if (child->getFlexStartMarginValue(mainAxis).unit == YGUnitAuto) {
if (child->getFlexStartMarginValue(mainAxis).unit() == Unit::Auto) {
numberOfAutoMarginsOnCurrentLine++;
}
if (child->marginTrailingValue(mainAxis).unit == YGUnitAuto) {
if (child->marginTrailingValue(mainAxis).unit() == Unit::Auto) {
numberOfAutoMarginsOnCurrentLine++;
}
}
@@ -1062,7 +1062,7 @@ static void justifyMainAxis(
// We need to do that only for relative elements. Absolute elements do not
// take part in that phase.
if (childStyle.positionType() != PositionType::Absolute) {
if (child->getFlexStartMarginValue(mainAxis).unit == YGUnitAuto) {
if (child->getFlexStartMarginValue(mainAxis).unit() == Unit::Auto) {
flexLine.layout.mainDim += flexLine.layout.remainingFreeSpace /
static_cast<float>(numberOfAutoMarginsOnCurrentLine);
}
@@ -1078,7 +1078,7 @@ static void justifyMainAxis(
flexLine.layout.mainDim += betweenMainDim;
}
if (child->marginTrailingValue(mainAxis).unit == YGUnitAuto) {
if (child->marginTrailingValue(mainAxis).unit() == Unit::Auto) {
flexLine.layout.mainDim += flexLine.layout.remainingFreeSpace /
static_cast<float>(numberOfAutoMarginsOnCurrentLine);
}
@@ -1630,8 +1630,8 @@ static void calculateLayoutImpl(
// time, this time forcing the cross-axis size to be the computed
// cross size for the current line.
if (alignItem == Align::Stretch &&
child->getFlexStartMarginValue(crossAxis).unit != YGUnitAuto &&
child->marginTrailingValue(crossAxis).unit != YGUnitAuto) {
child->getFlexStartMarginValue(crossAxis).unit() != Unit::Auto &&
child->marginTrailingValue(crossAxis).unit() != Unit::Auto) {
// If the child defines a definite size for its cross axis, there's
// no need to stretch.
if (!child->styleDefinesDimension(
@@ -1704,15 +1704,17 @@ static void calculateLayoutImpl(
const float remainingCrossDim = containerCrossAxis -
child->dimensionWithMargin(crossAxis, availableInnerWidth);
if (child->getFlexStartMarginValue(crossAxis).unit == YGUnitAuto &&
child->marginTrailingValue(crossAxis).unit == YGUnitAuto) {
if (child->getFlexStartMarginValue(crossAxis).unit() ==
Unit::Auto &&
child->marginTrailingValue(crossAxis).unit() == Unit::Auto) {
leadingCrossDim +=
yoga::maxOrDefined(0.0f, remainingCrossDim / 2);
} else if (
child->marginTrailingValue(crossAxis).unit == YGUnitAuto) {
child->marginTrailingValue(crossAxis).unit() == Unit::Auto) {
// No-Op
} else if (
child->getFlexStartMarginValue(crossAxis).unit == YGUnitAuto) {
child->getFlexStartMarginValue(crossAxis).unit() ==
Unit::Auto) {
leadingCrossDim += yoga::maxOrDefined(0.0f, remainingCrossDim);
} else if (alignItem == Align::FlexStart) {
// No-Op

View File

@@ -14,19 +14,15 @@
namespace facebook::yoga {
inline FloatOptional resolveValue(const YGValue value, const float ownerSize) {
switch (value.unit) {
case YGUnitPoint:
return FloatOptional{value.value};
case YGUnitPercent:
return FloatOptional{value.value * ownerSize * 0.01f};
inline FloatOptional resolveValue(Style::Length length, float ownerSize) {
switch (length.unit()) {
case Unit::Point:
return length.value();
case Unit::Percent:
return FloatOptional{length.value().unwrap() * ownerSize * 0.01f};
default:
return FloatOptional{};
}
}
inline FloatOptional resolveValue(Style::Length value, float ownerSize) {
return resolveValue((YGValue)value, ownerSize);
}
} // namespace facebook::yoga