-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use array for tracking memory usage instead of map #25269
base: master
Are you sure you want to change the base?
Use array for tracking memory usage instead of map #25269
Conversation
`std::map` has very slow access are requires lock on every access. We can use `std::array` instead to hold compile time known number of buckets. This method should have less latency and memory overhead, as well as possibly no lock on access.
No need to lock mutex to update atomic values
build_jenkins |
Hi @ialbrecht, |
std::map<allocation_type, std::atomic<uint64_t>> _memory_usage_map; | ||
std::map<allocation_type, std::atomic<uint64_t>> _peak_memory_usage_map; | ||
std::array<std::atomic<uint64_t>, static_cast<size_t>(allocation_type::max_value)> _memory_usage_data; | ||
std::array<std::atomic<uint64_t>, static_cast<size_t>(allocation_type::max_value)> _peak_memory_usage_data; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like array content has undefined values due to lack of explicit initialization
_memory_usage_data[idx] += bytes; | ||
auto new_val = _memory_usage_data[idx].fetch_add(bytes); | ||
// Make sure actual maximum value is stored | ||
while (new_val > _peak_memory_usage_data[idx]) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason to have while
instead of if
here?
Details:
std::map
has very slow access are requires lock on every access. We can usestd::array
instead to hold compile time known number of buckets.array
container has lower access latency and memory overhead.