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

128
yoga/style/StyleLength.h Normal file
View File

@@ -0,0 +1,128 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#pragma once
#include <yoga/enums/Unit.h>
#include <yoga/numeric/FloatOptional.h>
namespace facebook::yoga {
/**
* Style::Length represents a CSS Value which may be one of:
* 1. Undefined
* 2. A keyword (e.g. auto)
* 3. A CSS <length-percentage> value:
* a. <length> value (e.g. 10px)
* b. <percentage> value of a reference <length>
* 4. (soon) A math function which returns a <length-percentage> value
*
* References:
* 1. https://www.w3.org/TR/css-values-4/#lengths
* 2. https://www.w3.org/TR/css-values-4/#percentage-value
* 3. https://www.w3.org/TR/css-values-4/#mixed-percentages
* 4. https://www.w3.org/TR/css-values-4/#math
*/
class StyleLength {
public:
constexpr StyleLength() = default;
constexpr static StyleLength points(float value) {
return yoga::isUndefined(value) || yoga::isinf(value)
? undefined()
: StyleLength{FloatOptional{value}, Unit::Point};
}
constexpr static StyleLength percent(float value) {
return yoga::isUndefined(value) || yoga::isinf(value)
? undefined()
: StyleLength{FloatOptional{value}, Unit::Percent};
}
constexpr static StyleLength ofAuto() {
return StyleLength{{}, Unit::Auto};
}
constexpr static StyleLength undefined() {
return StyleLength{{}, Unit::Undefined};
}
constexpr bool isAuto() const {
return unit_ == Unit::Auto;
}
constexpr bool isUndefined() const {
return unit_ == Unit::Undefined;
}
constexpr bool isDefined() const {
return !isUndefined();
}
constexpr FloatOptional value() const {
return value_;
}
constexpr Unit unit() const {
return unit_;
}
explicit constexpr operator YGValue() const {
return YGValue{value_.unwrap(), unscopedEnum(unit_)};
}
constexpr bool operator==(const StyleLength& rhs) const {
return value_ == rhs.value_ && unit_ == rhs.unit_;
}
private:
// We intentionally do not allow direct construction using value and unit, to
// avoid invalid, or redundant combinations.
constexpr StyleLength(FloatOptional value, Unit unit)
: value_(value), unit_(unit) {}
FloatOptional value_{};
Unit unit_{Unit::Undefined};
};
inline bool inexactEquals(const StyleLength& a, const StyleLength& b) {
return a.unit() == b.unit() && inexactEquals(a.value(), b.value());
}
namespace value {
/**
* Canonical unit (one YGUnitPoint)
*/
constexpr StyleLength points(float value) {
return StyleLength::points(value);
}
/**
* Percent of reference
*/
constexpr StyleLength percent(float value) {
return StyleLength::percent(value);
}
/**
* "auto" keyword
*/
constexpr StyleLength ofAuto() {
return StyleLength::ofAuto();
}
/**
* Undefined
*/
constexpr StyleLength undefined() {
return StyleLength::undefined();
}
} // namespace value
} // namespace facebook::yoga