Skip to content

Commit

Permalink
Allow lazy resolution of edge dimension values
Browse files Browse the repository at this point in the history
Summary:
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]

Differential Revision: D50998164

fbshipit-source-id: 5f61c68b60782fadf7db4ae40010ae4dd2abc7a1
  • Loading branch information
NickGerleman authored and facebook-github-bot committed Nov 7, 2023
1 parent c712851 commit db0904c
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 392 deletions.
216 changes: 0 additions & 216 deletions tests/YGStyleAccessorsTest.cpp

This file was deleted.

35 changes: 17 additions & 18 deletions yoga/YGNodeStyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,65 +237,64 @@ YGValue YGNodeStyleGetFlexBasis(const YGNodeConstRef node) {

void YGNodeStyleSetPosition(YGNodeRef node, YGEdge edge, float points) {
auto value = CompactValue::ofMaybe<YGUnitPoint>(points);
updateIndexedStyleProp<MSVC_HINT(position)>(
node, &Style::position, edge, value);
updateIndexedStyleProp<&Style::position, &Style::setPosition>(
node, edge, value);
}

void YGNodeStyleSetPositionPercent(YGNodeRef node, YGEdge edge, float percent) {
auto value = CompactValue::ofMaybe<YGUnitPercent>(percent);
updateIndexedStyleProp<MSVC_HINT(position)>(
node, &Style::position, edge, value);
updateIndexedStyleProp<&Style::position, &Style::setPosition>(
node, edge, value);
}

YGValue YGNodeStyleGetPosition(YGNodeConstRef node, YGEdge edge) {
return resolveRef(node)->getStyle().position()[edge];
return resolveRef(node)->getStyle().position(edge);
}

void YGNodeStyleSetMargin(YGNodeRef node, YGEdge edge, float points) {
auto value = CompactValue::ofMaybe<YGUnitPoint>(points);
updateIndexedStyleProp<MSVC_HINT(margin)>(node, &Style::margin, edge, value);
updateIndexedStyleProp<&Style::margin, &Style::setMargin>(node, edge, value);
}

void YGNodeStyleSetMarginPercent(YGNodeRef node, YGEdge edge, float percent) {
auto value = CompactValue::ofMaybe<YGUnitPercent>(percent);
updateIndexedStyleProp<MSVC_HINT(margin)>(node, &Style::margin, edge, value);
updateIndexedStyleProp<&Style::margin, &Style::setMargin>(node, edge, value);
}

void YGNodeStyleSetMarginAuto(YGNodeRef node, YGEdge edge) {
updateIndexedStyleProp<MSVC_HINT(margin)>(
node, &Style::margin, edge, CompactValue::ofAuto());
updateIndexedStyleProp<&Style::margin, &Style::setMargin>(
node, edge, CompactValue::ofAuto());
}

YGValue YGNodeStyleGetMargin(YGNodeConstRef node, YGEdge edge) {
return resolveRef(node)->getStyle().margin()[edge];
return resolveRef(node)->getStyle().margin(edge);
}

void YGNodeStyleSetPadding(YGNodeRef node, YGEdge edge, float points) {
auto value = CompactValue::ofMaybe<YGUnitPoint>(points);
updateIndexedStyleProp<MSVC_HINT(padding)>(
node, &Style::padding, edge, value);
updateIndexedStyleProp<&Style::padding, &Style::setPadding>(
node, edge, value);
}

void YGNodeStyleSetPaddingPercent(YGNodeRef node, YGEdge edge, float percent) {
auto value = CompactValue::ofMaybe<YGUnitPercent>(percent);
updateIndexedStyleProp<MSVC_HINT(padding)>(
node, &Style::padding, edge, value);
updateIndexedStyleProp<&Style::padding, &Style::setPadding>(
node, edge, value);
}

YGValue YGNodeStyleGetPadding(YGNodeConstRef node, YGEdge edge) {
return resolveRef(node)->getStyle().padding()[edge];
return resolveRef(node)->getStyle().padding(edge);
}

void YGNodeStyleSetBorder(
const YGNodeRef node,
const YGEdge edge,
const float border) {
auto value = CompactValue::ofMaybe<YGUnitPoint>(border);
updateIndexedStyleProp<MSVC_HINT(border)>(node, &Style::border, edge, value);
updateIndexedStyleProp<&Style::border, &Style::setBorder>(node, edge, value);
}

float YGNodeStyleGetBorder(const YGNodeConstRef node, const YGEdge edge) {
auto border = resolveRef(node)->getStyle().border()[edge];
auto border = resolveRef(node)->getStyle().border(edge);
if (border.isUndefined() || border.isAuto()) {
return YGUndefined;
}
Expand Down
1 change: 1 addition & 0 deletions yoga/algorithm/FlexDirection.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

#include <yoga/debug/AssertFatal.h>
#include <yoga/enums/Dimension.h>
#include <yoga/enums/Direction.h>
#include <yoga/enums/FlexDirection.h>

namespace facebook::yoga {
Expand Down
2 changes: 2 additions & 0 deletions yoga/debug/AssertFatal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

#include <stdexcept>

#include <yoga/config/Config.h>
#include <yoga/debug/AssertFatal.h>
#include <yoga/debug/Log.h>
#include <yoga/node/Node.h>

namespace facebook::yoga {

Expand Down
5 changes: 3 additions & 2 deletions yoga/debug/AssertFatal.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
#pragma once

#include <yoga/Yoga.h>
#include <yoga/config/Config.h>
#include <yoga/node/Node.h>

namespace facebook::yoga {

class Node;
class Config;

[[noreturn]] void fatalWithMessage(const char* message);

void assertFatal(bool condition, const char* message);
Expand Down
Loading

0 comments on commit db0904c

Please sign in to comment.