Gotta Cache 'Em All: Optimize Your C++ Code By Utilizing Your Cache!
summary
Replacing an unordered_map with a vector produces a 10x speedup in a production case (toy test showed ~15x) because contiguous memory and index lookup avoid hashing, pointer chasing, and cache misses; a 512 MB cache polluter is used to clear caches between runs. Benchmarks compare std::vector, std::set, std::unordered_set, boost::flat_set and boost::unordered_flat_set (boost unordered flat wins at large N; std::unordered_set wins small, vector wins for N<10). Measurement uses RDTSC, taskset, BIOS SMT off and memory fences; padding or thread_local eliminates 67% improvement from removing false sharing; cachegrind misses multi-core effects.
AI-written summary · about
