Sub topic 11
Replacement Policies: LRU, FIFO, and Random
This chapter assumes that you know cache blocks, cache lines, sets, tags, and hit or miss results. You should also be comfortable tracing a sequence of memory-block references.
A replacement policy decides which cache block to remove when a new block must enter a full cache line or set. Direct-mapped caches do not choose among several blocks: each set has one line, so the incoming block always replaces the current block. Replacement policies matter in fully associative and set-associative caches.
Why replacement policies matter
Suppose a 2-way set can hold only two blocks, but a program references three blocks that all map to that set. At least one block must be evicted. A good policy removes a block that the program is unlikely to use soon.
| Policy | Block removed | Main idea |
|---|---|---|
| LRU | Least recently used | Keep blocks used recently |
| FIFO | First block inserted | Evict the oldest resident block |
| Random | Randomly selected block | Avoid tracking usage order |
The policies affect hit rate, hardware cost, and predictability. They do not change the address mapping. They only choose a victim after mapping identifies a full set.
LRU: least recently used
LRU removes the block that has gone unused for the longest time. After every hit, the block becomes the most recently used. After an insertion, the new block also becomes the most recently used.
Consider a 2-way set and this reference string:
A, B, A, C, A, B
Trace the set as follows:
- Access
A: miss. The set containsA. - Access
B: miss. The set containsA, B.Bis most recent. - Access
A: hit. NowAis most recent. - Access
C: miss.Bis least recent, so evictB. The set containsA, C. - Access
A: hit.Aremains most recent. - Access
B: miss.Cis least recent, so evictC.
LRU produces two hits and four misses in this trace.
Exact LRU tracking can require usage information for every way. This increases hardware complexity, especially for highly associative caches.
FIFO: first in, first out
FIFO removes the block that entered the set earliest. A hit does not change its insertion order.
For the same trace, after A, B, A, the insertion order is still A before B, because the hit on A does not reinsert it. Accessing C therefore evicts A, producing [B, C]. The next access to A misses.
FIFO is simpler than exact LRU because it only tracks age since insertion. However, it can evict a frequently used block if that block happens to be old.
Random replacement
Random replacement selects a victim without using access history. It needs little tracking hardware and can avoid some predictable patterns that repeatedly defeat a fixed policy. Its hit count varies between runs, so numerical questions must specify how the random choices are handled.
from collections import OrderedDict
def lru_hits(trace, capacity):
cache = OrderedDict()
hits = 0
for block in trace:
if block in cache:
hits += 1
cache.move_to_end(block)
else:
if len(cache) == capacity:
cache.popitem(last=False)
cache[block] = True
return hits
print(lru_hits(["A", "B", "A", "C", "A", "B"], 2)) # 2Exam strategy and common errors
First map each memory block to its set. Then maintain a separate replacement order for each set. Do not compare blocks from different sets. For a non-full set, insert the block without evicting anything. For a full set, apply the specified policy.
A policy can produce a different result from another policy even when the cache size and reference string are identical. Always count the initial compulsory misses as well as later misses caused by eviction.
Exercise
A 2-way set uses FIFO. Starting empty, what happens for the references A, B, A, C?
Show answer
A misses, B misses, A hits, and C misses. FIFO evicts A because it was inserted before B. The final contents are B and C.