Lesson 2 of 9 · 21m
Caches, Lines, and the Memory Hierarchy
What L1/L2/L3 actually are, why memory moves in 64-byte lines, and how associativity explains strange benchmark cliffs.
New hires often imagine cache as a small bag holding whichever variables are “hot.” Real caches are more mechanical: memory is transferred in fixed-size lines, mapped into sets, and evicted when competing addresses collide. Once you know that machinery, odd symptoms — tiny arrays beating big ones, power-of-two strides performing terribly — stop looking mystical.
Memory moves in cache lines
Typical x86 and ARM server cores use 64-byte cache lines. When your code reads one byte, the memory system fetches the whole aligned line containing it:
address 0x1000..0x103f one 64-byte line
address 0x1040..0x107f the next lineThat means locality is physical, not merely conceptual. Four adjacent integers often arrive together for the cost of one DRAM access. Four objects scattered across the heap require four independent lines even if each object is only eight bytes.
struct point { double x, y; };
struct point points[1024]; // packed: x and y neighbors share lines
struct point *scattered[1024]; // random allocations: every access can missThe hierarchy is a compromise
L1 is small and fast, L2 is larger and slower, and the last-level cache is larger still and often shared between cores. Sizes differ, but the shape is stable:
L1D: tens of KB per core fastest, closest to execution
L2: hundreds of KB per core catches L1 misses
L3: megabytes, often shared catches L2 misses before DRAM
DRAM: gigabytes large, comparatively distantThe hierarchy exists because a single huge, fast memory is physically impossible at sane cost and power. Fast memory is small because close wires, low latency, and high bandwidth are expensive.
Cache mapping causes cliffs
Caches do not store arbitrary lines anywhere. Addresses map to sets, and each set has limited associativity — perhaps 8 or 16 ways. If too many hot addresses map to the same set, they evict one another even while the rest of the cache is mostly idle.
This is why power-of-two strides can be poisonous. Iterating a large matrix by a stride that maps rows onto the same cache sets can cause conflict misses even though the total working set appears small enough to fit.
Prefetchers reward boring access patterns
Hardware prefetchers detect sequential and strided accesses and fetch upcoming lines before the program asks. They do not understand linked lists, hash tables, or pointer-heavy object graphs. You can help them by making the common path stream memory in one direction with a stable stride.
Field notes
- “Uses less memory” and “fits in cache” are different claims; cache fit is about the active working set.
- A line is fetched even if only one field in a struct is read. Packing hot fields together pays.
- False sharing happens because coherence operates on lines, not variables — covered in detail later.
- Matrix traversal order matters: row-major languages reward row-wise loops; column-wise loops can destroy locality.
lscpu,lstopo, and vendor manuals are more reliable than guesses about cache sizes.
Was this lesson helpful?