December 2025

Speeding up C++ functions with a thread_local cache -- Daniel Lemire

9af1530f-1aca-4fbc-8796-ff74a1a8b544-784x510.jpgWhen working with legacy or rigid codebases, performance bottlenecks can emerge from designs you can’t easily change—like interfaces that force inefficient map access by index. This article explores how a simple thread_local cache can dramatically improve performance in such cases, reducing repeated lookups from quadratic to near-constant time.

Speeding up C++ functions with a thread_local cache

by Daniel Lemire

From the article:

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 concrete example. Maybe someone designed the programming interface so that you have to access the values from a map using an index.