Given that CPU caches behave as N-way associative arrays, this has important…
- User
Given that CPU caches behave as N-way associative arrays, this has important implications for datastructures, access pattern and algorithms.
We can look at the cache set counts for AMD Zen 5: L1: 64 sets (48 kB), 12-way L2: 1,024 sets (1MB) 16-way L3: 8,192 sets (8 MB), 32,768 sets (32 MB) 16-way
We will look only at L1, where the total cache size is 49152 bytes, or 768 cachelines.
Using birthday problem / bins-and-balls heuristics, how many cachelines can I insert at random addresses before eviction rates exceeds 10% on average?
Assistant
Looking at this as a bins-and-balls problem, we have:
- 64 sets (bins)
- 12-way associativity (each bin can hold 12 cache lines)
- We're inserting cache lines with uniformly random set assignments
When a cache line maps to a set that already has 12 entries, an eviction occurs.
This is equivalent to the classical occupancy problem. The probability that a specific set has exactly k cache lines after n insertions follows a binomial distribution: B(n, 1/64).
The expected number of evictions after n insertions is: E[evictions] = Σ(i=1 to n) P(set is full when inserting line i)
For large n and m (number of sets), this can be approximated as: E[evictions] ≈ n - m × k × (1 - (1 - 1/m)^(n/k))
Where k is the associativity (12) and m is the number of sets (64).
To find when the eviction rate reaches 10%, we need: E[evictions]/n = 0.10
Solving numerically:
- n ≈ 580 cache lines for 10% eviction rate
We can verify this makes intuitive sense:
- Total capacity: 64 × 12 = 768 cache lines
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.