Identify and eliminate repeated expensive computations, especially in loops and frequently executed code paths. This optimization principle focuses on moving invariant calculations outside loops, caching results of expensive operations, and avoiding unnecessary data conversions or memory allocations.
Identify and eliminate repeated expensive computations, especially in loops and frequently executed code paths. This optimization principle focuses on moving invariant calculations outside loops, caching results of expensive operations, and avoiding unnecessary data conversions or memory allocations.
Key strategies include:
// Instead of this:
for (idx_t child_idx = 0; child_idx < entry.length; child_idx++) {
if (actual_value == ((T *)value_data.data)[0]) { // Repeated casting
// ...
}
}
// Do this:
auto values = (T *)value_data.data; // Cast once outside loop
for (idx_t child_idx = 0; child_idx < entry.length; child_idx++) {
if (actual_value == values[0]) {
// ...
}
}
Cache expensive expression evaluations: If an expression can be folded or has IsFoldable()
, cache the result to avoid repeated evaluation in performance-critical paths.
Avoid unnecessary string conversions and copies: Use direct data access methods like StringUtil::CIEquals
on string_t
data instead of creating temporary string copies for comparisons.
Optimize early exit conditions: Move null checks and other early termination conditions to the beginning of loops to avoid wasting computation on data that will be discarded.
This principle is particularly important in database query execution, aggregation functions, and data processing pipelines where the same operations may be performed millions of times.
Enter the URL of a public GitHub repository