Skip to content

Commit

Permalink
Support for (de)serializing config values
Browse files Browse the repository at this point in the history
Summary:
X-link: facebook/react-native#42750

tsia. This is state we need to capture as it can drastically affect the benchmark times

Differential Revision: D53203385

fbshipit-source-id: be6b6029322000eda2992e086ee2f3139834b904
  • Loading branch information
joevilches authored and facebook-github-bot committed Jan 31, 2024
1 parent 5514216 commit 21a8fef
Show file tree
Hide file tree
Showing 9 changed files with 140 additions and 41 deletions.
48 changes: 47 additions & 1 deletion benchmark/Benchmark.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,60 @@ YGEdge edgeFromString(std::string str) {
}
}

YGErrata errataFromString(std::string str) {
if (str == "none") {
return YGErrataNone;
} else if (str == "stretch-flex-basis") {
return YGErrataStretchFlexBasis;
} else if (str == "absolute-positioning-incorrect") {
return YGErrataAbsolutePositioningIncorrect;
} else if (str == "absolute-percent-against-inner-size") {
return YGErrataAbsolutePercentAgainstInnerSize;
} else if (str == "all") {
return YGErrataAll;
} else if (str == "classic") {
return YGErrataClassic;
} else {
throw std::invalid_argument(invalidArgumentMessage(str, "YGErrata"));
}
}

YGExperimentalFeature experimentalFeatureFromString(std::string str) {
if (str == "web-flex-basis") {
return YGExperimentalFeatureWebFlexBasis;
} else {
throw std::invalid_argument(
invalidArgumentMessage(str, "YGExperimentalFeature"));
}
}

std::string edgeStringFromPropertyName(
json::iterator it,
std::string propertyName) {
return it.key().substr(propertyName.length() + 1);
}

YGNodeRef buildTreeFromJson(json& j, YGNodeRef parent, size_t index) {
const YGNodeRef node = YGNodeNew();
json jsonConfig = j["config"];
YGConfigRef config = YGConfigNew();
for (json::iterator it = jsonConfig.begin(); it != jsonConfig.end(); it++) {
if (it.key() == "use-web-defaults") {
YGConfigSetUseWebDefaults(config, it.value());
} else if (it.key() == "point-scale-factor") {
YGConfigSetPointScaleFactor(config, it.value());
} else if (it.key() == "errata") {
YGConfigSetErrata(config, errataFromString(it.value()));
} else if (it.key() == "experimental-features") {
// Experimental features is serialized into an array where the values
// present indicate that that feature is enabled
for (json::iterator efIt = it.value().begin(); efIt != it.value().end();
efIt++) {
YGConfigSetExperimentalFeatureEnabled(
config, experimentalFeatureFromString(efIt.value()), true);
}
}
}
const YGNodeRef node = YGNodeNewWithConfig(config);
if (parent != nullptr) {
YGNodeInsertChild(parent, node, index);
}
Expand Down
5 changes: 4 additions & 1 deletion capture/CaptureTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ namespace facebook::yoga {

void captureTree(YGNodeRef node, std::filesystem::path& path) {
std::string str;
nodeToString(str, node, YGPrintOptionsStyle | YGPrintOptionsChildren);
nodeToString(
str,
node,
YGPrintOptionsStyle | YGPrintOptionsChildren | YGPrintOptionsConfig);
std::ofstream file(path);
file << str;
}
Expand Down
115 changes: 78 additions & 37 deletions capture/NodeToString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ static void appendFloatIfNotDefault(
float num,
float defaultNum) {
if (num != defaultNum && !YGFloatIsUndefined(num)) {
j["style"][key] = num;
j[key] = num;
}
}

Expand All @@ -29,13 +29,13 @@ static void appendYGValueIfNotDefault(
const YGValue& defaultValue) {
if (value != defaultValue) {
if (value.unit == YGUnitAuto) {
j["style"][key] = "auto";
j[key] = "auto";
} else if (value.unit == YGUnitUndefined) {
j["style"][key] = "undefined";
j[key] = "undefined";
} else {
std::string unit = value.unit == YGUnitPoint ? "px" : "%%";
j["style"][key]["value"] = value.value;
j["style"][key]["unit"] = unit;
j[key]["value"] = value.value;
j[key]["unit"] = unit;
}
}
}
Expand All @@ -46,7 +46,17 @@ static void appendEnumValueIfNotDefault(
std::string_view value,
std::string_view defaultValue) {
if (value != defaultValue) {
j["style"][key] = value;
j[key] = value;
}
}

static void appendBoolIfNotDefault(
json& j,
std::string_view key,
bool value,
bool defaultValue) {
if (value != defaultValue) {
j[key] = value;
}
}

Expand All @@ -57,47 +67,47 @@ static void appendEdges(
YGNodeRef node,
YGNodeRef defaultNode) {
appendYGValueIfNotDefault(
j,
j["style"],
key + "-left",
(*Field)(node, YGEdgeLeft),
(*Field)(defaultNode, YGEdgeLeft));
appendYGValueIfNotDefault(
j,
j["style"],
key + "-right",
(*Field)(node, YGEdgeRight),
(*Field)(defaultNode, YGEdgeRight));
appendYGValueIfNotDefault(
j,
j["style"],
key + "-top",
(*Field)(node, YGEdgeTop),
(*Field)(defaultNode, YGEdgeTop));
appendYGValueIfNotDefault(
j,
j["style"],
key + "-bottom",
(*Field)(node, YGEdgeBottom),
(*Field)(defaultNode, YGEdgeBottom));
appendYGValueIfNotDefault(
j,
j["style"],
key + "-all",
(*Field)(node, YGEdgeAll),
(*Field)(defaultNode, YGEdgeAll));
appendYGValueIfNotDefault(
j,
j["style"],
key + "-start",
(*Field)(node, YGEdgeStart),
(*Field)(defaultNode, YGEdgeStart));
appendYGValueIfNotDefault(
j,
j["style"],
key + "-end",
(*Field)(node, YGEdgeEnd),
(*Field)(defaultNode, YGEdgeEnd));
appendYGValueIfNotDefault(
j,
j["style"],
key + "-vertical",
(*Field)(node, YGEdgeVertical),
(*Field)(defaultNode, YGEdgeVertical));
appendYGValueIfNotDefault(
j,
j["style"],
key + "-horizontal",
(*Field)(node, YGEdgeHorizontal),
(*Field)(defaultNode, YGEdgeHorizontal));
Expand All @@ -120,65 +130,68 @@ static void nodeToStringImpl(json& j, YGNodeRef node, YGPrintOptions options) {
if ((options & YGPrintOptionsStyle) == YGPrintOptionsStyle) {
const YGNodeRef defaultNode = YGNodeNew();
appendEnumValueIfNotDefault(
j,
j["style"],
"flex-direction",
YGFlexDirectionToString(YGNodeStyleGetFlexDirection(node)),
YGFlexDirectionToString(YGNodeStyleGetFlexDirection(defaultNode)));
appendEnumValueIfNotDefault(
j,
j["style"],
"justify-content",
YGJustifyToString(YGNodeStyleGetJustifyContent(node)),
YGJustifyToString(YGNodeStyleGetJustifyContent(defaultNode)));
appendEnumValueIfNotDefault(
j,
j["style"],
"align-items",
YGAlignToString(YGNodeStyleGetAlignItems(node)),
YGAlignToString(YGNodeStyleGetAlignItems(defaultNode)));
appendEnumValueIfNotDefault(
j,
j["style"],
"align-content",
YGAlignToString(YGNodeStyleGetAlignContent(node)),
YGAlignToString(YGNodeStyleGetAlignContent(defaultNode)));
appendEnumValueIfNotDefault(
j,
j["style"],
"align-self",
YGAlignToString(YGNodeStyleGetAlignSelf(node)),
YGAlignToString(YGNodeStyleGetAlignSelf(defaultNode)));
appendEnumValueIfNotDefault(
j,
j["style"],
"flex-wrap",
YGWrapToString(YGNodeStyleGetFlexWrap(node)),
YGWrapToString(YGNodeStyleGetFlexWrap(defaultNode)));
appendEnumValueIfNotDefault(
j,
j["style"],
"overflow",
YGOverflowToString(YGNodeStyleGetOverflow(node)),
YGOverflowToString(YGNodeStyleGetOverflow(defaultNode)));
appendEnumValueIfNotDefault(
j,
j["style"],
"display",
YGDisplayToString(YGNodeStyleGetDisplay(node)),
YGDisplayToString(YGNodeStyleGetDisplay(defaultNode)));
appendEnumValueIfNotDefault(
j,
j["style"],
"position-type",
YGPositionTypeToString(YGNodeStyleGetPositionType(node)),
YGPositionTypeToString(YGNodeStyleGetPositionType(defaultNode)));

appendFloatIfNotDefault(
j,
j["style"],
"flex-grow",
YGNodeStyleGetFlexGrow(node),
YGNodeStyleGetFlexGrow(defaultNode));
appendFloatIfNotDefault(
j,
j["style"],
"flex-shrink",
YGNodeStyleGetFlexShrink(node),
YGNodeStyleGetFlexShrink(defaultNode));
appendFloatIfNotDefault(
j, "flex", YGNodeStyleGetFlex(node), YGNodeStyleGetFlex(defaultNode));
j["style"],
"flex",
YGNodeStyleGetFlex(node),
YGNodeStyleGetFlex(defaultNode));
appendYGValueIfNotDefault(
j,
j["style"],
"flex-basis",
YGNodeStyleGetFlexBasis(node),
YGNodeStyleGetFlexBasis(defaultNode));
Expand All @@ -189,48 +202,48 @@ static void nodeToStringImpl(json& j, YGNodeRef node, YGPrintOptions options) {
appendEdges<&YGNodeStyleGetPosition>(j, "position", node, defaultNode);

appendFloatIfNotDefault(
j,
j["style"],
"gap",
YGNodeStyleGetGap(node, YGGutterAll),
YGNodeStyleGetGap(defaultNode, YGGutterAll));
appendFloatIfNotDefault(
j,
j["style"],
"column-gap",
YGNodeStyleGetGap(node, YGGutterColumn),
YGNodeStyleGetGap(defaultNode, YGGutterColumn));
appendFloatIfNotDefault(
j,
j["style"],
"row-gap",
YGNodeStyleGetGap(node, YGGutterRow),
YGNodeStyleGetGap(defaultNode, YGGutterRow));

appendYGValueIfNotDefault(
j,
j["style"],
"width",
YGNodeStyleGetWidth(node),
YGNodeStyleGetWidth(defaultNode));
appendYGValueIfNotDefault(
j,
j["style"],
"height",
YGNodeStyleGetHeight(node),
YGNodeStyleGetHeight(defaultNode));
appendYGValueIfNotDefault(
j,
j["style"],
"max-width",
YGNodeStyleGetMaxWidth(node),
YGNodeStyleGetMaxWidth(defaultNode));
appendYGValueIfNotDefault(
j,
j["style"],
"max-height",
YGNodeStyleGetMaxHeight(node),
YGNodeStyleGetMaxHeight(defaultNode));
appendYGValueIfNotDefault(
j,
j["style"],
"min-width",
YGNodeStyleGetMinWidth(node),
YGNodeStyleGetMinWidth(defaultNode));
appendYGValueIfNotDefault(
j,
j["style"],
"min-height",
YGNodeStyleGetMinHeight(node),
YGNodeStyleGetMinHeight(defaultNode));
Expand All @@ -240,6 +253,34 @@ static void nodeToStringImpl(json& j, YGNodeRef node, YGPrintOptions options) {
}
}

if ((options & YGPrintOptionsConfig) == YGPrintOptionsConfig) {
YGConfigConstRef config = YGNodeGetConfig(node);
YGConfigConstRef defaultConfig = YGConfigGetDefault();

appendBoolIfNotDefault(
j["config"],
"use-web-defaults",
YGConfigGetUseWebDefaults(config),
YGConfigGetUseWebDefaults(defaultConfig));
appendFloatIfNotDefault(
j["config"],
"point-scale-factor",
YGConfigGetPointScaleFactor(config),
YGConfigGetPointScaleFactor(defaultConfig));
appendEnumValueIfNotDefault(
j["config"],
"errata",
YGErrataToString(YGConfigGetErrata(config)),
YGErrataToString(YGConfigGetErrata(defaultConfig)));
if (YGConfigIsExperimentalFeatureEnabled(
config, YGExperimentalFeatureWebFlexBasis) !=
YGConfigIsExperimentalFeatureEnabled(
defaultConfig, YGExperimentalFeatureWebFlexBasis)) {
j["config"]["experimental-features"].push_back(
YGExperimentalFeatureToString(YGExperimentalFeatureWebFlexBasis));
}
}

const size_t childCount = YGNodeGetChildCount(node);
if ((options & YGPrintOptionsChildren) == YGPrintOptionsChildren &&
childCount > 0) {
Expand Down
1 change: 1 addition & 0 deletions enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
("Layout", 1 << 0),
("Style", 1 << 1),
("Children", 1 << 2),
("Config", 1 << 3),
],
"Gutter": ["Column", "Row", "All"],
# Known incorrect behavior which can be enabled for compatibility
Expand Down
4 changes: 3 additions & 1 deletion java/com/facebook/yoga/YogaPrintOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
public enum YogaPrintOptions {
LAYOUT(1),
STYLE(2),
CHILDREN(4);
CHILDREN(4),
CONFIG(8);

private final int mIntValue;

Expand All @@ -29,6 +30,7 @@ public static YogaPrintOptions fromInt(int value) {
case 1: return LAYOUT;
case 2: return STYLE;
case 4: return CHILDREN;
case 8: return CONFIG;
default: throw new IllegalArgumentException("Unknown enum value: " + value);
}
}
Expand Down
2 changes: 2 additions & 0 deletions javascript/src/generated/YGEnums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export enum PrintOptions {
Layout = 1,
Style = 2,
Children = 4,
Config = 8,
}

export enum Unit {
Expand Down Expand Up @@ -199,6 +200,7 @@ const constants = {
PRINT_OPTIONS_LAYOUT: PrintOptions.Layout,
PRINT_OPTIONS_STYLE: PrintOptions.Style,
PRINT_OPTIONS_CHILDREN: PrintOptions.Children,
PRINT_OPTIONS_CONFIG: PrintOptions.Config,
UNIT_UNDEFINED: Unit.Undefined,
UNIT_POINT: Unit.Point,
UNIT_PERCENT: Unit.Percent,
Expand Down
Loading

0 comments on commit 21a8fef

Please sign in to comment.