What is Set-associative cache?

A cache organization in which data can go in any of several locations (ways) within the one set selected by the address. An n-way cache compares n tags in parallel on each access.

Field: Memory Hierarchy

Japanese name: セットアソシアティブ方式

Lookup in a set-associative cache Address Offset: position in line Tag Index Offset Way 0 Tag Data Way 1 Tag Data Selects set = = Compare tags Any match → hit No match → miss Address tag vs. every way's tag at once 2-way example (n-way compares n tags)
Lookup in a set-associative cache · View in diagrams

How the address is split

A cache address is divided into three fields. The low offset bits pick a byte within a line, the index bits pick one set, and the remaining high bits form the tag stored alongside the line. The number of sets equals capacity divided by line size times the number of ways. For a 32 KB cache with 64-byte lines and 32-bit addresses there are 512 lines: direct-mapped gives 512 sets, a 9-bit index and a 17-bit tag, while 4-way gives 128 sets, a 7-bit index and a 19-bit tag. Each doubling of associativity removes one index bit and adds one tag bit.

What happens on an access

The index selects a set, and the tags of all n ways in that set are read and compared with the address tag simultaneously, so an 8-way cache needs eight comparators. Each comparison also checks a valid bit. The way whose tag matches drives a multiplexer that delivers the data; many fast L1 designs read the data of every way in parallel with the tag check and select at the end, trading power for speed. On a miss, a replacement policy such as LRU (least recently used) or a cheaper pseudo-LRU approximation chooses which way in the set to evict.

Direct-mapped, set-associative and fully associative

These are points on one spectrum. A direct-mapped cache is 1-way: each address has exactly one possible slot, so two hot blocks that share an index evict each other repeatedly, a conflict miss. A fully associative cache is a single set in which any line can go anywhere, removing conflict misses but requiring a comparison against every tag. Set-associative caches sit in between. Raising associativity reduces conflict misses but not compulsory or capacity misses, and returns diminish beyond about 8 ways, while comparators, multiplexer delay, hit time and power keep rising.

Typical numbers

Level-1 caches are commonly 4 to 12 ways because hit time is critical and the cache is small. Last-level caches, where a miss costs a trip to DRAM, often use around 16 ways, since a slightly slower hit is acceptable in exchange for fewer misses. Translation lookaside buffers (TLBs) are frequently highly or fully associative because they hold few entries and a miss is expensive. When estimating hardware cost, remember that tag storage grows with associativity too, because, at the same capacity, each doubling of the ways removes an index bit and lengthens every stored tag by one bit.

Try it: questions about Set-associative cache

Which correctly describes a set-associative cache?

  1. AData for a given address can be placed in any of several locations (ways) within a fixed set
  2. BData for a given address can be placed in only one location in the cache
  3. CData can be placed anywhere in the cache
Show answer and explanation

Answer: A. Data for a given address can be placed in any of several locations (ways) within a fixed set

In a set-associative cache, part of the address selects a set, and data may occupy any of that set's ways—eight candidate slots in an 8-way cache—reducing conflict misses compared with direct mapping. More ways need more comparators and power, so the way count is kept moderate—for example, 8–12 ways for L1 and around 16 for L3. A cache in which data can go anywhere is fully associative.

How many sets does a 4-way set-associative cache with 32-bit addresses, 32 KB capacity and 64-byte lines have?

  1. A128
  2. B512
  3. C32
Show answer and explanation

Answer: A. 128

The number of lines is32 KB64 B=51232 KB ÷ 64 B = 512. A 4-way cache holds four lines per set, so there are5124=128=27512 ÷ 4 = 128 = 2⁷sets, giving a 7-bit index and a 32 − 7 − 6 = 19-bit tag. 512 is the line count itself, which equals the set count only when direct-mapped. Raising associativity reduces the number of sets, which adds tag bits and comparators. 32 comes from dividing the 128 sets by the four ways a second time.

FAQ

How do you calculate the number of sets in a set-associative cache?

Divide the capacity by the line size to get the number of lines, then divide by the number of ways. A 32 KB, 4-way cache with 64-byte lines has 512 lines and therefore 128 sets.

Does higher associativity always make a cache faster?

No. It lowers conflict misses, but each access must compare more tags and select among more ways, which tends to lengthen hit time and raise power. The best choice balances miss rate against hit time for that cache level.

What is the difference between a set and a way?

A set is the group of slots that one index value selects, and a way is one slot within that set. An 8-way cache has eight ways in every set, and a block may be placed in any of them.

Questions that mention Set-associative cache (3)

Set-associative cache appears in 5 practice questions across 2 topics, including Memory Hierarchy, CPUs and architecture.

  1. In an 8-way set-associative cache, how many tags are compared in parallel on each access?
  2. For a direct-mapped cache with 32-bit addresses, 32 KB capacity and 64-byte lines, how many tag bits are there?
  3. What changes when a cache is switched from direct-mapped to set-associative?

Books on Memory Hierarchy Ad

As an Amazon Associate I earn from qualifying purchases.

Comparisons that include this term

Semiconductor, Electronics and Computing Calculators

This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.

Back to the glossary