What is Spatial locality?
The tendency to access addresses near one just accessed, as when reading an array in order. It is why caches fetch data a whole cache line at a time.
Field: Memory Hierarchy
Read the beginner explainer: How Does CPU Cache Work? Hit Rate, Lines and Locality
Japanese name: 空間的局所性
Why programs have it
Instructions are fetched one after another from consecutive addresses until a branch, arrays are commonly walked element by element, and the fields of a structure or the local variables in a stack frame sit side by side. As a result, an access to one address is often followed soon by accesses to neighboring addresses. Spatial locality is this tendency, and it is a property of the program's access pattern rather than of the hardware.
How hardware exploits it
Caches move data in lines, 64 bytes in many CPUs, so one miss brings in neighbors that will likely be used next. This also fits DRAM bursts: in DDR4, eight transfers of 8 bytes deliver exactly 64 bytes. Hardware prefetchers detect sequential or constant-stride streams and fetch upcoming lines ahead of time, and DRAM rows left open serve nearby accesses faster. Pages and the TLB benefit as well, since one translation covers many nearby addresses.
Spatial vs. temporal locality
Temporal locality means reusing the same data soon, such as a loop counter or a running sum; spatial locality means using nearby data soon, such as the next array element. A single pass over a large array has strong spatial but no temporal locality, while repeatedly updating one variable has temporal but little spatial locality. Random accesses to a large data set, as in some hash tables or graph traversals, have little of either and miss frequently.
Writing code that has it
In C, two-dimensional arrays are stored row-major, so iterating the inner loop over columns touches consecutive addresses while swapping the loops jumps a full row each step. Choosing a structure-of-arrays layout when only one field is processed avoids loading unused fields. Larger cache lines exploit spatial locality better but raise miss penalty and can load unneeded data, and in multithreaded code unrelated variables sharing one line cause false sharing.
Try it: questions about Spatial locality
- ATo retain data when the power is turned off
- BTo keep frequently used data close to the CPU and bridge the speed gap with main memory
- CTo correct errors in computation results
Show answer and explanation
Answer: B. To keep frequently used data close to the CPU and bridge the speed gap with main memory
While one clock cycle of a CPU running at several GHz lasts 0.2–0.3 ns, a DRAM access takes tens to 100 ns—hundreds of cycles. Frequently used data and instructions are therefore kept in fast SRAM caches to cut waiting. Caches exploit temporal locality (reusing the same data soon) and spatial locality (using nearby addresses next), with L1 reachable in a few cycles and even L3 in tens of cycles.
- A1 byte
- B64 bytes
- C64 MB
Show answer and explanation
Answer: B. 64 bytes
Most CPUs transfer data between cache and main memory in 64-byte lines. Neighboring addresses are often used together (spatial locality), so fetching them as a block is efficient, and it matches DRAM bursts (8 transfers × 8 bytes = 64 bytes in DDR4). In multithreaded code, beware of false sharing, where separate variables in the same line cause cores to contend for it.
FAQ
What is an example of spatial locality?
Reading an array from beginning to end: after element i is used, element i+1 at the next address is used, and it was likely brought into the cache by the same line fill.
How do caches take advantage of spatial locality?
They transfer whole cache lines, commonly 64 bytes, on each miss and often prefetch the following lines, so neighboring data is already present when the program asks for it.
Why is column-wise traversal of a C array slow?
C stores arrays row by row, so stepping down a column jumps a full row each access and uses only a small part of each cache line fetched.
Books on Memory Hierarchy Ad
As an Amazon Associate I earn from qualifying purchases.
This page was drafted with AI and fact-checked by a separate AI review before publication. Our Editorial policy explains how we write and check content. If you spot an error, please tell us on our Contact page.
Back to the glossary