Back to all reviewers

Cache expensive computations

microsoft/terminal
Based on 8 comments
C++

Identify and cache the results of expensive operations that are computed repeatedly with the same inputs. This includes function calls, data structure creation, regex compilation, and complex calculations that don't change between invocations.

Performance Optimization C++

Reviewer Prompt

Identify and cache the results of expensive operations that are computed repeatedly with the same inputs. This includes function calls, data structure creation, regex compilation, and complex calculations that don’t change between invocations.

Common patterns to watch for:

  • Repeated calls to the same expensive function with identical parameters
  • Recreating the same data structures across multiple instances
  • Recompiling regular expressions on each use
  • Duplicate algorithmic work in related operations

Examples of optimization opportunities:

// Before: Each CommandViewModel duplicates the same data
class CommandViewModel {
    IMap<ShortcutAction, hstring> _AvailableActionsAndNamesMap; // Duplicated across instances
};

// After: Share expensive data across instances
class CommandViewModel {
    static const IMap<ShortcutAction, hstring> _SharedActionsAndNames; // Computed once
};

// Before: Repeated expensive function calls
if (_WindowProperties.WindowName() != L"") {
    auto name = _WindowProperties.WindowName(); // Called again
}

// After: Cache the result
if (const auto windowName = _WindowProperties.WindowName(); !windowName.empty()) {
    // Use windowName variable
}

// Before: Regex compiled on every validation
void _validateRegex(const hstring& regex) {
    std::wregex{ regex.cbegin(), regex.cend() }; // Expensive compilation each time
}

// After: Cache compiled regex instances
static std::unordered_map<hstring, std::wregex> _regexCache;

This optimization is particularly important for operations in hot paths, UI updates, or when processing large datasets. Always measure performance impact to ensure the caching overhead doesn’t exceed the benefits.

8
Comments Analyzed
C++
Primary Language
Performance Optimization
Category

Source Discussions