Programming
We provide developers and engineers the guides and tutorials they...
Search
We provide developers and engineers the guides and tutorials they...
Content is king. It's not just about how you're going to write, b...
Looking for an engaging connection with thousands of other people...
Why memory access dominates performance: L1, L2 and L3 caches, cache lines, locality, false sharing, and practical tips for cache-friendly code.
Quick answer: A CPU can execute instructions far faster than main memory can deliver data. To bridge the gap, processors keep small, extremely fast memories called caches (L1, L2 and L3) that hold copies of recently used data. If the data a program needs is in the cache (a hit), it arrives in about a nanosecond. If not (a miss), the CPU waits around a hundred times longer for RAM. Code that reads memory in a compact, predictable order gets mostly hits and can run many times faster than code doing the same work in a scattered order.
A modern core can perform several simple operations per nanosecond. A trip to main memory takes roughly 100 nanoseconds. Without caches, the processor would spend almost all its time waiting.
The fix is a hierarchy of memories, each level larger and slower than the one before:
| Level | Typical size | Typical access time | Shared? |
|---|---|---|---|
| L1 | Tens of KB per core | About 1 ns | Per core |
| L2 | Hundreds of KB to a few MB per core | A few ns | Usually per core |
| L3 | Several MB to tens of MB | 10 to 20 ns or more | Shared across cores |
| Main memory (RAM) | Gigabytes | Around 100 ns | Whole system |
The figures vary by processor, but the ratios are what matter. The well-known latency numbers list and Ulrich Drepper's What every programmer should know about memory explain the hardware reasons in depth.
Caches rely on two patterns that almost all programs show:
To exploit spatial locality, the cache never loads a single byte. It loads a whole cache line, typically 64 bytes. Touch one element of an array and its neighbours come along for free.
The CPU also has a prefetcher that spots simple patterns, such as walking forward through memory, and loads upcoming lines before you ask for them.
Summing a large two-dimensional array can be written two ways:
// Row by row: walks memory in order
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
sum += grid[i][j];
// Column by column: jumps N elements each step
for (int j = 0; j < N; j++)
for (int i = 0; i < N; i++)
sum += grid[i][j];
Both do exactly the same additions. But in C, rows are stored one after another, so the first version reads memory sequentially and the second jumps to a different cache line on almost every access. For a large array, the second version can be several times slower. Same algorithm, same big-O, very different speed.
This is why the "best" data structure in theory is not always the fastest in practice.
Where data lives matters too. Values on the stack or packed in an array are contiguous; many small separately allocated heap objects are scattered. See stack vs heap.
perf stat -e cache-misses ./program reports misses directly.Caches work on whole lines, which creates a subtle problem with threads. Suppose two threads each update their own counter, and the two counters happen to sit in the same 64-byte cache line. Each write by one core forces the other core's copy of the line to be invalidated. The line bounces between cores, and the program runs far slower than expected, even though the threads never touch the same variable.
This is false sharing. The fix is to place heavily written per-thread data on separate cache lines, usually by adding padding or alignment. For more on threads sharing memory, see processes vs threads.
"Cache" appears everywhere in computing: the browser cache, CDN caches, the operating system's file cache, Redis. They all share one idea, keeping a fast copy of something slow to fetch, but the CPU cache is hardware inside the processor and is managed automatically. You cannot control it directly; you can only arrange your data so it works well. The same layered idea applies to the whole machine, as described in why we need both RAM and storage.
It happens when the CPU needs data that is not in the cache and must fetch it from a slower level or from RAM. The processor stalls while it waits.
Larger caches hold more but are slower to search, which is why there are several levels: a tiny, very fast L1 backed by larger, slower L2 and L3.
Yes, though interpreter overhead usually dominates. Libraries such as NumPy are fast largely because they store numbers in contiguous arrays and process them in tight native loops.
The unit the cache works in, usually 64 bytes. Loading any byte loads the whole line containing it.
On modern hardware, memory access patterns often matter more than instruction counts. The cache rewards code that keeps related data together and reads it in order, and punishes code that hops around memory. Choose contiguous data structures, loop in layout order, and measure cache misses before reaching for cleverer algorithms.