Memory and Caches in One Picture
A large memory is slow, and a processor asks it something in almost every cycle. A cache keeps copies of recently used boxes close by.
You Will Be Able To
- Tell ROM, RAM and flash apart, and say why a large memory answers slowly.
- Split an address into a line number and a tag, and decide hit or miss by hand.
- Explain when a cache helps, and when it does not.
- Lesson
- S0.7
- Time
- 25 min
- Runs on
- Your browser
- Check
- 5 questions
S0.6 counted the bus transfers of a small program: 9 of 13 were fetches from memory. Now make that memory large enough for real software. It becomes slow, and the processor waits on almost every instruction.
Before You Start
You need S0.1: memory boxes and addresses. You need S0.6: fetch, load and store. You also need binary numbers, to split an address into two groups of bits.
| Question | |
|---|---|
| 1 | How do ROM, RAM and flash differ? |
| 2 | Why does a large memory answer more slowly than a small one? |
| 3 | What is a cache, and what are a hit and a miss? |
| 4 | How does a cache know whose copy a line holds? |
| 5 | When does a cache save time, and when does it save none? |
The Idea: Keep a Copy Close
A chip holds more than one kind of memory, because no single kind does every job.
| Kind | Keeps its content with the power off? | Can a program write it? | Typical job |
|---|---|---|---|
| ROM (read-only memory) | yes | no: its content is fixed when the chip is made | the first program a chip runs |
| RAM (random-access memory) | no | yes, as fast as it reads | the working data of a program |
| Flash | yes | yes, but slowly, and a whole block must be erased first | the program of a product, which may be updated |
The Boot ROM and the SRAM of ap_soc, met in S0.2, are a ROM and a RAM.
All three share one problem: size costs speed. S0.1 gave the reason. Picking one box of sixteen takes a small multiplexer. Picking one of millions takes many more gates in a row, and far longer wires. A large memory may need many cycles for one read.
Programs offer a way out. They use the same few addresses again and again: a loop fetches the same instructions on every round. So a chip puts a small, fast memory next to the processor. It keeps copies of the boxes used most recently. That small memory is a cache.
When the processor asks for an address, the cache looks for a copy first. Finding one is a hit, and the answer comes at once. Not finding one is a miss. The cache then reads the large memory, hands the value over, and keeps a copy for next time.
New Words
| Word | Meaning |
|---|---|
| ROM, RAM, flash | The three kinds of memory in the table above. |
| Cache | A small, fast memory that holds copies of recently used boxes of a larger, slower memory. |
| Hit | The cache holds a copy of the address asked for. The answer is fast. |
| Miss | The cache holds no copy. The value comes from the slow memory, and a copy is kept. |
| Cache line | One place in the cache, called a line for short. It holds one copy and remembers whose copy it is. |
| Tag | The part of the address that a line stores beside its copy, to record whose copy it is. |
| Valid bit | One bit for each line. 0 means the line is empty. Every line starts at 0. |
The cache on this page has eight lines. It sits in front of a memory of 64 boxes, so an address has six bits. The cache splits every address in two.
| Bits | Name | Job | Address 8 (binary 001 000) | Address 16 (binary 010 000) |
|---|---|---|---|---|
| bottom three | line number | picks one of the eight lines | 000: line 0 | 000: line 0 |
| top three | tag | stored in the line, and compared on every read | 001: tag 1 | 010: tag 2 |
Every address has exactly one line it may use. Eight different addresses share each line, and the tag says which one is there now.
One Picture: Miss, Hit, Replace
- Processor
- Memory
- Without a cache
Every read walks to the large memory and waits 10 cycles for the answer.
- Add a small memory in between
The cache has only eight lines, so it answers in one cycle. Each line can hold a copy of one memory box.
- Read box 8: a miss
The cache starts empty, so it has no copy. It reads box 8 from memory, which takes 10 cycles. It keeps a copy in line 0.
Follow the calls, values and waits
- data 108: Memory → Cache
- Read box 8 again: a hit
Line 0 holds the copy. The cache answers in one cycle, and memory is not asked at all.
Follow the calls, values and waits
- data 108: Cache → Processor
- Read box 16: a miss that replaces
Box 16 also belongs in line 0, and the copy there is of box 8. The cache reads box 16 from memory and overwrites the line.
Follow the calls, values and waits
- data 116: Memory → Cache
Read the diagram as text
- Processor: asks for one address at a time
- Cache: 8 lines: answers in 1 cycle
- Memory: 64 boxes: answers in 10 cycles
- Line 0 of the cache: a valid bit, a tag and one copy
- Processor → Cache: the address
- Cache → Processor: the value
- Cache → Memory: only on a miss
- Memory → Cache: the value, kept as a copy
- Cache → Line 0 of the cache: the address picks the line
In this example box n holds the number 100 + n. The costs of 1 and 10 cycles are choices of this page.
The processor never changed what it does. It asks for an address and waits for the value. The cache only changes how long the wait is.
The Smallest Example, Worked by Hand
A program reads boxes 8, 9 and 10, twice round. Then it reads box 16, and then box 8 again. The cache starts empty. For each read, split the address, look at that line, and compare the tag.
| Read | Address in binary | Line | Tag | What the line held | Result | Cycles |
|---|---|---|---|---|---|---|
| box 8 | 001 000 | 0 | 1 | empty | miss | 10 |
| box 9 | 001 001 | 1 | 1 | empty | miss | 10 |
| box 10 | 001 010 | 2 | 1 | empty | miss | 10 |
| box 8 | 001 000 | 0 | 1 | tag 1 | hit | 1 |
| box 9 | 001 001 | 1 | 1 | tag 1 | hit | 1 |
| box 10 | 001 010 | 2 | 1 | tag 1 | hit | 1 |
| box 16 | 010 000 | 0 | 2 | tag 1 | miss: replaces the copy of box 8 | 10 |
| box 8 | 001 000 | 0 | 1 | tag 2 | miss: replaces the copy of box 16 | 10 |
Three hits and five misses cost 3 + 50 = 53 cycles. With no cache, eight reads cost 80. The second round of the loop was almost free. The last two reads show the weak spot: boxes 8 and 16 want the same line.
The Rules
Rule 1: A cache holds copies.
The large memory still holds every box. The cache holds a few of them a second time, close to the processor.
Rule 2: The address picks the line.
The bottom bits of the address are the line number. Each address has exactly one line it may use.
Rule 3: A read is a hit when the line is valid and its tag equals the tag of the address.
Then the copy is the answer, and the large memory is not asked.
Rule 4: Any other read is a miss.
The value comes from the large memory. The line then takes the copy and the new tag, and its old content is lost.
Rule 5: A cache saves time only when addresses repeat.
The first read of any box is always a miss.
Sources for These Rules
The textbook "Computer Organization and Design" by Patterson and Hennessy. Why a small memory in front of a large one works, and the habit of programs to reuse addresses: section 5.1. Memory technologies: section 5.2. The cache with one possible line for each address, its tag and its valid bit: section 5.3.
ROM, RAM and flash as circuits: the textbook "Digital Design and Computer Architecture" by Harris and Harris, section 5.5. Caches: the same book, section 8.3.
The textbooks call the line number the index, and call this kind of cache direct-mapped. The eight-line cache, the 64-box memory and the costs of 1 and 10 cycles are our own teaching example.
Now Run It
The lab at the end of this page is this cache in SystemVerilog, in front of a 64-box memory. It has no clock. It adds up the cycles that each read would cost: 1 for a hit and 10 for a miss.
read 8 line=0 tag=1 miss value=108 (the line was empty)
read 9 line=1 tag=1 miss value=109 (the line was empty)
read 10 line=2 tag=1 miss value=110 (the line was empty)
read 8 line=0 tag=1 hit value=108
read 9 line=1 tag=1 hit value=109
read 10 line=2 tag=1 hit value=110
read 16 line=0 tag=2 miss value=116 (replaces the copy of box 8)
read 8 line=0 tag=1 miss value=108 (replaces the copy of box 16)
8 reads: 3 hits, 5 misses, 53 cycles (80 with no cache)Compare it with your hand table. Rule 3 at work: the fourth, fifth and sixth rows are hits, because the line is valid and the tags match. Rule 4 at work: the read of box 16 replaces the copy of box 8, and the last row pays for it.
The last row is the bill: 53 cycles against 80. Every value is still correct. A cache never changes what a read returns. It changes only how long the read takes.
This lab leaves out writes. A store must reach the copy and the large memory, in some order. Later lessons on caches cover that.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| A small memory in front of a large one | One memory, large and fast | Speed close to the small one, at the size of the large one. A large fast memory cannot be built at a sane cost. |
| The address picks the line | Search every line for a copy | One line to look at and one tag to compare, so a hit is quick. Two busy addresses may fight over one line. |
| Store a tag beside each copy | Store the whole address | Fewer bits in each line. The line number is already known from where the copy sits. |
| Keep the newest copy on a miss | Keep the old copy | The address just used is the one most likely to be used again. |
The Trap: Two Boxes, One Line
A program reads box 8 and box 16 in turn, three times. The cache has eight lines and seven of them are empty. It still does not help at all.
read 8 line=0 tag=1 miss value=108 (the line was empty)
read 16 line=0 tag=2 miss value=116 (replaces the copy of box 8)
read 8 line=0 tag=1 miss value=108 (replaces the copy of box 16)
read 16 line=0 tag=2 miss value=116 (replaces the copy of box 8)
read 8 line=0 tag=1 miss value=108 (replaces the copy of box 16)
read 16 line=0 tag=2 miss value=116 (replaces the copy of box 8)
6 reads: 0 hits, 6 misses, 60 cycles (60 with no cache)Every read is a miss. Both boxes map to line 0, so each read throws out the copy that the next read needs. The bill is 60 cycles, the same as with no cache. Engineers who meet a slow loop on a fast chip learn to check for this.
At Scale
This section looks ahead. A phone chip has several caches of many thousand lines each. The ideas of this page carry over unchanged.
Longer lines
A real line holds several neighbouring bytes, often 64. Programs tend to use neighbours soon after each other, so one miss brings in data for several later hits.
More than one line to choose from
Large caches let each address use one of several lines. That softens the trap above, and costs more comparing.
Caches behind caches
A large chip puts a small, very fast cache next to each processor, and larger, slower ones behind it. The lesson The Cache Hierarchy of a Core Cluster shows the layout.
No cache at all
A small chip, such as the one in a thermostat, often has none. Its memory is small and on the chip, so it answers quickly anyway.
Try It
Run the lab. Match each printed row to one row of your hand table.
Add a ninth read, of box 9. Predict hit or miss before you run it.
Change SLOW to 50. Predict the two numbers of cycles in the last row.
Replace the read of box 16 by a read of box 17. Predict which later read changes from a miss to a hit.
read 8 line=0 tag=1 miss value=108 (the line was empty) read 9 line=1 tag=1 miss value=109 (the line was empty) read 10 line=2 tag=1 miss value=110 (the line was empty) read 8 line=0 tag=1 hit value=108 read 9 line=1 tag=1 hit value=109 read 10 line=2 tag=1 hit value=110 read 16 line=0 tag=2 miss value=116 (replaces the copy of box 8) read 8 line=0 tag=1 miss value=108 (replaces the copy of box 16) 8 reads: 3 hits, 5 misses, 53 cycles (80 with no cache)Check Yourself
Common Mistakes
Wrong Expect the first read of a box to be a hit.
The cache holds only copies of boxes that were read before.
Fix Count the first read of every box as a miss.
Wrong Judge a hit by the line number alone.
Eight addresses share each line. The line may hold a copy of a different box.
Fix Check the valid bit, then compare the stored tag with the tag of the address.
Wrong Assume a nearly empty cache cannot miss.
Two addresses with the same line number replace each other, however empty the other lines are.
Fix Look at which lines the busy addresses use, not at how full the cache is.
Remember
- ROM keeps its content and cannot be written; RAM can be written and forgets; flash keeps its content and is rewritten slowly.
- A large memory is slow, because picking one box among many takes more logic and longer wires.
- A cache is a small fast memory holding copies of recently used boxes. A hit is fast; a miss fetches and keeps a copy.
- The bottom bits of an address pick the line; the stored tag says whose copy the line holds.
- A cache helps only when addresses repeat, and two busy addresses that share a line can defeat it.