The Memory Hierarchy & Caches

a set-associative cache

Go back to the coat-check, but now each ticket number maps not to one locker but to a small bank of, say, four lockers, and your coat may go in any of those four. Finding it means checking just those four (quick), but now four people with colliding numbers can all leave coats without anyone being turned away. A set-associative cache is this compromise: each memory block maps to one SET, but within that set it may sit in any of several ways (slots), giving some flexibility without the cost of searching everywhere.

An n-way set-associative cache divides the cache into sets, each holding n lines (the 'ways'). The address splits into offset, index, and tag as before, but now the index picks a set rather than a single slot. Lookup compares the address's tag against all n tags in that set in parallel; if any matches a valid line, it is a hit. Because a block can live in any of n places within its set, you now need a replacement policy (commonly LRU or an approximation) to choose which way to evict on a miss. Direct-mapped is just the n=1 case; fully associative (a block may go anywhere, one big set) is the n equals total-lines case.

Associativity is the main weapon against conflict misses: with several ways per set, two hot blocks that map to the same set can coexist instead of evicting each other. The cost is real, though — comparing n tags in parallel needs more hardware and can slightly lengthen the hit time, and the replacement logic adds complexity. The classic finding is diminishing returns: going from direct-mapped to 2-way removes a large fraction of conflict misses, 2-way to 4-way removes much of the rest, and beyond 8-way the gain is usually tiny. So real L1 caches are often 4- or 8-way: enough associativity to tame conflicts, not so much that hit time suffers.

In a 4-way set-associative cache, an address's index selects one set; the four tags there are compared at once. Two arrays that collided to one slot in a direct-mapped cache can now occupy two different ways of the same set and both stay resident — conflict misses gone.

Each block maps to a set with several ways; parallel tag compare finds it, easing conflict misses.

More associativity is not free and has diminishing returns: 1-way to 2-way to 4-way removes most conflict misses, but past ~8-way the extra parallel tag checks add hit-time and power cost for little benefit. Associativity is a tradeoff, not a 'higher is always better' dial.

Also called
n-way set-associative cache組相聯快取集合關聯快取