Index

Free, with Google

Keep going, free.

A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.

Start free with Google

A chalk plan

Open everything on chalk.

Every lesson, board, video, lab and paper, and the full review decks. Yearly is $39/year.

See plansStart free instead

Module 02 · How Computers Work · The machine runs a program · about 13 min to read

Play it as a journeyTutor comes with a plan

Which shelf the data lands on: cache associativity and writes

The Memory Hierarchy
The Memory Hierarchy · Open board

The small fast caches in front of RAM. This lesson opens one of those boxes and shows where each piece of data is allowed to sit.

A programmer has a grid of 512 rows and 512 columns of 4-byte numbers. They add up one column, top to bottom. Then they change the grid to 513 columns and run the same loop. The loop does the same work on almost the same amount of data. Yet the 512 version is far slower. Nothing in the code explains it. The cause is inside the cache.

By the end of this lesson you will be able to predict when a cache throws out data it still had room for, and say what a cache does when your program writes data instead of reading it.

The history (optional)

A well-known Stack Overflow question asks why transposing a 512 by 512 matrix is much slower than transposing a 513 by 513 one. The answers blame cache associativity, the subject of this lesson.

Before you start. The Memory Hierarchy (cache, locality, the memory wall) gave you the cache itself: a small fast copy of recent memory, moved in 64-byte blocks called cache lines, where a hit is fast and a miss is slow. The CPU (fetch-decode-execute, the ISA, assembly) gave you the clock tick, the unit used for costs below. This lesson answers a question those two left open: when a new line arrives, which spot does it take?

Lesson 7 of 41 in How Computers Work

The rest of this lesson is part of a plan.

A plan opens every lesson, board, video, lab and paper on chalk. Yearly is $39/year.

See plansAlready a member? Sign in

Or start free: a free account opens lessons 2 and 3 of every track.

In this lesson

  • One address, one allowed place
  • Two arrays 32 KB apart: the conflict miss
  • Several shelves per spot: the set, and associativity
  • The stride that lands on one set
  • What a write does: write-through and write-back

Terms it explains: direct-mapped, conflict miss, set, associativity, sets, way, Write-through, Write-back, Fully associative, cache, cache line, DRAM, hit rate, hit/miss, latency vs bandwidth, LRU/associativity, memory wall, MMU/page table, NUMA, prefetch, spatial locality, SRAM, swap/paging, temporal locality, TLB, virtual memory, working set.

Lab

Same data, same O(n), four access patterns. Why is one of them 31 times slower?

Pick Following a chain of pointers, drag Memory the loop walks from end to end, and watch The bill jump.

Open The memory hierarchy

Related paper

Review How Computers Work