liveBook · Manning

Temporal locality

As explained in 100 Go Mistakes and How to Avoid Them

Temporal locality is the tendency for a recently accessed memory location to be accessed again soon.

Temporal locality means that a recently accessed memory location is likely to be accessed again soon. It works with spatial locality as part of locality of reference and helps explain why CPU caches speed repeated accesses. section 12.1.2

Repeated loop accesses

In a slice-summing loop, the variables i, length, and total are accessed throughout the iteration, giving them temporal locality. The slice elements also benefit from spatial locality because the backing array is contiguous. section 12.1.2

Temporal locality motivates caching repeated values, while spatial locality explains why the CPU fetches a whole cache line rather than one variable. A typical 64-byte line holds eight int64 values. section 12.1.2

How 100 Go Mistakes and How to Avoid Them covers it

How deep it goes
Connects repeated accesses to cache lines and cache levels, but stops short of detailing cache replacement decisions.
Examples
Uses a Go slice-summing loop and repeated accesses to i, length, and total as examples.
What you can do after
Evaluate how repeated variable accesses and cache-line fetching influence memory-access performance.
How it connects
Connects temporal locality with spatial locality, locality of reference, CPU caches, and cache lines.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 Go Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.