Skip to content

Commit

Permalink
Add config version, and invalidate layout on config change
Browse files Browse the repository at this point in the history
Summary:
This is a continuation of the previous PR: facebook#45047

I made the change more generic for allowing any kind of config change to invalidate layout.

X-link: facebook/yoga#1674

Differential Revision: D59286992

Pulled By: NickGerleman
  • Loading branch information
acoates-ms authored and facebook-github-bot committed Jul 2, 2024
1 parent 3fc7ebb commit 3bd7da8
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2140,6 +2140,7 @@ bool calculateLayoutInternal(

const bool needToVisitNode =
(node->isDirty() && layout->generationCount != generationCount) ||
layout->configVersion != node->getConfig()->getVersion() ||
layout->lastOwnerDirection != ownerDirection;

if (needToVisitNode) {
Expand Down Expand Up @@ -2255,6 +2256,7 @@ bool calculateLayoutInternal(
reason);

layout->lastOwnerDirection = ownerDirection;
layout->configVersion = node->getConfig()->getVersion();

if (cachedResults == nullptr) {
layoutMarkerData.maxMeasureCache = std::max(
Expand Down
29 changes: 24 additions & 5 deletions packages/react-native/ReactCommon/yoga/yoga/config/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ bool Config::useWebDefaults() const {
void Config::setExperimentalFeatureEnabled(
ExperimentalFeature feature,
bool enabled) {
experimentalFeatures_.set(static_cast<size_t>(feature), enabled);
if (isExperimentalFeatureEnabled(feature) != enabled) {
experimentalFeatures_.set(static_cast<size_t>(feature), enabled);
version_++;
}
}

bool Config::isExperimentalFeatureEnabled(ExperimentalFeature feature) const {
Expand All @@ -43,15 +46,24 @@ ExperimentalFeatureSet Config::getEnabledExperiments() const {
}

void Config::setErrata(Errata errata) {
errata_ = errata;
if (errata_ != errata) {
errata_ = errata;
version_++;
}
}

void Config::addErrata(Errata errata) {
errata_ |= errata;
if (!hasErrata(errata)) {
errata_ |= errata;
version_++;
}
}

void Config::removeErrata(Errata errata) {
errata_ &= (~errata);
if (hasErrata(errata)) {
errata_ &= (~errata);
version_++;
}
}

Errata Config::getErrata() const {
Expand All @@ -63,7 +75,10 @@ bool Config::hasErrata(Errata errata) const {
}

void Config::setPointScaleFactor(float pointScaleFactor) {
pointScaleFactor_ = pointScaleFactor;
if (pointScaleFactor_ != pointScaleFactor) {
pointScaleFactor_ = pointScaleFactor;
version_++;
}
}

float Config::getPointScaleFactor() const {
Expand All @@ -78,6 +93,10 @@ void* Config::getContext() const {
return context_;
}

uint32_t Config::getVersion() const noexcept {
return version_;
}

void Config::setLogger(YGLogger logger) {
logger_ = logger;
}
Expand Down
3 changes: 3 additions & 0 deletions packages/react-native/ReactCommon/yoga/yoga/config/Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class YG_EXPORT Config : public ::YGConfig {
void setContext(void* context);
void* getContext() const;

uint32_t getVersion() const noexcept;

void setLogger(YGLogger logger);
void log(
const yoga::Node* node,
Expand All @@ -72,6 +74,7 @@ class YG_EXPORT Config : public ::YGConfig {

bool useWebDefaults_ : 1 = false;

uint32_t version_ = 0;
ExperimentalFeatureSet experimentalFeatures_{};
Errata errata_ = Errata::None;
float pointScaleFactor_ = 1.0f;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ bool LayoutResults::operator==(LayoutResults layout) const {
direction() == layout.direction() &&
hadOverflow() == layout.hadOverflow() &&
lastOwnerDirection == layout.lastOwnerDirection &&
configVersion == layout.configVersion &&
nextCachedMeasurementsIndex == layout.nextCachedMeasurementsIndex &&
cachedLayout == layout.cachedLayout &&
computedFlexBasis == layout.computedFlexBasis;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ struct LayoutResults {
// Instead of recomputing the entire layout every single time, we cache some
// information to break early when nothing changed
uint32_t generationCount = 0;
uint32_t configVersion = 0;
Direction lastOwnerDirection = Direction::Inherit;

uint32_t nextCachedMeasurementsIndex = 0;
Expand Down
5 changes: 5 additions & 0 deletions packages/react-native/ReactCommon/yoga/yoga/node/Node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ void Node::setConfig(yoga::Config* config) {

if (yoga::configUpdateInvalidatesLayout(*config_, *config)) {
markDirtyAndPropagate();
layout_.configVersion = 0;
} else {
// If the config is functionally the same, then align the configVersion so
// that we can reuse the layout cache
layout_.configVersion = config->getVersion();
}

config_ = config;
Expand Down

0 comments on commit 3bd7da8

Please sign in to comment.