Week 3 · Lesson 3 · 55 minutes

Primary memory and cache

I can explain why a computer uses different memory types.

Planned lesson. Follow your teacher’s teaching order.

Source workbook pages: 20.

Resources

Vocabulary

Vocabulary
volatile: loses stored data when power is removed; cache hit: the requested item is found in cache
55-minute lesson sequence
  1. 0–5 · Retrieve register location.
  2. 5–15 · Model hierarchy and one cache hit/miss sequence.
  3. 15–25 · Guide the first three requests of the two-item LRU model.
  4. 25–45 · Students finish the trace and justify the laptop explanation.
  5. 45–50 · Each student names a claim the evidence does not support, such as all larger caches being faster.
  6. 50–55 · Collect the miss explanation.

See it

Memory levels from the core outwardRegistersCache: L1, L2, L3RAMPersistent storageFaster, nearer the coreLarger capacity
Levels nearer the core are faster and smaller. On a cache miss the data is not lost: it is fetched from the next level down.

Watch

Watch how RAM, cache and long-term storage differ, and why a computer uses all of them.

How computer memory works - Kanawat SenananTED-Ed

Open on YouTube

The video is on YouTube, which needs a VPN on most networks in mainland China. It is extra: the lesson works without it.

Try before notes

Which is nearer the ALU: a register or a file on an SSD?

Build the idea

Registers, cache, RAM and persistent storage solve different problems. Registers are tiny immediate workspaces. Cache keeps copies of recently or frequently useful memory data. RAM holds active programs and data. Persistent storage keeps saved work without power.

A cache hit finds the item in cache. A miss requires a lower-level access; it does not mean that the data vanished. Cache benefits depend on reuse and access patterns. A larger cache is not a promise of the same speed-up for every program.

Our toy cache uses least recently used replacement. After every access, move the accessed item to the most-recent position. On a miss into a full cache, remove the least-recent item first.

registers → cache → RAM → persistent storage
small fast workspace      larger saved data

See the method

Registers hold immediate working values. Cache holds copies likely to be used soon. L1 is normally smaller and nearer the core than L2 and L3. RAM holds active programs and data. ROM is non-volatile storage for firmware in the simplified model. On a cache miss the item must be obtained from a lower level. Real cache layouts differ.

Predict then reveal

A two-item LRU cache starts empty. Trace X,Y,X,Z, showing least→most recent.

Reveal reasoning

X: miss [X]. Y: miss [X,Y]. X: hit [Y,X]. Z: miss, evict Y, leaving [X,Z]. Revisiting X changed its recency.

A student saves a file to SSD, then loses power. Which data should survive: the saved file or unsaved edits held only in RAM?

Reveal reasoning

The saved SSD file should survive normal power loss because SSD storage is non-volatile. Edits held only in volatile RAM are lost. Applications may have autosave, but this scenario says the edits exist only in RAM.

Work one step together

Read the first task. Each learner predicts the first step privately. Compare the method, then complete one intermediate row or one code iteration together. Do not display a finished workbook answer.

Practice

  1. A toy cache holds two items. On a miss it loads the item and evicts the least recently used item if full. Trace requests A,B,A,C,A,B from empty. Mark hit or miss and show the cache from least to most recent.

Your turn

Complete the remaining steps of task 1 alone, then the next tasks. Predict before running code. Save your own evidence before comparing.

Practice

  1. A toy cache holds two items. On a miss it loads the item and evicts the least recently used item if full. Trace requests A,B,A,C,A,B from empty. Mark hit or miss and show the cache from least to most recent.
  2. A laptop has slow repeated calculations and too little RAM for its open files. Explain one role of cache and one role of RAM. Why would adding SSD space alone not make them equivalent?

Check and explain

More SSD capacity does not turn the SSD into RAM or cache.

Point to the first step, row, condition or requirement that supports your answer. If results differ, find the first different state and repair one cause.

Show what you know

Does a cache miss mean the data is lost?

HL challenge when ready

Would every program gain equally from a larger cache?

Week 3 homework

Show working. Keep code and output. Stop at 25 minutes and mark where you reached.

  1. In the toy machine PC=4. Memory 4 contains LOAD 20 and memory 20 holds 9. Trace the fetch and execute.
  2. Explain why cache, RAM and SSD are not interchangeable. Use one job for each.
  3. Six independent instructions use F,D,E,W with no stalls. Find the ideal cycles and compare with no overlap. Write ideal_cycles(n) in Python for n >= 1. Test n = 1, 4 and 6.
  4. Retrieve logic: simplify (A AND B) OR (A AND NOT B). Give one proof step.

Source workbook pages: 23.

Weekly homework (Word)

Python practice · two short prompts

Spend about 3 minutes on each during practice time. Predict, run, change one thing, then explain. These prompts do not add homework.

A starter is runnable code to explore or complete. If the playground restores your saved code, the linked starter is selected in Example: use Load example when ready. Download your work before confirming replacement.

Prompt 1

Count cache hits for this access sequence. Add one new address and predict the count.

No imports needed.

Hint and starter code

A cache hit means the requested item is already in a fast local copy; this fixed set does not model replacement.

# A cache hit means the requested item is already in a fast local copy; this fixed set does not model replacement.
cache = {1, 3}
accesses = [1, 2, 3, 1]
hits = 0
for address in accesses:
    if address in cache:
        hits += 1
print(hits)
Open this starter in Python playground →
Prompt 2

Compare total access cost with and without cache using invented cost units.

No imports needed.

Hint and starter code

These are illustrative units, not measurements of a real machine.

# These are illustrative units, not measurements of a real machine.
hits, misses = 3, 1
hit_cost, miss_cost = 1, 10
print(hits * hit_cost + misses * miss_cost)
print((hits + misses) * miss_cost)
Open this starter in Python playground →