liveBook · Manning

Set-associative cache

As explained in 100 Go Mistakes and How to Avoid Them

A set-associative cache partitions cache lines into sets and restricts each memory block to one set while allowing placement among that set's lines.

A set-associative cache partitions cache lines into sets. Each memory block maps to one set, then occupies one of that set’s available lines. This avoids searching every cache line, as a fully associative cache might, while allowing more placement flexibility than a single fixed line. section 12.1.5

A useful diagnostic mistake is treating nearby memory accesses as equally cache-friendly. In a reduced example, a 512-byte cache has eight 64-byte lines and uses two-way associativity, so each set contains two lines. Address bits determine the destination set; competing blocks can replace one another when they map to the same set. section 12.1.5

Matrix traversal

The same loop sums the first eight elements of rows stored in [][512]int64 and [][513]int64. The resulting performance difference is attributed to CPU-cache placement, making the array shape a useful diagnostic comparison. section 12.1.5

How 100 Go Mistakes and How to Avoid Them covers it

How deep it goes
Explains sets, associativity, cache lines, memory blocks, and address bits, but stops before a complete address-bit decomposition.
Examples
Uses a two-way 512-byte cache, a 4-by-32 matrix, address diagrams, and Go functions comparing 512- and 513-element rows.
What it assumes
Assumes basic memory-address and cache terminology while defining cache lines, blocks, sets, and associativity through a reduced example.
What you can do after
Evaluate cache-sensitive traversal differences by inspecting row widths, memory blocks, and competing set assignments.
How it connects
Connects cache placement with CPU caches, cache lines, memory blocks, data alignment, and matrix traversal.

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.