Vengeance Blog

What Programmers Should Know About Memory

Why cache behavior dominates many real-world performance outcomes.

Performance1 minInspired by Ulrich Drepper (2007)

Memory hierarchy

Registers, caches, RAM, and storage have very different latency and bandwidth. Algorithms that ignore locality pay large runtime costs.

Locality

  • Temporal locality: reuse data soon
  • Spatial locality: access nearby addresses
  • Sequential access helps prefetchers

Example

tsx
1// cache-friendly2for (let r = 0; r < rows; r++)3  for (let c = 0; c < cols; c++) sum += matrix[r][c];4 5// cache-hostile6for (let c = 0; c < cols; c++)7  for (let r = 0; r < rows; r++) sum += matrix[r][c];