Set associative mapping combines direct mapping with fully associative mapping by arrangement lines of a cache into sets. The sets are persistent using a direct mapping scheme. However, the lines within each set are treated as a small fully associative cache where any block that can save in the set can be stored to any line inside the set.
The diagram represents this arrangement using a sample cache that uses four lines to a set.
A set-associative cache that includes k lines per set is known as a k way set-associative cache. Because the mapping approach uses the memory address only like direct mapping does, the number of lines included in a set should be similar to an integer power of two, for example, two, four, eight, sixteen, etc.
Example − Consider a cache with 29 = 512 lines, a block of memory contains 23 = 8 words, and the full memory space includes 230 = 1G words. In a direct mapping scheme, this can leave 30 – 9 – 3 = 18 bits for the tag.
By sending from direct mapping to set associative with a set size of two lines per set, the various sets achieved equals half the number of lines. In the instance of the cache having 512 lines, we can achieve 256 sets of two lines each, which would require eight bits from the memory address to recognize the set.
This can leave 30 – 8 – 3 = 19 bits for the tag. By sending to four lines per set, the number of sets is decreased to 128 sets needing 7 bits to recognize the set and twenty bits for the tag.