Unit content
Spatial and temporal memory locality
Programs are faster when their memory-access patterns cooperate with the cache hierarchy. Two especially important patterns are temporal locality and spatial locality.
Temporal locality
If a value is used repeatedly within a short period, keeping it in cache avoids repeatedly fetching it from slower memory.
Algorithms with a small active working set often benefit from this form of reuse.
Spatial locality
When one memory location is accessed, the surrounding cache line is fetched too. Sequential access to neighboring array elements therefore tends to use most of the data already brought into cache.
Pointer-heavy traversal can instead jump between unrelated locations and waste much of each fetched cache line.
Traversal order matters
Two algorithms can perform the same number of arithmetic operations while producing very different cache behaviour because they visit data in different orders.
Blocking or tiling techniques reorganize work so that a small region of data is reused before moving on.
Locality is workload-dependent
Contiguous memory is not automatically optimal for every problem. The important question is which data is used together and how frequently. Locality turns that access pattern into a design consideration.