liveBook · Manning

Cache hit

As explained in 100 Go Mistakes and How to Avoid Them

A cache hit occurs when a requested memory location is found in the cache being checked.

A cache hit occurs when a requested memory location is found in the cache being checked. CPUs check L1, then L2, then L3, before accessing main memory. section 12.1.2

Sequential slice access

When sum first accesses s[0], the address causes a compulsory cache miss and may load the entire memory block containing elements 0 through 7. Accessing elements 1 through 7 then produces cache hits. Accessing s[8] causes another compulsory miss, while elements 9 through 15 hit the newly loaded block. Iterating over all 16 int64 values therefore produces 2 compulsory misses and 14 cache hits. 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
}

Use the sequential sum implementation to connect adjacent slice accesses with cache-line reuse. section 12.1.2

How 100 Go Mistakes and How to Avoid Them covers it

How deep it goes
Defines hits through a 16-element slice example, but defers detailed CPU caching strategies.
Examples
Uses Go functions summing 16 int64 slice elements and tracks hits across two loaded memory blocks.
What it assumes
Assumes familiarity with Go slices and iteration while defining cache hits through concrete memory-block behavior.
What you can do after
Evaluate sequential slice access by distinguishing compulsory misses from subsequent cache hits.
How it connects
Connects cache hits with cache misses, cache lines, CPU caches, memory blocks, and main memory.

Also explained in

Compare how 3 books explain cache hit →

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.