← ⚡ Cache Memory
Subtopic 1 of 9

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

  1. Temporal locality: if you used x, you will probably use x again soon.
  2. Spatial locality: if you used a[i], you will probably use a[i+1].
  3. So on a miss, fetch a whole block of neighbours, not one word.
  4. 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.
12345678
index01234567
accessMHHHMHHH

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?

Remember: Locality + blocks = most accesses become hits.
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.