Replacement: LRU, FIFO, Random and the Pseudo-LRU Tree
When a set is full, a new block pushes an old one out. Which one goes decides how many of the next reads hit, and how much logic every set must carry.
You Will Be Able To
- Say what LRU, FIFO and random replacement each give up, and what each must remember.
- Follow a pseudo-LRU tree of three bits to its way, and update it after a use.
- Show a trace on which the tree and true LRU choose differently.
- Lesson
- S3.16
- Time
- 28 min
- Runs on
- Your browser
- Check
- 6 questions
A set holds four blocks. One of them is read in every round of a loop. A fifth block arrives, and the cache throws out exactly that busy block. The next round starts with a miss that nobody expected.
Before You Start
You need S3.4: sets and ways, and a set that is full. You need S3.2: temporal locality.
You also need to count orders: three things can stand in a row in six ways.
| Question | |
|---|---|
| 1 | When must a cache choose a block to replace, and why does the choice matter? |
| 2 | What does LRU replace, and what must it remember? |
| 3 | What do FIFO and random replace, and what do they save? |
| 4 | How does a pseudo-LRU tree point at a way, and how does a use change it? |
| 5 | When does the tree choose differently from true LRU? |
The Idea: Choosing the Block That Leaves
A miss brings a block in. If its set has an empty way, the block goes there. If the set is full, one block must leave. The block that leaves is called the victim, and the rule that picks it is the replacement policy. A direct-mapped cache has no choice to make. Every cache with ways has.
The best victim is the block that will not be needed for the longest time. Nobody knows the future, so the cache guesses from the past. Temporal locality says: what was used a moment ago will be used again soon. So the block that was used longest ago is the best guess. That policy is LRU, for least recently used.
LRU is costly. The cache must know the full order in which the ways of a set were last used. It must also update that order on every hit. With two ways, that is one bit. With four ways there are 24 possible orders, and with eight there are 40,320.
Two policies go the other way and remember almost nothing. FIFO, first in first out, replaces the block that came in first, whether it was used since or not. Random replaces any block. Neither looks at hits at all.
Between them stands a clever compromise. A pseudo-LRU tree keeps three bits for a set of four ways. The bits point away from whatever was used last. Following them finds a block that was not used recently. It is usually the LRU block, and it is never the most recent one.
New Words
| Word | Meaning |
|---|---|
| Victim | The block that must leave a full set, to make room for a new one. Also the way that holds it. |
| Replacement policy | The rule by which a cache picks the victim. |
| LRU | Least recently used. The policy that replaces the block whose last use is furthest in the past. |
| FIFO | First in, first out. The policy that replaces the block that came into the set first. |
| Random replacement | The policy that replaces a way picked by a pseudo-random number. |
| Pseudo-LRU | A policy that comes close to LRU with far fewer bits. This page uses the tree form. |
| Tree bit | One bit of a pseudo-LRU tree. It points at one of two halves: the half that was not used last. |
| Pair of ways | Two ways that share one tree bit. Four ways are two pairs. |
| Use of a way | A hit in that way, or a new block that comes into it. |
One Picture: The Tree of Three Bits
- Processor
- Control / FSM
- Memory
- Bus
- A full set
A new block arrives, and all four ways of its set hold a block. One of them must go. The question is which.
- The top bit picks a pair
The four ways are split into two pairs. One bit, the top bit, points at the pair that was not used last.
- A second bit picks a way
Each pair has a bit of its own. It points at the way of that pair that was not used last. Follow two bits, and you are at one way.
- A use turns the bits away
Whenever a way is used, the two bits on its path are turned to point away from it. The top bit points at the other pair. The pair bit points at the other way.
- Three bits, not a full history
The bit of the other pair is not touched. So the tree remembers who was last in each pair, and which pair was last. It does not know the full order of the four.
Read the diagram as text
- A new block: its set is full
- Top bit: 0: left pair, 1: right pair
- Left bit: 0: way 0, 1: way 1
- Right bit: 0: way 2, 1: way 3
- Ways 0 and 1: the left pair
- Ways 2 and 3: the right pair
- A use of a way: a hit, or a block that comes in
- A new block → Top bit: which way goes?
- Top bit → Left bit: top is 0
- Top bit → Right bit: top is 1
- Left bit → Ways 0 and 1: points at one way
- Right bit → Ways 2 and 3: points at one way
- A use of a way → Top bit: turns the bits away
The pseudo-LRU tree of one set with four ways. The meaning of 0 and 1 is a choice of this page.
The Smallest Example, Worked by Hand
One set with four ways, all empty. Seven reads arrive: blocks 1, 2, 3, 4, then 1 again, then 5, then 3. First work out what true LRU does. The first four reads fill ways 0 to 3. The read of block 1 is a hit. Then block 5 needs a victim.
| Way | Holds | Last used at read | Order |
|---|---|---|---|
| 0 | block 1 | 5 | most recent |
| 1 | block 2 | 2 | least recent: the victim |
| 2 | block 3 | 3 | |
| 3 | block 4 | 4 |
LRU replaces block 2. The last read, of block 3, is then a hit: 2 hits in all.
Now the tree, with all three bits at 0 to begin with. After each use, turn the top bit away from the pair that was used. Then turn the bit of that pair away from the way that was used. Leave the third bit alone.
| Read | Way used | Top | Left | Right | The tree now points at |
|---|---|---|---|---|---|
| block 1, miss | 0 | 1 | 1 | 0 | right pair, way 2 |
| block 2, miss | 1 | 1 | 0 | 0 | right pair, way 2 |
| block 3, miss | 2 | 0 | 0 | 1 | left pair, way 0 |
| block 4, miss | 3 | 0 | 0 | 0 | left pair, way 0 |
| block 1, hit | 0 | 1 | 1 | 0 | right pair, way 2 |
| block 5, miss | 2: the tree said so | 0 | 1 | 1 | left pair, way 1 |
| block 3, miss | 1 | 1 | 0 | 1 | right pair, way 3 |
When block 5 arrives the tree points at way 2, so block 3 goes. True LRU would have taken block 2 in way 1.
The tree made a different choice, and it cost a hit: the last read, of block 3, is now a miss. Look at why. After the hit on way 0 the top bit points at the right pair. Inside that pair, the right bit points at way 2. The tree never compared way 1 with ways 2 and 3.
Still, the tree did not pick badly. Its victim was the second oldest of the four. It can never pick the block that was used last, because every use turns the bits away from itself.
The Rules
Rule 1: A miss uses an empty way first. Only a full set needs a victim.
The replacement policy picks it.
Rule 2: LRU replaces the block that was used longest ago.
It must keep the full order of the ways of each set, and change it on every hit.
Rule 3: FIFO replaces the block that came in first. Random replaces any block.
Neither looks at hits. FIFO needs a small counter for each set, and random needs nothing for a set.
Rule 4: A pseudo-LRU tree keeps one bit for each split in two.
Four ways need three bits: one for the two pairs, and one inside each pair.
Rule 5: Every use turns the bits on its path away from the way that was used.
To find the victim, follow the bits from the top.
Rule 6: The tree never picks the way that was used last, and it does not always pick the LRU way.
It knows the last use inside each half, not the full order.
Sources for These Rules
The textbook "Computer Organization and Design" by Patterson and Hennessy, section 5.4, "Choosing Which Block to Replace", and section 5.8, question 3. They give least recently used and random replacement, and say that exact tracking is too costly beyond a few ways. Section 5.8 also gives the approximation for four ways: one bit for the pair, and one bit inside each pair.
The textbook "Digital Design and Computer Architecture" by Harris and Harris, section 8.3.3. It gives least recently used replacement, the use bit of a two-way cache, and a pseudo-LRU that splits the ways into two groups. Its summary names first-in-first-out and random.
The tree for four ways follows Patterson and Hennessy, section 5.8. The tree for eight ways under "At Scale" is the same idea with one more layer, given as common practice. Harris and Harris use another form: two groups of ways, and a random pick inside a group. Which value of a bit means left is our choice.
The trace, the set of four ways and the numbers of the random policy are our own teaching example.
Now Run It
The lab at the end of this page is test code for one set of four ways. It runs one trace of eleven reads three times: with LRU, with random and with FIFO. It prints a row for each read under LRU, and then the three totals.
LRU read 1: miss into way 0 | ways hold 1 0 0 0
LRU read 2: miss into way 1 | ways hold 1 2 0 0
LRU read 3: miss into way 2 | ways hold 1 2 3 0
LRU read 4: miss into way 3 | ways hold 1 2 3 4
LRU read 1: hit in way 0 | ways hold 1 2 3 4
LRU read 2: hit in way 1 | ways hold 1 2 3 4
LRU read 5: miss into way 2 | ways hold 1 2 5 4
LRU read 1: hit in way 0 | ways hold 1 2 5 4
LRU read 2: hit in way 1 | ways hold 1 2 5 4
LRU read 1: hit in way 0 | ways hold 1 2 5 4
LRU read 4: hit in way 3 | ways hold 1 2 5 4
LRU : reads=11 hits=6
random: reads=11 hits=5
FIFO : reads=11 hits=4Rule 1 at work: the first four rows. Each miss takes the lowest empty way, and no policy is asked.
Rule 2 at work: the row "read 5". The set is full. Blocks 1 and 2 were just used, and block 4 came in after block 3. So block 3 in way 2 is the least recent, and it goes. Blocks 1, 2 and 4 are all read again later, and all hit.
Rule 3 at work: the last two rows. FIFO has 4 hits where LRU has 6. When block 5 arrived, FIFO threw out block 1, the first one in. Block 1 was the busiest block of the trace. Random has 5 hits here. With other random numbers it would have another count.
The lab does not hold the tree. You build it in the practice problem of this page. So here is the same trace through the answer to that problem, as recorded output:
tree read 1: miss into way 0 | ways hold 1 0 0 0 | top=1 left=1 right=0
tree read 2: miss into way 1 | ways hold 1 2 0 0 | top=1 left=0 right=0
tree read 3: miss into way 2 | ways hold 1 2 3 0 | top=0 left=0 right=1
tree read 4: miss into way 3 | ways hold 1 2 3 4 | top=0 left=0 right=0
tree read 1: hit in way 0 | ways hold 1 2 3 4 | top=1 left=1 right=0
tree read 2: hit in way 1 | ways hold 1 2 3 4 | top=1 left=0 right=0
tree read 5: miss into way 2 | ways hold 1 2 5 4 | top=0 left=0 right=1
tree read 1: hit in way 0 | ways hold 1 2 5 4 | top=1 left=1 right=1
tree read 2: hit in way 1 | ways hold 1 2 5 4 | top=1 left=0 right=1
tree read 1: hit in way 0 | ways hold 1 2 5 4 | top=1 left=1 right=1
tree read 4: hit in way 3 | ways hold 1 2 5 4 | top=0 left=1 right=0
tree : reads=11 hits=6Rules 4 and 5 at work: the row "read 5". The bits before it were top=1, left=0, right=0, from the row above. They point at the right pair and at way 2: the same victim as LRU. On this trace the tree gets 6 hits, as many as LRU, with three bits.
This lab leaves out everything but one set. A real cache keeps these bits for every set, and updates them in the same cycle as the hit.
Why It Is Built This Way
| Policy | Bits for a set of 4 ways | Bits for a set of 8 ways | Changes on a hit? | What it buys, and costs |
|---|---|---|---|---|
| LRU | 5 | 16 | yes, the whole order | The best guess from the past. A lot of logic in the hit path. |
| Pseudo-LRU tree | 3 | 7 | yes, the bits on one path | Close to LRU. Sometimes a block that is not the oldest goes. |
| FIFO | 2 | 3 | no | A counter and nothing else. A busy block is thrown out when its turn comes. |
| Random | 0 | 0 | no | One shared number generator. No worst trace, and no use of locality. |
LRU must tell 24 orders of four ways apart, which takes 5 bits. For eight ways there are 40,320 orders, which takes 16 bits.
The numbers explain what real caches do. With two ways, LRU is one bit, and everybody uses it. With four ways or more, exact LRU costs more than it gains, and the tree takes its place.
The Trap: The Model That Kept Exact LRU
A verification engineer writes a model of the cache in software. The model keeps a list of blocks in order of use, which is exact LRU. The hardware has the tree. The two agree for thousands of reads, and then a test fails with a miss that the model calls a hit. Here is the trace of the hand example under exact LRU:
LRU read 1: miss into way 0 | ways hold 1 0 0 0
LRU read 2: miss into way 1 | ways hold 1 2 0 0
LRU read 3: miss into way 2 | ways hold 1 2 3 0
LRU read 4: miss into way 3 | ways hold 1 2 3 4
LRU read 1: hit in way 0 | ways hold 1 2 3 4
LRU read 5: miss into way 1 | ways hold 1 5 3 4
LRU read 3: hit in way 2 | ways hold 1 5 3 4
LRU : reads=7 hits=2And here it is through the tree:
tree read 1: miss into way 0 | ways hold 1 0 0 0 | top=1 left=1 right=0
tree read 2: miss into way 1 | ways hold 1 2 0 0 | top=1 left=0 right=0
tree read 3: miss into way 2 | ways hold 1 2 3 0 | top=0 left=0 right=1
tree read 4: miss into way 3 | ways hold 1 2 3 4 | top=0 left=0 right=0
tree read 1: hit in way 0 | ways hold 1 2 3 4 | top=1 left=1 right=0
tree read 5: miss into way 2 | ways hold 1 2 5 4 | top=0 left=1 right=1
tree read 3: miss into way 1 | ways hold 1 3 5 4 | top=1 left=0 right=1
tree : reads=7 hits=1The two part at the row "read 5". LRU puts block 5 into way 1, and the tree puts it into way 2. One read later, block 3 is a hit for LRU and a miss for the tree. Rule 6 said this could happen.
Neither is wrong. They are two different policies, and the hardware is the one that counts. A model of a cache must copy the replacement policy bit for bit. If it does not, its hits and misses drift away from the real ones.
At Scale
This section looks ahead. The policies are the same in real caches, and the bookkeeping is repeated for every set.
Eight ways
A tree for eight ways has seven bits in three layers: one bit, then two, then four. A use turns three of them.
Where the bits live
The tree bits change on almost every read. They are kept in flip-flops or in a small fast memory beside the tags.
Random in large caches
In a large cache the gap between random replacement and LRU becomes small, and random is far simpler.
Dirty blocks
A victim that was written must be saved before it goes. A later lesson of this module is about that.
Try It
Run the lab. Follow the LRU rows, and check the victim of the read of block 5.
Work out by hand which block FIFO replaces when block 5 arrives. Then count its hits.
Change the last read from 4 to 3. Predict the new hits of LRU and of FIFO.
LRU read 1: miss into way 0 | ways hold 1 0 0 0 LRU read 2: miss into way 1 | ways hold 1 2 0 0 LRU read 3: miss into way 2 | ways hold 1 2 3 0 LRU read 4: miss into way 3 | ways hold 1 2 3 4 LRU read 1: hit in way 0 | ways hold 1 2 3 4 LRU read 2: hit in way 1 | ways hold 1 2 3 4 LRU read 5: miss into way 2 | ways hold 1 2 5 4 LRU read 1: hit in way 0 | ways hold 1 2 5 4 LRU read 2: hit in way 1 | ways hold 1 2 5 4 LRU read 1: hit in way 0 | ways hold 1 2 5 4 LRU read 4: hit in way 3 | ways hold 1 2 5 4 LRU : reads=11 hits=6 random: reads=11 hits=5 FIFO : reads=11 hits=4Check Yourself
Common Mistakes
Wrong Treat pseudo-LRU as LRU in a model or a test.
The tree keeps three bits, not the full order, and sometimes picks another way.
Fix Model the tree bit for bit, with the same meaning of 0 and 1.
Wrong Update the replacement bits on a miss only.
A hit is a use too. Without it the policy turns into FIFO.
Fix Turn the bits away from the way on every hit and on every block that comes in.
Wrong Ask the policy while a way of the set is still empty.
It would throw out a good block for nothing.
Fix Fill an empty way first. Ask the policy only when the set is full.
Remember
- A full set must give up a block. The replacement policy picks the victim.
- LRU replaces the block used longest ago. It needs the full order of the ways, changed on every hit.
- FIFO and random ignore hits and need almost no bits.
- A pseudo-LRU tree keeps three bits for four ways. Every use turns the bits on its path away.