The Runtime Theory
Cache and Memory Hierarchy

Why Memory Locality Matters

Processors use a hierarchy of storage because small, nearby storage can be accessed faster than large, distant storage.

The Runtime Theory Team5 min read#cache#memory#locality
▸ On this page

The model

Processors use a hierarchy of storage because small, nearby storage can be accessed faster than large, distant storage. A cache keeps copies of recently or predictably used memory blocks. Programs benefit when accesses show temporal locality, reusing data, or spatial locality, touching nearby addresses.

A concrete walk-through

A row-major matrix traversal that increments the inner column index typically consumes adjacent elements, while stepping through a column may jump by an entire row stride. The values are mathematically identical, but the second access pattern may fetch cache lines that contain mostly unused data.

Costs and failure cases

A cache hit avoids a slower level; a miss fetches a block and may evict another useful block. Capacity, associativity, and line size shape conflict behavior. Caches improve typical access time but make performance depend on working-set size and access order.

Check your understanding

Rewrite a matrix transpose loop so reads are contiguous. Then reason about the writes: why can optimizing one side still leave the other side with poor locality?

Further reading

CS:APP Cache Lab

Not started

Sign in to save your learning progress.

Sign in to save