Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Locality of reference is the tendency for a program to access the same data or instructions again soon, or to access addresses near ones it has just used. Those patterns are called temporal locality and spatial locality. Caches take advantage of both: they keep recently used items close to the processor and fetch data in blocks that include neighboring addresses.

What does locality of reference mean?

Locality of reference describes how a program’s memory and instruction accesses tend to cluster over time or across address space. It is a common pattern, not a guarantee that every access will be predictable. Cornell University’s CS 3410 cache notes and the University of Texas at Austin’s CS429 cache material explain the principle through recurring accesses in program loops.

What are temporal and spatial locality?

Type What tends to happen Example How a cache uses it
Temporal locality The same item is accessed again soon. A loop reuses an accumulator or executes the same instructions repeatedly. Keep recently accessed items available for reuse.
Spatial locality Addresses near a recently accessed address are accessed soon. A program reads consecutive elements of an array. Fetch a block containing the requested address and nearby addresses.

The distinction is the kind of pattern being predicted: temporal locality concerns when an item may be reused; spatial locality concerns where a later access may occur.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

How one loop shows both types

int sum = 0;
for (int i = 0; i < n; i++) {
    sum += a[i];
}

The loop repeatedly uses sum and executes its instructions, illustrating temporal locality. If the array elements are stored contiguously, the accesses to a[i], a[i+1], and subsequent elements illustrate spatial locality. Cornell’s cache notes and MIT OpenCourseWare’s Computation Structures annotated slides describe why repeated instructions and nearby data are useful patterns for caches.

How caches exploit locality

A cache is a small, fast storage layer between the processor and larger, slower memory. Temporal locality gives the cache a reason to retain recently accessed data or instructions: the program may need them again soon. Spatial locality gives it a reason to transfer a block rather than just one address, bringing nearby data along in anticipation of subsequent accesses. MIT’s slides describe this movement as transferring blocks from DRAM into SRAM.

These strategies work well when a program exhibits the corresponding access patterns. Locality itself does not ensure that a particular access will be a cache hit, nor does it establish a fixed performance gain for every program.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Is sequential access the same as spatial locality?

No. Sequential access is a particularly regular form of spatial locality: after accessing address s, a program accesses s + 1 soon afterward. Spatial locality is broader; nearby addresses may be accessed without following a strictly sequential order. The University of Massachusetts Amherst’s basic cache tutorial describes sequentiality as a restricted spatial pattern.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.