Computer Architecture Cheatsheet

Caches

Use this Computer Architecture reference while you build software engineering projects, review code for technical interview prep, or polish examples for a software engineer resume.

Cache Fundamentals

A cache is a small, fast SRAM memory that transparently stores copies of recently-used main-memory locations. On each access the CPU checks the cache first:

  • Hit — data found in cache → return in hit time (~1–10 cycles)
  • Miss — data not found → fetch from next level, install in cache, then return

Hit rate h = hits / total accesses Miss rate m = 1 − h

Cache Organization

Every cache line (block) is identified by decomposing the memory address:

Address bits:
 [ Tag | Index | Block Offset ]
   t      s        b
where: 2^b = block size (bytes), 2^s = number of sets

Example: 32-bit address, 64-byte blocks (b=6), 256 sets (s=8): - Block offset: bits [5:0] → 6 bits - Index: bits [13:6] → 8 bits - Tag: bits [31:14] → 18 bits

Associativity

TypeMappingDescription
Direct-mapped1 wayEach address maps to exactly one cache line
2-way set associative2 waysAddress maps to a set of 2 lines; choose either
4-way set associative4 waysSet of 4; most common for L1
8-/16-way8–16 waysL2/L3 common
Fully associativeAny lineAny address → any cache slot; victim caches, small L1 TLBs

Trade-offs:

PropertyDirect-mappedHigh associativity
Hit timeLowest (1 comparison)Higher (parallel comparisons)
Conflict missesHighLow
Hardware costLowHigher (more comparators)

The Three C's of Cache Misses

Miss typeCauseSolution
Compulsory (cold)First access to a block — always missesPrefetching
CapacityCache too small to hold working setLarger cache
ConflictMultiple addresses map to same setHigher associativity, victim caches
(Coherence)Invalidations in multi-core systemsBetter protocols

Replacement Policies

PolicyDescriptionHit rate
LRU (Least Recently Used)Evict the line not used for longest timeBest practical
Pseudo-LRUApproximation using tree bitsClose to LRU, cheaper
FIFOEvict oldest-installed lineSuffers Belady's anomaly
RandomEvict a random lineSurprisingly competitive; simple
Optimal (Bélády)Evict line used farthest in futureTheoretical lower bound; offline only

Write Policies

Write-Hit

PolicyDescriptionProsCons
Write-throughUpdate cache AND memory on every writeMemory always consistentHigh memory bandwidth
Write-backUpdate cache only; write to memory on evictionLow bandwidthComplexity; dirty bit needed

Write-Miss

PolicyDescription
Write-allocateFetch block into cache, then modify (pair with write-back)
No-write-allocateWrite directly to next level; don't bring into cache (pair with write-through)

Most modern caches: write-back + write-allocate.

Cache Parameters and Their Effects

ParameterIncreasing it →
Cache sizeFewer capacity misses; higher hit time; more area/power
Block sizeFewer compulsory misses (spatial locality); more conflict misses (fewer sets); larger miss penalty
AssociativityFewer conflict misses; higher hit time (more comparators)
Number of levelsBetter AMAT; more complexity

Typical Cache Hierarchy (Modern CPU)

LevelTypical sizeLatencyAssociativityShared?
L1-I32–64 KB per core3–5 cycles8-wayPer core
L1-D32–64 KB per core4–5 cycles8-wayPer core
L2256 KB–2 MB per core12–15 cycles8-wayPer core
L3 (LLC)8–64 MB30–50 cycles16-wayShared
L4 (eDRAM)64–128 MB (rare)~50–100 cyclesShared

Prefetching

Bring blocks into cache before they are demanded.

TypeDescriptionExample
Hardware sequential prefetchDetect stride, fetch next linesDetect A[0], A[1], A[2] → prefetch A[3]
Stride prefetcherDetect arbitrary stride ΔA[0], A[4], A[8] → stride 4
Software prefetchCompiler/programmer inserts __builtin_prefetch()Can prefetch non-trivial patterns
Next-line prefetchAlways prefetch the next cache lineSimple, effective for streaming

Cache Performance Example

Direct-mapped, 1 KB cache, 16-byte lines (4-bit offset, 6-bit index, 22-bit tag):

Access pattern A[0], B[0], A[1], B[1] where A and B map to the same set:

Access    Cache state                    Result
A[0]      Set 0: [A[0] block]            Miss (cold)
B[0]      Set 0: [B[0] block]            Miss (conflict — evicts A)
A[1]      Same set as A[0]               Miss (conflict — evicts B)
B[1]      Same set as B[0]               Miss (conflict — evicts A)

100% miss rate despite data fitting in cache — classic conflict miss (thrashing). Fix: 2-way associativity or padding B to a different cache set.

Inclusion and Exclusion

PolicyDescriptionTrade-off
Inclusive L2Every L1 line is also in L2Simplifies coherence (can invalidate from L2); wastes L2 space
ExclusiveL1 and L2 hold disjoint setsMaximizes total capacity
Non-inclusive / Non-exclusive (NINE)Neither guaranteedModern LLC approach (Intel L3)

Cache Coherence (see Memory Hierarchy for MESI)

On a write to a shared line: - Invalidation protocol (MESI/MOESI): broadcast invalidation; other copies become Invalid - Update protocol: broadcast new value to all copies; lower bandwidth but more traffic

Invalidation dominates in practice.

Virtual vs Physical Caches

Cache typeTags useLook up before TLB?Problem
Physically indexed, physically tagged (PIPT)Physical addressesNo (after TLB)Safe; correct; slower
Virtually indexed, physically tagged (VIPT)VA index, PA tagParallel with TLBAliasing possible if VA index > page offset bits
Virtually indexed, virtually tagged (VIVT)Virtual addressesYes (before TLB)Aliasing and homonym problems

Modern L1 caches are usually VIPT designed so that the index bits fall within the page offset, making them effectively PIPT (no aliasing).