Skip to content

Commit 3b5bea0

Browse files
NickGerlemanfacebook-github-bot
authored andcommitted
Simplify bitfields (#39485)
Summary: Pull Request resolved: #39485 X-link: facebook/yoga#1393 These were previously packed into structs to allow zero initializing all the flags at once without needing a ctor. C++ 20 has in-class member initializer support for bitfields, which makes these look more like normal member variables. Setting enum values is a bit jank right now, due to relying on C enums which are effectively int32_t, along with GCC `-Wconversion` being a bit aggressive in needing to explicitly mask. I have some ideas to fix this later (e.g. using scoped enums internally). bypass-github-export-checks Changelog: [General][Breaking] - Require C++ 20 when including renderer headers Reviewed By: sammy-SC Differential Revision: D49265967 fbshipit-source-id: 6ab935a866196df06e742c821f3af88eb4d18e1a
1 parent 3275b60 commit 3b5bea0

File tree

5 files changed

+32
-51
lines changed

5 files changed

+32
-51
lines changed

packages/react-native/ReactCommon/yoga/yoga/config/Config.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,19 @@ Config::Config(YGLogger logger) : cloneNodeCallback_{nullptr} {
2525
}
2626

2727
void Config::setUseWebDefaults(bool useWebDefaults) {
28-
flags_.useWebDefaults = useWebDefaults;
28+
useWebDefaults_ = useWebDefaults;
2929
}
3030

3131
bool Config::useWebDefaults() const {
32-
return flags_.useWebDefaults;
32+
return useWebDefaults_;
3333
}
3434

3535
void Config::setShouldPrintTree(bool printTree) {
36-
flags_.printTree = printTree;
36+
printTree_ = printTree;
3737
}
3838

3939
bool Config::shouldPrintTree() const {
40-
return flags_.printTree;
40+
return printTree_;
4141
}
4242

4343
void Config::setExperimentalFeatureEnabled(

packages/react-native/ReactCommon/yoga/yoga/config/Config.h

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,6 @@ bool configUpdateInvalidatesLayout(
3030
const Config& oldConfig,
3131
const Config& newConfig);
3232

33-
#pragma pack(push)
34-
#pragma pack(1)
35-
// Packed structure of <32-bit options to miminize size per node.
36-
struct ConfigFlags {
37-
bool useWebDefaults : 1;
38-
bool printTree : 1;
39-
};
40-
#pragma pack(pop)
41-
4233
class YG_EXPORT Config : public ::YGConfig {
4334
public:
4435
Config(YGLogger logger);
@@ -82,7 +73,9 @@ class YG_EXPORT Config : public ::YGConfig {
8273
YGCloneNodeFunc cloneNodeCallback_;
8374
YGLogger logger_;
8475

85-
ConfigFlags flags_{};
76+
bool useWebDefaults_ : 1 = false;
77+
bool printTree_ : 1 = false;
78+
8679
ExperimentalFeatureSet experimentalFeatures_{};
8780
Errata errata_ = Errata::None;
8881
float pointScaleFactor_ = 1.0f;

packages/react-native/ReactCommon/yoga/yoga/node/LayoutResults.h

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,6 @@
1515

1616
namespace facebook::yoga {
1717

18-
#pragma pack(push)
19-
#pragma pack(1)
20-
struct LayoutResultFlags {
21-
uint32_t direction : 2;
22-
bool hadOverflow : 1;
23-
};
24-
#pragma pack(pop)
25-
2618
struct LayoutResults {
2719
// This value was chosen based on empirical data:
2820
// 98% of analyzed layouts require less than 8 entries.
@@ -35,7 +27,8 @@ struct LayoutResults {
3527
std::array<float, 4> padding = {};
3628

3729
private:
38-
LayoutResultFlags flags_{};
30+
uint32_t direction_ : 2 = static_cast<uint32_t>(YGDirectionInherit) & 0x03;
31+
bool hadOverflow_ : 1 = false;
3932

4033
public:
4134
uint32_t computedFlexBasisGeneration = 0;
@@ -53,18 +46,18 @@ struct LayoutResults {
5346
CachedMeasurement cachedLayout{};
5447

5548
YGDirection direction() const {
56-
return static_cast<YGDirection>(flags_.direction);
49+
return static_cast<YGDirection>(direction_);
5750
}
5851

5952
void setDirection(YGDirection direction) {
60-
flags_.direction = static_cast<uint32_t>(direction) & 0x03;
53+
direction_ = static_cast<uint32_t>(direction) & 0x03;
6154
}
6255

6356
bool hadOverflow() const {
64-
return flags_.hadOverflow;
57+
return hadOverflow_;
6558
}
6659
void setHadOverflow(bool hadOverflow) {
67-
flags_.hadOverflow = hadOverflow;
60+
hadOverflow_ = hadOverflow;
6861
}
6962

7063
bool operator==(LayoutResults layout) const;

packages/react-native/ReactCommon/yoga/yoga/node/Node.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,17 @@ Node::Node(const yoga::Config* config) : config_{config} {
2323
yoga::assertFatal(
2424
config != nullptr, "Attempting to construct Node with null config");
2525

26-
flags_.hasNewLayout = true;
2726
if (config->useWebDefaults()) {
2827
useWebDefaults();
2928
}
3029
}
3130

3231
Node::Node(Node&& node) {
32+
hasNewLayout_ = node.hasNewLayout_;
33+
isReferenceBaseline_ = node.isReferenceBaseline_;
34+
isDirty_ = node.isDirty_;
35+
nodeType_ = node.nodeType_;
3336
context_ = node.context_;
34-
flags_ = node.flags_;
3537
measureFunc_ = node.measureFunc_;
3638
baselineFunc_ = node.baselineFunc_;
3739
printFunc_ = node.printFunc_;
@@ -271,10 +273,10 @@ void Node::setConfig(yoga::Config* config) {
271273
}
272274

273275
void Node::setDirty(bool isDirty) {
274-
if (isDirty == flags_.isDirty) {
276+
if (isDirty == isDirty_) {
275277
return;
276278
}
277-
flags_.isDirty = isDirty;
279+
isDirty_ = isDirty;
278280
if (isDirty && dirtiedFunc_) {
279281
dirtiedFunc_(this);
280282
}
@@ -473,7 +475,7 @@ void Node::cloneChildrenIfNeeded() {
473475
}
474476

475477
void Node::markDirtyAndPropagate() {
476-
if (!flags_.isDirty) {
478+
if (!isDirty_) {
477479
setDirty(true);
478480
setLayoutComputedFlexBasis(FloatOptional());
479481
if (owner_) {
@@ -483,7 +485,7 @@ void Node::markDirtyAndPropagate() {
483485
}
484486

485487
void Node::markDirtyAndPropagateDownwards() {
486-
flags_.isDirty = true;
488+
isDirty_ = true;
487489
for_each(children_.begin(), children_.end(), [](Node* childNode) {
488490
childNode->markDirtyAndPropagateDownwards();
489491
});

packages/react-native/ReactCommon/yoga/yoga/node/Node.h

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,13 @@ struct YGNode {};
2626

2727
namespace facebook::yoga {
2828

29-
#pragma pack(push)
30-
#pragma pack(1)
31-
struct NodeFlags {
32-
bool hasNewLayout : 1;
33-
bool isReferenceBaseline : 1;
34-
bool isDirty : 1;
35-
NodeType nodeType : bitCount<NodeType>();
36-
};
37-
#pragma pack(pop)
38-
3929
class YG_EXPORT Node : public ::YGNode {
4030
private:
31+
bool hasNewLayout_ : 1 = true;
32+
bool isReferenceBaseline_ : 1 = false;
33+
bool isDirty_ : 1 = false;
34+
NodeType nodeType_ : bitCount<NodeType>() = NodeType::Default;
4135
void* context_ = nullptr;
42-
NodeFlags flags_ = {};
4336
YGMeasureFunc measureFunc_ = {nullptr};
4437
YGBaselineFunc baselineFunc_ = {nullptr};
4538
YGPrintFunc printFunc_ = {nullptr};
@@ -92,11 +85,11 @@ class YG_EXPORT Node : public ::YGNode {
9285
void print();
9386

9487
bool getHasNewLayout() const {
95-
return flags_.hasNewLayout;
88+
return hasNewLayout_;
9689
}
9790

9891
NodeType getNodeType() const {
99-
return flags_.nodeType;
92+
return nodeType_;
10093
}
10194

10295
bool hasMeasureFunc() const noexcept {
@@ -142,7 +135,7 @@ class YG_EXPORT Node : public ::YGNode {
142135
}
143136

144137
bool isReferenceBaseline() const {
145-
return flags_.isReferenceBaseline;
138+
return isReferenceBaseline_;
146139
}
147140

148141
// returns the Node that owns this Node. An owner is used to identify
@@ -175,7 +168,7 @@ class YG_EXPORT Node : public ::YGNode {
175168
}
176169

177170
bool isDirty() const {
178-
return flags_.isDirty;
171+
return isDirty_;
179172
}
180173

181174
std::array<YGValue, 2> getResolvedDimensions() const {
@@ -250,11 +243,11 @@ class YG_EXPORT Node : public ::YGNode {
250243
}
251244

252245
void setHasNewLayout(bool hasNewLayout) {
253-
flags_.hasNewLayout = hasNewLayout;
246+
hasNewLayout_ = hasNewLayout;
254247
}
255248

256249
void setNodeType(NodeType nodeType) {
257-
flags_.nodeType = nodeType;
250+
nodeType_ = nodeType;
258251
}
259252

260253
void setMeasureFunc(YGMeasureFunc measureFunc);
@@ -280,7 +273,7 @@ class YG_EXPORT Node : public ::YGNode {
280273
}
281274

282275
void setIsReferenceBaseline(bool isReferenceBaseline) {
283-
flags_.isReferenceBaseline = isReferenceBaseline;
276+
isReferenceBaseline_ = isReferenceBaseline;
284277
}
285278

286279
void setOwner(Node* owner) {

0 commit comments

Comments
 (0)