diff --git a/benchmarks/CSSBenchmark.c b/benchmarks/CSSBenchmark.c index 839be338..d86e7fb6 100644 --- a/benchmarks/CSSBenchmark.c +++ b/benchmarks/CSSBenchmark.c @@ -9,8 +9,20 @@ #include "CSSBenchmark.h" +#include #include +// Measure functions can be quite slow, for example when measuring text. +// Simulate this by sleeping for 1 millisecond. +static CSSSize _measure(void *context, float width, CSSMeasureMode widthMode, float height, CSSMeasureMode heightMode) { + struct timespec sleeptime = {0, 1000000}; + nanosleep(&sleeptime, NULL); + return (CSSSize) { + .width = widthMode == CSSMeasureModeUndefined ? 10 : width, + .height = heightMode == CSSMeasureModeUndefined ? 10 : width, + }; +} + CSS_BENCHMARKS({ CSS_BENCHMARK("Stack with flex", { @@ -18,14 +30,47 @@ CSS_BENCHMARKS({ CSSNodeStyleSetWidth(root, 100); CSSNodeStyleSetHeight(root, 100); - for (uint32_t i = 0; i < 3; i++) { + for (uint32_t i = 0; i < 10; i++) { CSSNodeRef child = CSSNodeNew(); - CSSNodeStyleSetHeight(child, 20); + CSSNodeSetMeasureFunc(child, _measure); CSSNodeStyleSetFlex(child, 1); CSSNodeInsertChild(root, child, 0); } - CSSNodeCalculateLayout(root, 100, 100, CSSDirectionLTR); + CSSNodeCalculateLayout(root, CSSUndefined, CSSUndefined, CSSDirectionLTR); + }); + + CSS_BENCHMARK("Align stretch in undefined axis", { + CSSNodeRef root = CSSNodeNew(); + + for (uint32_t i = 0; i < 10; i++) { + CSSNodeRef child = CSSNodeNew(); + CSSNodeStyleSetHeight(child, 20); + CSSNodeSetMeasureFunc(child, _measure); + CSSNodeInsertChild(root, child, 0); + } + + CSSNodeCalculateLayout(root, CSSUndefined, CSSUndefined, CSSDirectionLTR); + }); + + CSS_BENCHMARK("Nested flex", { + CSSNodeRef root = CSSNodeNew(); + + for (uint32_t i = 0; i < 10; i++) { + CSSNodeRef child = CSSNodeNew(); + CSSNodeSetMeasureFunc(child, _measure); + CSSNodeStyleSetFlex(child, 1); + CSSNodeInsertChild(root, child, 0); + + for (uint32_t ii = 0; ii < 10; ii++) { + CSSNodeRef grandChild = CSSNodeNew(); + CSSNodeSetMeasureFunc(grandChild, _measure); + CSSNodeStyleSetFlex(grandChild, 1); + CSSNodeInsertChild(child, grandChild, 0); + } + } + + CSSNodeCalculateLayout(root, CSSUndefined, CSSUndefined, CSSDirectionLTR); }); }); diff --git a/benchmarks/CSSBenchmark.h b/benchmarks/CSSBenchmark.h index 7b5bc291..fdb0f5eb 100644 --- a/benchmarks/CSSBenchmark.h +++ b/benchmarks/CSSBenchmark.h @@ -14,7 +14,7 @@ #include #include -#define NUM_REPETITIONS 100000 +#define NUM_REPETITIONS 100 #define CSS_BENCHMARKS(BLOCK) \ int main(int argc, char const *argv[]) { \