Computer engineeringLevel 5 · Research Engineer
Memory hierarchy and caches
Fast memory is small and expensive, large memory is slow, so computers stack them: registers, several levels of cache, main memory and storage. Programs reuse recent data (temporal locality) and touch nearby data (spatial locality), so a small cache can serve most accesses. A cache splits each address into tag, index and offset bits. Direct-mapped caches give each block one possible line; set-associative caches give it several, which avoids conflict misses at the cost of more comparators. Average memory access time is hit time plus miss rate times miss penalty, and it often decides whether a program is fast or slow more than the arithmetic does.
Why it mattersRearranging a loop so it walks memory in order can make the same program on the same chip several times faster.