2. The cache hierarchy
One-liner. Small, fast, expensive memory close to the core; each level trades capacity for latency.
Memorize
| Level | Typical size | Latency (cycles) |
|---|---|---|
| L1 | 32–64 KB | ~4–5 |
| L2 | 256 KB–1 MB | ~12–20 |
| L3 | 8–64 MB (shared) | ~40–70 |
| DRAM | GBs | ~100–300 |
- A cache line is 64 bytes; you always fetch a whole line, even for one byte.
- Address splits into tag / set index / block offset; associativity decides how many lines share a set.
- Write-back + write-allocate is the default: writes hit the cache and flush later.
- Inclusive caches duplicate lines upward; exclusive/victim caches avoid duplication.
- Hardware prefetchers detect streams and strides; unpredictable access defeats them.
- Cache is why sequential memory access beats pointer chasing by an order of magnitude.
Trap. Optimizing instruction count while ignoring memory access patterns — a cache miss can cost more than hundreds of arithmetic ops. Do this. Measure with perf stat (cache-misses, LLC-load-misses) before micro-tuning.
Check
Why can reading one byte be as expensive as reading 64?
Why
Spatial locality is exploited by fetching the line once; subsequent bytes in that line are nearly free.