Sub topic 2
Temporal and Spatial Locality
This chapter assumes that you know binary numbers, powers of two, byte-addressable memory, and the basic idea that a CPU fetches instructions and data from memory. You should also know that a cache stores recently used blocks of main memory.
Locality is the tendency of a program to reuse the same memory locations or to access nearby locations. Caches exploit this tendency because a small, fast cache can hold the data that the CPU is likely to need soon.
Temporal locality
Temporal locality means that a memory location used recently is likely to be used again soon.
For example, a loop repeatedly updates the same variable:
total = 0
for value in values:
total += valueThe variable total is read and written during every iteration. After the first access, its cache block is likely to remain in the cache. Later accesses can therefore be cache hits.
Instructions also show temporal locality. The instructions inside a loop are fetched repeatedly. A cache can keep those instruction blocks close to the CPU instead of fetching them from slower main memory each time.
Temporal locality depends on the time between uses. If a program accesses thousands of other blocks between two uses of the same block, the first block may have been evicted before it is needed again.
Spatial locality
Spatial locality means that locations near a recently used location are likely to be used soon.
Consider an array traversal:
total = 0
for i in range(len(data)):
total += data[i]Array elements are stored in adjacent memory locations, assuming a normal contiguous array. When the CPU requests one element, the cache usually fetches an entire block containing several neighboring elements. The next elements can then produce cache hits.
Spatial locality also explains why sequential instruction execution works well. Instructions placed next to one another in memory are often fetched together in one cache block.
| Locality type | Main prediction | Typical example |
|---|---|---|
| Temporal | The same location will be reused soon | A loop counter or repeated instruction |
| Spatial | A nearby location will be used soon | Consecutive array elements |
How a cache uses both types
A cache transfers data between main memory and the cache in fixed-size blocks, also called cache lines. A block is larger than one byte so that one access can bring nearby bytes into the cache.
The following sequence has both forms of locality:
A, A, A, B, C, D, B, AThe repeated accesses to A demonstrate temporal locality. If B, C, and D occupy nearby addresses in the same cache block, those accesses demonstrate spatial locality.
Common errors
- Confusing spatial locality with accessing the same location repeatedly. That is temporal locality.
- Assuming every nearby byte is in the cache. Only bytes within the fetched block are guaranteed to be present.
- Ignoring block size. Larger blocks can exploit more spatial locality, but they also transfer unused data and can increase miss penalties.
- Assuming locality guarantees a hit. Mapping conflicts, limited capacity, and other accesses can cause misses.
Exercise
A program reads array elements at addresses 100, 104, 108, and 112. It then reads address 100 again. Which accesses demonstrate spatial locality, and which demonstrate temporal locality?
Show answer
The accesses to 100, 104, 108, and 112 can demonstrate spatial locality if they lie in the same cache block. The second access to 100 demonstrates temporal locality.