Cache Memory
How caches are organised — lines, tags, sets and associativity; mapping addresses; replacement and write policies; the 3 Cs of misses; coherence — with an interactive cache simulator.
A cache is a small, fast memory that holds copies of recently used lines of main memory. On every access the hardware must answer quickly: "is this address in the cache, and if so where?" The answer depends on how the cache is organised.
Splitting an address
For a cache with 2ˢ sets and 2ᵇ-byte lines, an address is split into three fields:
- Offset (b bits) — which byte within the line.
- Index (s bits) — which set the line must go in.
- Tag (the rest) — stored alongside the line to identify which memory block is there.
Each line also has a valid bit, and a dirty bit for write-back caches.
Associativity
| Organisation | A block can go in | Pros | Cons |
|---|---|---|---|
| Direct-mapped | Exactly one line | Simplest, fastest lookup | Conflict misses when hot blocks share an index |
| N-way set-associative | Any of N lines in its set | Far fewer conflicts | Compare N tags in parallel |
| Fully associative | Anywhere | No conflict misses | Expensive; only for tiny caches (TLBs) |
Real L1 caches are typically 8–12-way, L2 16-way, and L3 12–16-way.
Try it
Choose direct-mapped or 2-way set-associative, step through the access sequence, and watch hits, misses and evictions.
| set | way 0 |
|---|---|
| 0 | tag 0 |
| 1 | — |
| 2 | — |
| 3 | — |
| 4 | — |
| 5 | — |
| 6 | — |
| 7 | — |
The 3 Cs of misses
| Miss type | Cause | Reduce it by |
|---|---|---|
| Compulsory (cold) | First access to a block | Bigger lines, prefetching |
| Capacity | The working set is larger than the cache | A bigger cache, better locality (tiling) |
| Conflict | Too many blocks map to the same set | Higher associativity, padding data |
(A 4th C, coherence misses, appears with multiple cores.)
Replacement policies
When a set is full, which line is evicted?
- LRU (least recently used) — best in general; exact LRU is costly for high associativity, so hardware uses pseudo-LRU (tree bits).
- FIFO, random — cheap; random is surprisingly decent.
- Modern LLCs use adaptive policies (RRIP) that resist scanning patterns, where a one-off sweep through data would otherwise flush everything useful.
Write policies
Every write updates the cache and memory. Simple and memory is always up to date, but it generates heavy traffic, so it's usually paired with a write buffer.
Writes update only the cache and mark the line dirty; memory is updated when the line is evicted. Much less traffic. Used by nearly all modern CPU caches.
On a write miss: write-allocate loads the line and then writes it (usual with write-back); no-write-allocate writes straight to memory.
Worked example
A 32 KB, 8-way cache with 64-byte lines:
- Lines: 32 KB / 64 B = 512 lines.
- Sets: 512 / 8 = 64 sets → 6 index bits.
- Offset: 64 B → 6 offset bits.
- With 48-bit addresses: tag = 48 − 6 − 6 = 36 bits.
Notice that index + offset = 12 bits = a 4 KB page offset. That's deliberate: the L1 can be indexed with the virtual address while the TLB translates the tag in parallel (VIPT caches).
Multicore: cache coherence
Each core has private L1/L2 caches. If two cores cache the same line and one writes, the other's copy becomes stale. Coherence protocols prevent that. The classic one is MESI:
| State | Meaning |
|---|---|
| Modified | Only this cache has it, and it's dirty |
| Exclusive | Only this cache has it, and it's clean |
| Shared | Several caches may have clean copies |
| Invalid | Not usable |
A write to a Shared line first invalidates the other copies. Cores keep track through snooping or a directory.
- Address = tag | index | offset; the index selects a set, the tag identifies the block.
- Associativity trades lookup cost for fewer conflict misses.
- Misses: compulsory, capacity, conflict (and coherence).
- Write-back + write-allocate is the norm; dirty lines are written on eviction.
- MESI keeps per-core caches coherent; watch out for false sharing.
QGiven a 64 KB 4-way cache with 32-byte lines and 32-bit addresses, how many tag bits are there?medium
Lines = 64 KB / 32 B = 2048. Sets = 2048 / 4 = 512 → 9 index bits. Offset = 5 bits. Tag = 32 − 9 − 5 = 18 bits.
QWhat is false sharing and how do you fix it?hard
Independent variables written by different cores share a cache line, so coherence invalidations bounce the line between cores even though there's no real data sharing. Pad or align each thread's hot data to its own cache line, or accumulate locally and combine at the end.