Skip to content

Commit

Permalink
Only skip updating computed flex basis within the same generation
Browse files Browse the repository at this point in the history
Reviewed By: dshahidehpour

Differential Revision: D4207106

fbshipit-source-id: fc1063ca79ecf75f6101aadded53bca96cb0585d
  • Loading branch information
Emil Sjolander authored and Facebook Github Bot committed Nov 20, 2016
1 parent 1835dbe commit 15f848e
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 21 deletions.
16 changes: 10 additions & 6 deletions React/CSSLayout/CSSLayout.c
Expand Up @@ -49,6 +49,7 @@ typedef struct CSSLayout {
float dimensions[2];
CSSDirection direction;

uint32_t computedFlexBasisGeneration;
float computedFlexBasis;

// Instead of recomputing the entire layout every single time, we
Expand Down Expand Up @@ -967,7 +968,8 @@ static void computeChildFlexBasis(const CSSNodeRef node,

if (!CSSValueIsUndefined(CSSNodeStyleGetFlexBasis(child)) &&
!CSSValueIsUndefined(isMainAxisRow ? width : height)) {
if (CSSValueIsUndefined(child->layout.computedFlexBasis)) {
if (CSSValueIsUndefined(child->layout.computedFlexBasis) ||
child->layout.computedFlexBasisGeneration != gCurrentGenerationCount) {
child->layout.computedFlexBasis =
fmaxf(CSSNodeStyleGetFlexBasis(child), getPaddingAndBorderAxis(child, mainAxis));
}
Expand Down Expand Up @@ -1052,6 +1054,8 @@ static void computeChildFlexBasis(const CSSNodeRef node,
: child->layout.measuredDimensions[CSSDimensionHeight],
getPaddingAndBorderAxis(child, mainAxis));
}

child->layout.computedFlexBasisGeneration = gCurrentGenerationCount;
}

static void absoluteLayoutChild(const CSSNodeRef node,
Expand Down Expand Up @@ -1477,6 +1481,7 @@ static void layoutNodeImpl(const CSSNodeRef node,
child->nextChild = NULL;
} else {
if (child == singleFlexChild) {
child->layout.computedFlexBasisGeneration = gCurrentGenerationCount;
child->layout.computedFlexBasis = 0;
} else {
computeChildFlexBasis(node,
Expand Down Expand Up @@ -1813,8 +1818,8 @@ static void layoutNodeImpl(const CSSNodeRef node,
if (!CSSValueIsUndefined(node->style.minDimensions[dim[mainAxis]]) &&
node->style.minDimensions[dim[mainAxis]] >= 0) {
remainingFreeSpace = fmaxf(0,
node->style.minDimensions[dim[mainAxis]] -
(availableInnerMainDim - remainingFreeSpace));
node->style.minDimensions[dim[mainAxis]] -
(availableInnerMainDim - remainingFreeSpace));
} else {
remainingFreeSpace = 0;
}
Expand Down Expand Up @@ -2542,10 +2547,9 @@ void CSSLayoutSetMemoryFuncs(CSSMalloc cssMalloc,
CSSCalloc cssCalloc,
CSSRealloc cssRealloc,
CSSFree cssFree) {
CSS_ASSERT(gNodeInstanceCount == 0,
"Cannot set memory functions: all node must be freed first");
CSS_ASSERT(gNodeInstanceCount == 0, "Cannot set memory functions: all node must be freed first");
CSS_ASSERT((cssMalloc == NULL && cssCalloc == NULL && cssRealloc == NULL && cssFree == NULL) ||
(cssMalloc != NULL && cssCalloc != NULL && cssRealloc != NULL && cssFree != NULL),
(cssMalloc != NULL && cssCalloc != NULL && cssRealloc != NULL && cssFree != NULL),
"Cannot set memory functions: functions must be all NULL or Non-NULL");

if (cssMalloc == NULL || cssCalloc == NULL || cssRealloc == NULL || cssFree == NULL) {
Expand Down
5 changes: 3 additions & 2 deletions React/CSSLayout/CSSLayout.h
Expand Up @@ -28,8 +28,8 @@ static const unsigned long __nan[2] = {0xffffffff, 0x7fffffff};

#define CSSUndefined NAN

#include "CSSMacros.h"
#include "CSSEnums.h"
#include "CSSMacros.h"

CSS_EXTERN_C_BEGIN

Expand Down Expand Up @@ -160,7 +160,8 @@ CSS_NODE_LAYOUT_PROPERTY(CSSDirection, Direction);
WIN_EXPORT void CSSLayoutSetLogger(CSSLogger logger);
WIN_EXPORT void CSSLog(CSSLogLevel level, const char *message, ...);

WIN_EXPORT void CSSLayoutSetExperimentalFeatureEnabled(CSSExperimentalFeature feature, bool enabled);
WIN_EXPORT void CSSLayoutSetExperimentalFeatureEnabled(CSSExperimentalFeature feature,
bool enabled);
WIN_EXPORT bool CSSLayoutIsExperimentalFeatureEnabled(CSSExperimentalFeature feature);

WIN_EXPORT void CSSLayoutSetMemoryFuncs(CSSMalloc cssMalloc,
Expand Down
15 changes: 10 additions & 5 deletions ReactAndroid/src/main/jni/first-party/csslayoutjni/jni/CSSJNI.cpp
Expand Up @@ -77,12 +77,15 @@ static int _jniLog(CSSLogLevel level, const char *format, va_list args) {
char buffer[256];
int result = vsnprintf(buffer, sizeof(buffer), format, args);

static auto logFunc =
findClassLocal("com/facebook/csslayout/CSSLogger")->getMethod<void(local_ref<JCSSLogLevel>, jstring)>("log");
static auto logFunc = findClassLocal("com/facebook/csslayout/CSSLogger")
->getMethod<void(local_ref<JCSSLogLevel>, jstring)>("log");

static auto logLevelFromInt = JCSSLogLevel::javaClassStatic()->getStaticMethod<JCSSLogLevel::javaobject(jint)>("fromInt");
static auto logLevelFromInt =
JCSSLogLevel::javaClassStatic()->getStaticMethod<JCSSLogLevel::javaobject(jint)>("fromInt");

logFunc(jLogger->get(), logLevelFromInt(JCSSLogLevel::javaClassStatic(), static_cast<jint>(level)), Environment::current()->NewStringUTF(buffer));
logFunc(jLogger->get(),
logLevelFromInt(JCSSLogLevel::javaClassStatic(), static_cast<jint>(level)),
Environment::current()->NewStringUTF(buffer));

return result;
}
Expand Down Expand Up @@ -112,7 +115,9 @@ void jni_CSSLog(alias_ref<jclass> clazz, jint level, jstring message) {
Environment::current()->ReleaseStringUTFChars(message, nMessage);
}

void jni_CSSLayoutSetExperimentalFeatureEnabled(alias_ref<jclass> clazz, jint feature, jboolean enabled) {
void jni_CSSLayoutSetExperimentalFeatureEnabled(alias_ref<jclass> clazz,
jint feature,
jboolean enabled) {
CSSLayoutSetExperimentalFeatureEnabled(static_cast<CSSExperimentalFeature>(feature), enabled);
}

Expand Down
16 changes: 10 additions & 6 deletions ReactCommon/CSSLayout/CSSLayout/CSSLayout.c
Expand Up @@ -49,6 +49,7 @@ typedef struct CSSLayout {
float dimensions[2];
CSSDirection direction;

uint32_t computedFlexBasisGeneration;
float computedFlexBasis;

// Instead of recomputing the entire layout every single time, we
Expand Down Expand Up @@ -967,7 +968,8 @@ static void computeChildFlexBasis(const CSSNodeRef node,

if (!CSSValueIsUndefined(CSSNodeStyleGetFlexBasis(child)) &&
!CSSValueIsUndefined(isMainAxisRow ? width : height)) {
if (CSSValueIsUndefined(child->layout.computedFlexBasis)) {
if (CSSValueIsUndefined(child->layout.computedFlexBasis) ||
child->layout.computedFlexBasisGeneration != gCurrentGenerationCount) {
child->layout.computedFlexBasis =
fmaxf(CSSNodeStyleGetFlexBasis(child), getPaddingAndBorderAxis(child, mainAxis));
}
Expand Down Expand Up @@ -1052,6 +1054,8 @@ static void computeChildFlexBasis(const CSSNodeRef node,
: child->layout.measuredDimensions[CSSDimensionHeight],
getPaddingAndBorderAxis(child, mainAxis));
}

child->layout.computedFlexBasisGeneration = gCurrentGenerationCount;
}

static void absoluteLayoutChild(const CSSNodeRef node,
Expand Down Expand Up @@ -1477,6 +1481,7 @@ static void layoutNodeImpl(const CSSNodeRef node,
child->nextChild = NULL;
} else {
if (child == singleFlexChild) {
child->layout.computedFlexBasisGeneration = gCurrentGenerationCount;
child->layout.computedFlexBasis = 0;
} else {
computeChildFlexBasis(node,
Expand Down Expand Up @@ -1813,8 +1818,8 @@ static void layoutNodeImpl(const CSSNodeRef node,
if (!CSSValueIsUndefined(node->style.minDimensions[dim[mainAxis]]) &&
node->style.minDimensions[dim[mainAxis]] >= 0) {
remainingFreeSpace = fmaxf(0,
node->style.minDimensions[dim[mainAxis]] -
(availableInnerMainDim - remainingFreeSpace));
node->style.minDimensions[dim[mainAxis]] -
(availableInnerMainDim - remainingFreeSpace));
} else {
remainingFreeSpace = 0;
}
Expand Down Expand Up @@ -2542,10 +2547,9 @@ void CSSLayoutSetMemoryFuncs(CSSMalloc cssMalloc,
CSSCalloc cssCalloc,
CSSRealloc cssRealloc,
CSSFree cssFree) {
CSS_ASSERT(gNodeInstanceCount == 0,
"Cannot set memory functions: all node must be freed first");
CSS_ASSERT(gNodeInstanceCount == 0, "Cannot set memory functions: all node must be freed first");
CSS_ASSERT((cssMalloc == NULL && cssCalloc == NULL && cssRealloc == NULL && cssFree == NULL) ||
(cssMalloc != NULL && cssCalloc != NULL && cssRealloc != NULL && cssFree != NULL),
(cssMalloc != NULL && cssCalloc != NULL && cssRealloc != NULL && cssFree != NULL),
"Cannot set memory functions: functions must be all NULL or Non-NULL");

if (cssMalloc == NULL || cssCalloc == NULL || cssRealloc == NULL || cssFree == NULL) {
Expand Down
5 changes: 3 additions & 2 deletions ReactCommon/CSSLayout/CSSLayout/CSSLayout.h
Expand Up @@ -28,8 +28,8 @@ static const unsigned long __nan[2] = {0xffffffff, 0x7fffffff};

#define CSSUndefined NAN

#include "CSSMacros.h"
#include "CSSEnums.h"
#include "CSSMacros.h"

CSS_EXTERN_C_BEGIN

Expand Down Expand Up @@ -160,7 +160,8 @@ CSS_NODE_LAYOUT_PROPERTY(CSSDirection, Direction);
WIN_EXPORT void CSSLayoutSetLogger(CSSLogger logger);
WIN_EXPORT void CSSLog(CSSLogLevel level, const char *message, ...);

WIN_EXPORT void CSSLayoutSetExperimentalFeatureEnabled(CSSExperimentalFeature feature, bool enabled);
WIN_EXPORT void CSSLayoutSetExperimentalFeatureEnabled(CSSExperimentalFeature feature,
bool enabled);
WIN_EXPORT bool CSSLayoutIsExperimentalFeatureEnabled(CSSExperimentalFeature feature);

WIN_EXPORT void CSSLayoutSetMemoryFuncs(CSSMalloc cssMalloc,
Expand Down

0 comments on commit 15f848e

Please sign in to comment.