Define Locality of Reference
In computer systems, locality of reference (also called the principle of locality), is the observed behavior that a processor is more likely to access certain memory locations again soon or access nearby locations after an access. This matters because caches and other performance optimizations rely on predicting which data/instructions will be needed next. A standard split is into temporal locality and spatial locality.2
Footnotes
-
Locality of reference - Wikipedia - Defines locality of reference and temporal/spatial locality. ↩
-
L14: The Memory Hierarchy - Explains locality as a basis for cache hit prediction and nearby-address expectations. ↩
Locality of Reference in Cache Memory (Spatial vs Temporal)
Core definition (what it means)
Locality of reference is the tendency of a program (during execution) to reference the same memory locations repeatedly over a short period, rather than accessing memory uniformly at random. Informally:
- If you access address at time , then you’re likely to access again at a later time soon (temporal reuse).
- If you access address at time , you’re likely to access addresses near soon (spatial reuse), since neighboring elements are often fetched together into cache line/block structures.
Because caches move data/instructions from slower main memory into faster storage in blocks, spatial locality implies that once one element is fetched, nearby elements are also likely to be available without additional long-latency memory accesses.
Footnotes
-
Locality of reference - Wikipedia - Defines locality of reference and temporal/spatial locality. ↩ ↩2 ↩3 ↩4
Types you should be able to state precisely
-
Temporal locality
Reuse of the same data/instructions within a relatively small time window (e.g., loop variables repeatedly used across iterations).2 -
Spatial locality
Use of data elements in relatively close memory locations (e.g., array traversal).2 -
Sequential locality
A special case of spatial locality: linear/stride-1 access patterns like walking an array from index 0 to . -
Locality principle
Designers exploit locality to make the memory hierarchy efficient: upper levels of the hierarchy are smaller/faster, so good locality keeps relevant blocks there longer and avoids evicting useful data too soon.
Footnotes
-
Locality of reference - Wikipedia - Defines locality of reference and temporal/spatial locality. ↩ ↩2 ↩3 ↩4
-
1.7.2 Cache Memory - Locality of reference - Engineering LibreTexts - Separates temporal vs spatial locality and explains cache placement rationale. ↩ ↩2
How to recognize locality of reference in code (temporal vs spatial)
- 1Step 1
Look for loops, counters, and frequently reused variables—these indicate temporal locality.
- 2Step 2
Look for array accesses with small strides (e.g., iterating or walking structures in order)—these indicate spatial locality.
- 3Step 3
Cache fills typically bring in a block/line that contains the referenced address and nearby bytes/words; spatial locality increases the chance those neighboring elements are used before eviction.
Footnotes
-
Locality of reference - Wikipedia - Defines locality of reference and temporal/spatial locality. ↩
-
- 4Step 4
Conditional code can still show locality if the program revisits the same hot paths/working sets repeatedly (phases). Working-set thinking formalizes that locality occurs in phases.
Footnotes
-
WORKING SET - the denning institute - Discusses Denning’s working set view: references cluster into phases/locality sets. ↩
-
- 5Step 5
If temporal + spatial locality are strong, you expect higher hit rates and fewer expensive misses; if accesses are random/irregular, you expect more misses.
Footnotes
-
L14: The Memory Hierarchy - Explains locality as a basis for cache hit prediction and nearby-address expectations. ↩
-
Why locality is central to memory performance
Caches work because locality makes it likely that data requested now will be requested again soon (temporal) and that nearby data will already be present after a fill (spatial). In the memory hierarchy, upper levels (caches, registers) are faster but smaller; good locality helps ensure the needed working data stays in those small fast levels rather than being constantly evicted.
Key takeaway: locality of reference is not just a definition—it is an empirical property that allows systems to predict and reduce average memory access time.2
Footnotes
-
Locality of reference - Wikipedia - Defines locality of reference and temporal/spatial locality. ↩ ↩2 ↩3
-
L14: The Memory Hierarchy - Explains locality as a basis for cache hit prediction and nearby-address expectations. ↩
Pro Tip: Use locality as a debugging lens
When performance is poor, ask: “Is my code revisiting the same addresses soon (temporal) or scanning nearby memory (spatial)?” Poor locality often looks like cache misses and high latency.2
Footnotes
-
L14: The Memory Hierarchy - Explains locality as a basis for cache hit prediction and nearby-address expectations. ↩
-
Locality of reference - Wikipedia - Defines locality of reference and temporal/spatial locality. ↩
Warning: “Random access” may break the assumption
Locality of reference is probabilistic, not guaranteed. Irregular access patterns (e.g., pointer-chasing with unpredictable targets) can significantly weaken spatial/temporal locality, lowering cache effectiveness.
Footnotes
-
Locality of reference - Wikipedia - Defines locality of reference and temporal/spatial locality. ↩
From locality observation to system design
Working-set thinking
1960sDenning’s working set model formalizes locality as dynamically clustered references to a subset over time, occurring in phases and transitions."
Footnotes
-
WORKING SET - the denning institute - Discusses Denning’s working set view: references cluster into phases/locality sets. ↩
Exploiting locality in hardware
Cache eraCache hierarchies assume repeated accesses and nearby reuse to convert slow memory traffic into faster hits.2"
Footnotes
-
Locality of reference - Wikipedia - Defines locality of reference and temporal/spatial locality. ↩
-
L14: The Memory Hierarchy - Explains locality as a basis for cache hit prediction and nearby-address expectations. ↩
Program analysis & locality-aware coding
Modern optimizationCompilers and programmers restructure loops/data layouts to improve locality, increasing the fraction of accesses served in upper cache levels."
Footnotes
-
Locality of reference - Wikipedia - Defines locality of reference and temporal/spatial locality. ↩
Locality patterns → expected cache behavior (qualitative)
Strong locality increases likelihood of cache hits; weak locality increases misses.
Common clarifications
Knowledge Check
Which statement best defines locality of reference?