liveBook · Manning

Compulsory miss

As explained in 100 Go Mistakes and How to Avoid Them

A compulsory miss is a cache miss caused by the first access to a memory block that has not yet been loaded into the cache.

A compulsory miss occurs when the CPU accesses a memory block for the first time and that block is not yet present in the cache. Fetching the block into a cache line can make nearby accesses cache hits. section 12.1.2

Walking through 16 int64 elements

When sum first accesses s[0], the CPU fetches its memory block, producing a compulsory miss. Accesses to s[1] through s[7] hit the same cache line. Accessing s[8] fetches another block and causes a second compulsory miss; s[9] through s[15] then hit. The complete iteration therefore produces two compulsory misses and fourteen 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 function to relate array traversal to cache-line fetching; the accompanying figures show the blocks fetched by s[0] and s[8]. section 12.1.2

How 100 Go Mistakes and How to Avoid Them covers it

How deep it goes
Traces two misses and fourteen hits in a slice traversal, then defers detailed cache-inclusion and eviction strategies.
Examples
Uses a sum function over sixteen int64 elements, memory-block diagrams, and a count of two misses versus fourteen hits.
What it assumes
Assumes familiarity with memory locations, slices, and cache levels while establishing cache-line fetching through a concrete example.
What you can do after
Evaluate/understand sequential memory access by tracing cache-line fetches and distinguishing compulsory misses from later cache hits.
How it connects
Links compulsory misses to cache lines, cache hits, CPU caches, and the memory hierarchy.

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.