Back to all reviewers

Optimize container operations

pytorch/pytorch
Based on 2 comments
Other

Minimize CPU overhead in container operations by using efficient data structure patterns: 1. **Use static containers** for fixed lookup tables or maps that are frequently accessed but rarely changed. This avoids reconstruction overhead on each function call:

Performance Optimization Other

Reviewer Prompt

Minimize CPU overhead in container operations by using efficient data structure patterns:

  1. Use static containers for fixed lookup tables or maps that are frequently accessed but rarely changed. This avoids reconstruction overhead on each function call: ```cpp // Inefficient: Recreates map on every function call void handle() { std::unordered_map<int, HandlerFn> handler_map = { /* entries */ }; // … }

// Efficient: Creates map once for all calls void handle() { static std::unordered_map<int, HandlerFn> handler_map = { /* entries */ }; // … }


2. **Prefer emplace over insert** for maps and other containers to avoid unnecessary temporary object construction:
```cpp
// Less efficient: Creates a temporary pair object
args.insert({DNNL_ARG_SRC, dnnl::memory(m1_md, eng, m1.data_ptr())});

// More efficient: Constructs the object in-place
args.emplace(DNNL_ARG_SRC, dnnl::memory(m1_md, eng, m1.data_ptr()));

These optimizations reduce memory allocations, minimize CPU cycles, and can significantly improve performance in hot code paths or when working with large containers.

2
Comments Analyzed
Other
Primary Language
Performance Optimization
Category

Source Discussions