liveBook · Manning

Cache replacement policy

As explained in 100 Go Mistakes and How to Avoid Them

A cache replacement policy selects an existing cache line to evict when a new block must be stored in a full set.

A cache replacement policy chooses which existing cache line to evict when a new memory block targets a full cache set. It does not move the block to another set. CPUs commonly use pseudo-LRU because exact least-recently-used tracking is too complex. section 12.1.5

When repeated rows evict one another

A matrix traversal can repeatedly address blocks whose set index is 00. After set 0 fills, reading the next row replaces an existing line; revisiting the first row then misses because its block was evicted. These conflict misses arise from concentrating accesses in one cache set, a pattern called a critical stride. section 12.1.5

func calculateSum512(s [][512]int64) int64 {     #1
    var sum int64
    for i := 0; i < len(s); i++ {
        for j := 0; j < 8; j++ {
            sum += s[i][j]
        }
    }
    return sum
}
 
func calculateSum513(s [][513]int64) int64 {     #2
    // Same implementation as calculateSum512
}

Compare the otherwise identical calculateSum512 and calculateSum513 traversals when diagnosing cache behavior caused by their row layouts. section 12.1.5

How 100 Go Mistakes and How to Avoid Them covers it

How deep it goes
Explains set-index collisions, eviction, pseudo-LRU behavior, and resulting conflict misses, but not replacement implementation internals.
Examples
Uses matrix rows, repeated address patterns, and calculateSum512 and calculateSum513 benchmark functions.
What it assumes
Assumes cache lines, cache sets, set indices, and memory strides; teaches how full sets trigger replacement and conflict misses.
What you can do after
Diagnose conflict misses by tracing set indices and identifying accesses that fill and replace one cache set.
How it connects
Connects cache lines with cache sets, set-associative caches, cache misses, conflict misses, and critical stride.

Also explained in

Compare how 2 books explain cache replacement policy →

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.