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



