3.1Why cache works
The idea
A cache works because programs show locality: they reuse recent data (temporal) and touch neighbours of recent data (spatial).
Think of it like this: Keep the books you are using on your desk (cache), the rest on the shelf (memory) and in the library (disk).
Why it exists
Main memory is far slower than the CPU. A small fast memory holding the right data hides that delay.
How it works
- Temporal locality: if you used x, you will probably use x again soon.
- Spatial locality: if you used a[i], you will probably use a[i+1].
- So on a miss, fetch a whole block of neighbours, not one word.
- Later accesses to that block are cheap hits.
Worked example: Reading a[0..7], 4 words per block
a[0] misses and brings in a[0..3]; a[1], a[2], a[3] hit. a[4] misses again. Result: 2 misses, 6 hits (75% hits).
Now play with it
- →Click the tabs: the spatial pattern is M H H H, the strided one is all misses.
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | |
|---|---|---|---|---|---|---|---|---|
| index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| access | M | H | H | H | M | H | H | H |
Block holds 4 words. First touch misses and brings neighbours too, so the next 3 hit: 75% hits.
Check yourself
Sequential read of 16 words, 4 words per block, cold cache. How many misses?
Exam notes (the dense version)
Temporal locality (reuse soon) and spatial locality (neighbours soon). Blocks (lines) are fetched, not single words.
Previous year questions: Cache Memory (22)
PYQs are grouped by topic, so this is the full set for Cache Memory; try the ones that match this subtopic. Open all 22 on the topic page.