Direct-Mapped vs Set-Associative vs Fully Associative Cache
A cache must decide where each block of memory may be stored. In a direct-mapped cache each block has exactly one possible line, which is simple and fast but suffers conflicts when two blocks share a line. In a fully associative cache a block can go anywhere, which avoids conflicts but needs a comparison against every line. A set-associative cache is the compromise: a block can go in any of a few ways within one set. Most CPU caches are set-associative.
| Aspect | Direct-mapped | Set-associative | Fully associative |
|---|---|---|---|
| Where a block can go | Exactly one line, chosen by the index bits | Any of the ways (for example 4 or 8) in one set | Any line in the cache |
| Tag comparisons per access | One | One per way (for example 8) | All lines at once, in parallel |
| Hit time | Fastest | Slightly slower | Slowest |
| Hardware cost and power | Lowest | Moderate | Highest: a comparator for every line |
| Conflict misses | Most frequent | Reduced as the number of ways grows | None: only compulsory and capacity misses remain |
| Replacement policy | Not needed: only one choice | Chosen within the set (LRU or approximations) | Chosen among all lines |
| Typical uses | Small, simple caches and some microcontrollers | L1, L2 and L3 caches in most CPUs, usually 4 to 16 ways | Small structures such as TLBs and victim caches |
Figures are typical orders of magnitude and vary by process generation and product.