Here is a puzzle from a real chip, an AMD Ryzen 7 7700, measured for this chapter. A small program reads k memory locations in turn, over and over, each read waiting for the one before it. For k up to 8, every read takes about 0.76 nanoseconds. At k = 9 it jumps to 6 nanoseconds, and from k = 10 it settles at 3.4, more than four times slower. Nothing else has changed: nine locations are only 576 bytes of data, nothing next to the processor's 32 KB level-1 cache.
Now move each location 64 bytes further from the next one, a tiny change, and run it again. The time stays at 0.76 nanoseconds for every k, all the way to 20. Same number of locations, same amount of data, same cache. The only difference is where the locations sit. How can the address of a byte decide whether a read costs 0.76 or 3.4 nanoseconds?
The answer is a rule that every cache follows, and it is what this chapter is about. A cache holds copies of memory blocks, but it cannot put a block just anywhere, because then finding it again would mean searching the whole cache. So each cache has a mapping rule that says which lines a block may use. Three rules exist, and the choice between them decides how fast the cache is, how much it costs and, as the puzzle shows, which programs it quietly punishes.
By the end you will be able to place any block under all three rules and say how a lookup works in each. You will trace the same list of addresses through a direct-mapped, a set-associative and a fully associative cache and count the hits by hand. You will name each miss as compulsory, capacity or conflict and say what cures it. And you will show which block LRU or FIFO throws out when a set is full, and why real chips only approximate LRU.