Back to all reviewers

Optimize memory allocation patterns

opencv/opencv
Based on 5 comments
C++

Prefer efficient memory allocation patterns to improve performance. Key practices: 1. Use RAII containers (like Mat) instead of raw pointers to prevent memory leaks

Performance Optimization C++

Reviewer Prompt

Prefer efficient memory allocation patterns to improve performance. Key practices:

  1. Use RAII containers (like Mat) instead of raw pointers to prevent memory leaks
  2. Pre-allocate containers when size is known
  3. Use stack allocation for small, fixed-size objects
  4. Avoid unnecessary dynamic allocation

Example - Before:

// Inefficient allocation patterns
char* get_raw_data() {
    return new char[size];  // Raw pointer, potential memory leak
}

std::vector<Mat> img_vec;
img_vec.push_back(img);  // Potential reallocation

fcvPyramidLevel_v2 *framePyr = new fcvPyramidLevel_v2[2];  // Unnecessary heap allocation

Example - After:

// Efficient allocation patterns
Mat get_data() {
    return Mat(size);  // RAII handles cleanup
}

std::vector<Mat> img_vec(1, img);  // Pre-allocated size

fcvPyramidLevel_v2 framePyr[2];  // Stack allocation for small arrays

Benefits:

  • Prevents memory leaks
  • Reduces allocation overhead
  • Improves cache utilization
  • More predictable performance
5
Comments Analyzed
C++
Primary Language
Performance Optimization
Category

Source Discussions