Speeding up C++ functions with a thread_local cache
lemire.me·6h·
Discuss: Hacker News
Flag this post

In large code bases, we are often stuck with unpleasant designs that are harming our performance. We might be looking for a non-intrusive method to improve the performance. For example, you may not want to change the function signatures.

Let us consider a concret example. Maybe someone designed the programming interface so that you have to access the values from a map using an index. They may have code like so:

auto at_index(map_like auto& index_map, size_t idx) {
size_t count = 0;
for (const auto &[key, value] : index_map) {
if(count == idx)
return value;
count++;
}
throw std::out_of_range("Index out of range");
}

This code goes through the keys of the map idx times. Typictally, it implies some kind of linked list traversal. If you are stuck with this interface, going…

Similar Posts

Loading similar posts...