Caches
The memory wall, temporal and spatial locality, tag/index/offset and associativity, AMAT, miss kinds, write-back/write-through, and what a program can do with traversal order.
The memory wall
Registers and the stack treated memory as uniform. Real DRAM is not: an L1 hit is ~4 cycles; a miss that reaches DRAM costs 50–200.
Notation. Blocks of bytes; sets of ways; capacity . Address → tag / index / offset. Hit time , miss rate , miss penalty .
In 1985 a DRAM access ≈ one 80286 cycle (~200 ns vs ~166 ns); by 2015 Core i7 cycles were ~0.33 ns while DRAM latency had fallen only about 10× to ~20 ns — about 60 cycles of chip latency, and a miss through the memory controller costs 50–200 cycles. Wulf and McKee (1995) named the widening gap the memory wall. Typical hierarchy (Bryant & O’Hallaron Core i7 figures): registers → L1 32 KiB / 4 cycles → L2 256 KiB / 10 → shared L3 8 MiB / 40–75 → DRAM → SSD (25–75 µs, ~ cycles at 3 GHz). An Apple M1 performance core reports 128 KiB L1D, 12 MiB L2 shared by four P-cores, and 128-byte lines — sizes vary; the pattern of “each level slower and larger” does not.
Locality
Key idea. Keep recently used blocks in a small fast memory. On a miss, fetch the whole block — neighbours are likely next.
Temporal locality: loop code, counters, accumulators, the top of the stack. Spatial: sequential arrays, adjacent struct fields, consecutive instruction fetch. Summing 1024 ints with 64-byte lines: 16 ints/line → first access misses, next 15 hit → compulsory miss rate , hit rate 93.75%, without rereading any element. Stacking several such levels gives the hierarchy: average cost near the fast level, capacity of the slow one.
Organization: tag, index, offset
Direct-mapped (), -way set-associative, fully associative (). Offset = low bits; index = next ; tag = the rest. Lookup: index selects one set; all ways’ tags compare in parallel with the address tag (only if ); on a hit the offset picks bytes inside the block.
Worked. Address 0x1234ABCC with (64 B lines, 64 sets): offset 12, index 47, tag 0x1234A. A 16 KiB 4-way cache with 64 B lines has ; a 32 KiB 8-way L1 with 64 B lines also has . Same capacity, 256 blocks of 64 B: direct-mapped needs 1 comparison and 18 tag bits; 4-way needs 4 comparisons and 20 tag bits; fully associative needs 256 comparisons and 26 tag bits — affordable only for small structures such as TLBs.
Conflict example. Two int arrays of 1024 elements lying exactly 16 KiB apart thrash a 16 KiB direct-mapped cache: every a[k]/b[k] pair shares an index → 2048 misses. A 2-way cache of the same size keeps both blocks and misses only first touches (128). Replacement among full ways is usually LRU (or an approximation / random at high ).
AMAT and miss kinds
With , , , AMAT ≈ 10.25 cycles — far closer to a hit than to raw DRAM. A miss rate of 3% at gives AMAT 10; 1% gives 6 — two points of hit rate cut average time by 40%.
Multi-level: of L1 is L2’s AMAT. Toy numbers: L1 , ; L2 , local ; DRAM 200 → cycles, versus 14 without L2. Global miss rate .
Three Cs (Hill & Smith): compulsory (first touch — infinite cache still has them), capacity (working set > — fully associative LRU of same size minus compulsory), conflict (too many blocks in one set — real cache beyond fully associative). Larger , larger , more , or a better access order each attack different Cs.
Writes and code
Write-through: every store updates cache and next level (write buffer helps). Write-back: update cache, set dirty; write next level on dirty eviction. Miss on store: write-allocate vs no-write-allocate. Usual pairs: WB+WA, WT+NWA. A counter incremented 1000 times: WT sends 1000×4 B to the next level; WB sends one dirty 64 B line once on eviction.
Code. Row-major ints (16 KiB): each row is 256 B = 4 lines; row-major miss rate 6.25% at every reasonable size (all compulsory); column-major ~100% until the cache holds 64 lines of a column — fully associative needs 4 KiB; direct-mapped needs 16 KiB because stride-256 addresses hit only every fourth set. Same , different CPI. Measured on M1: int sum 0.021 s by rows vs 0.137 s by columns (~6.5×) at -O0. Blocking tiles so three working sets fit in cache.
Next in the hierarchy: Virtual memory — every process’s private address space, pages, and how translation meets the cache.
VIPT L1 caches index with bits that sit inside the page offset so set selection overlaps the TLB and can proceed in parallel with translation — a detail the next topic makes precise. Inclusive vs exclusive hierarchies change whether an L3 hit implies the line is also in L1; either way, AMAT still composes level by level from the core outward.
Check your understanding
The same questions as in Pulse. Answers are shared between modes.
Go deeper — optional
Code and labs do not count toward the main percentage or the certificate.
- Direct-mapped cache hit/miss traceoptional
On an address trace compute tag/index/offset; mark hit/miss and conflict victim.
~25 min · easier on a computer · The matching course topic has been read.
Self-check in the notebook — not part of mastery or the certificate.
Answers are saved on the server and tied to this browser until you sign in.