Game dev
Saved & recent

Cache misses

Related terms: CPU cache, data-oriented design, memory layout

Reading memory is a hidden cost in game code. The CPU keeps recently used memory in a small, very fast cache. Data sitting next to what was just read is almost free. Data somewhere else is a cache miss, roughly a hundred times slower.

Looping over thousands of objects scattered around memory is a chain of misses. Data-oriented design stores the same kind of data together in tight arrays and walks it in order. This is the performance argument for ECS.

Further reading
Microsoft: Tips for Improving Time-Critical Code: Cache Misses and Page Faults
1 2 3 4 5 6 7 8 scattered records 1 8 4 6 3 2 7 5 8 misses · 0 hits 1 2 3 4 5 6 7 8 packed records 1 1 1 1 2 2 2 2 2 misses · 6 hits one cache line line numbers in read order
Assume an empty cache that can hold all eight lines. The scattered reads touch eight different lines, so all eight miss. The packed reads touch two lines, producing two misses and six hits.

View in the full Game dev collection