Identify and cache the results of expensive computations to avoid redundant calculations, especially in performance-critical code paths. This includes recursive functions, user-defined callbacks, and complex arithmetic operations that may be called multiple times with the same inputs.
Identify and cache the results of expensive computations to avoid redundant calculations, especially in performance-critical code paths. This includes recursive functions, user-defined callbacks, and complex arithmetic operations that may be called multiple times with the same inputs.
When reviewing code, look for:
Example of caching expensive baseline calculations:
// Before: Calling YGBaseline multiple times
if (alignItem == YGAlignBaseline) {
leadingCrossDim += collectedFlexItemsValues.maxBaselineAscent - YGBaseline(child);
}
// Later in code...
someOtherValue = YGBaseline(child); // Redundant call
// After: Cache the result in child layout
// At algorithm start: child->getLayout().cachedBaseline = YGBaseline(child);
if (alignItem == YGAlignBaseline) {
leadingCrossDim += collectedFlexItemsValues.maxBaselineAscent - child->getLayout().cachedBaseline;
}
This optimization is particularly important for functions that are recursive, call user-defined callbacks, or are executed in tight loops where performance matters most.
Enter the URL of a public GitHub repository