liveBook · Manning

Data locality

As explained in 100 Go Mistakes and How to Avoid Them

Data locality is the tendency for related or recently accessed data to reside near one another in memory or storage.

Avoid treating memory accesses as independent: locality of reference explains why repeated or nearby accesses can benefit from CPU caches. section 12.1.2

Distinguish the two forms of locality:

  • Temporal locality repeats access to the same location, such as repeatedly using a loop index, slice length, or running total. section 12.1.2
  • Spatial locality accesses nearby locations, such as consecutive elements in a slice backed by a contiguous array. section 12.1.2
  • A cache line transfers a fixed contiguous block, usually 64 bytes or eight int64 values, rather than one variable. section 12.1.2
func sum(s []int64) int64 {
    var total int64
    length := len(s)
    for i := 0; i < length; i++ {
        total += s[i]
    }
    return total
}

Trace sequential slice summation: accessing s[0] can bring nearby elements into the cache, allowing later accesses to benefit from spatial locality. section 12.1.2

func sum2(s []int64) int64 {
    var total int64
    for i := 0; i < len(s); i+=2 {     #1
        total += s[i]
    }
    return total
}
 
func sum8(s []int64) int64 {
    var total int64
    for i := 0; i < len(s); i += 8 {   #2
        total += s[i]
    }
    return total
}

Compare sequential traversal with strides of two and eight to inspect how access patterns use data fetched into cache lines. section 12.1.2

How 100 Go Mistakes and How to Avoid Them covers it

How deep it goes
Explains temporal and spatial locality, cache-line transfers, cache levels, and compulsory misses without detailing cache-replacement decisions.
Examples
Uses Go slice-summing functions, sequential access, and stride-two or stride-eight traversal.
What it assumes
Introduces locality through memory-access motivation before defining cache lines and assumes basic familiarity with Go slices and loops.
What you can do after
Evaluate how sequential and strided slice access interacts with cache lines.
How it connects
Connects temporal locality, spatial locality, CPU caches, cache lines, contiguous arrays, and memory hierarchy.

Also explained in

Compare how 4 books explain data locality →

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.