core22 min studyLesson 112 in Engineering

Cache mapping, associativity, and conflicts

Trace an explicit set-associative example and separate capacity pressure from mapping conflicts.

On this page

What you’ll understand

  • Compute set indices for a declared modulo mapping.
  • Explain why a working set can fit in bytes and still conflict.
  • Design a controlled alignment experiment without assuming a real cache's hash.

A cache with room for hundreds of lines may still fail to retain three particular lines together. Capacity describes total storage; mapping decides which locations may hold an address. Associativity constrains how many lines competing for the same set can coexist. A small address trace makes the distinction visible and prevents a common mistake: diagnosing every miss that survives warmup as evidence that the entire working set is simply too large.

A declared mapping makes the trace reproducible

Consider a hypothetical cache with line size L, S sets, and A ways per set. Under a simple modulo mapping, divide the byte address by L to obtain its line number, then take the remainder modulo S to choose a set. The remaining address identity acts as a tag. Each set has A places, but a line cannot use an empty place in some other set merely because that would avoid an eviction.

This arithmetic is a teaching contract, not a reverse-engineered description of a particular processor. Real designs can use physical or virtual address information at different stages, slice hashes, skewed mappings, and policies that vary across levels. Software should query documented geometry where possible and treat inferred mappings as hypotheses. Even with known geometry, replacement and concurrent traffic can make the actual residency sequence differ from an ideal least-recently-used trace.

ℓ=⌊aL⌋,s=ℓ mod S,C=LSA\ell=\left\lfloor\frac{a}{L}\right\rfloor,\qquad s=\ell\bmod S,\qquad C=L S A
a is a nonnegative byte address; L and C are bytes. Line number ℓ, set index s, number of sets S, and associativity A are dimensionless integers. The mapping is explicitly hypothetical modulo indexing.↔ Scroll wide equations horizontally.

Separate compulsory, capacity, and conflict effects

The first touch of a line cannot hit in an initially empty cache: it is compulsory under that starting condition. Later misses may occur because too many live lines need storage, because their mapping overloads one set, or because coherence and other agents remove copies. The classic capacity-versus-conflict distinction uses a fully associative reference cache of the same total capacity and a specified replacement rule; classification depends on that comparison, not merely on a performance counter's name.

To isolate conflict behavior, repeat a short sequence whose total footprint is below total capacity but whose lines all map to one set. Under ideal LRU, cycling through A+1 distinct competing lines can miss on every access after warmup. Changing an address offset can spread the lines across sets without changing their useful values. A timing improvement is evidence worth investigating, but does not by itself prove that a documented cache level caused it.

aj=a0+jSL⟹s(aj)=s(a0)a_j=a_0+jSL\quad\Longrightarrow\quad s(a_j)=s(a_0)
For nonnegative integer j in the simple modulo cache, addresses separated by S×L bytes have the same set index. The result concerns mapping, not a prediction about a real processor's undocumented address hash.↔ Scroll wide equations horizontally.

Change layout with an explanation

Padding a matrix leading dimension can change which sets its rows compete for. The extra storage may help one access pattern while hurting another, increasing page count, or defeating vector-friendly alignment. Compare multiple nearby dimensions instead of tuning only a conveniently favorable size. Record the full layout contract so a later allocator change or architecture transition does not silently invalidate the explanation. An optimization should earn its complexity across the workloads that matter.

The probability lab is not a cache model. Its success probability p and integer trial count n define independent Bernoulli trials and exact binomial bars. Use it only as a contrasting model: predict that p=0 makes every success count zero, then explain why assigning a fixed independent hit probability cannot reproduce a deterministic repeated conflict trace. Cache outcomes depend on address history and replacement state; the lab's independent trials deliberately omit both mechanisms.

Three lines competing for two ways

Use a cold cache with 64-byte lines, four sets, two ways, modulo indexing, and ideal LRU. Repeat byte addresses 0, 256, 512. What happens over two complete cycles?

  1. Divide each address by 64 to get line numbers 0, 4, and 8. Modulo four, all select set zero. The full cache holds eight lines, but this set holds only two.

  2. The first cycle has three compulsory misses. After addresses 0 and 256 fill the set, address 512 evicts line zero, the least recently used entry.

  3. On the second cycle, address 0 evicts line four; 256 evicts line eight; 512 evicts line zero again. All six accesses miss, despite a three-line footprint below the cache's eight-line capacity.

The result

The declared mapping creates persistent two-way conflicts. Spreading the three lines across sets would permit hits after their first fills in this isolated example.

Explore the model

Interactive lab

Interactive lab · probability

Why independent hit guesses miss the mechanism

Change success probability p and trial count n in the exact binomial model. Predict p=0 and compare with p=0.5. This is a contrast with stateful cache traces, not a simulation of mapping or replacement.

Why independent hit guesses miss the mechanismThese are exact binomial probabilities for independent trials with one fixed success probability. Playback tours the distribution; it does not resample outcomes. Current readings: Mean · np: 5; Variance · np(1−p): 2.5; Total probability: 1. All parameters are adjustable with the labeled controls below.03581000.070.150.220.3Successes kP(X = k)k = 0 · P = 9.77e-4
Exact probabilityHighlighted outcomeMean np
Mean · np
5
Variance · np(1−p)
2.5
Total probability
1
t = 0 s

These are exact binomial probabilities for independent trials with one fixed success probability. Playback tours the distribution; it does not resample outcomes. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.

Look forA cache trace needs address and residency state; independent success trials cannot establish a conflict mechanism.

Check your understanding

A three-line working set repeatedly misses in a two-way set even though total cache capacity is eight lines. What is the most specific explanation in the declared example?

Try it yourself

In the same four-set cache, replace address 512 with 576. Determine the set indices and count misses over two cycles of 0, 256, 576.

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

576/64 is nine. Trace each set separately.

Compare with the solution

Line numbers are 0, 4, and 9, giving set indices 0, 0, and 1. The first cycle has three compulsory misses. Set zero retains its two lines and set one retains line nine, so all three accesses in the second cycle hit. This prediction requires the isolated cache, stated mapping, and lack of intervening coherence or replacement pressure; it does not establish how an arbitrary physical cache behaves.

Take it with you

  • Total capacity does not eliminate per-set pressure.
  • Conflict explanations require a declared mapping and replacement model.
  • Layout changes need workload measurements and architecture-specific confirmation.

Read further

Original sources and deeper explanations to follow at your own pace.

Optional progress, saved in this browser when available. Revisit any lesson at any time.

Lesson not marked complete.