CSArchitecture & RuntimeDiagramIntermediate
CPU Cache Locality
KO: CPU 캐시 지역성·EN: CPU Cache Locality·JA: CPUキャッシュ局所性
The performance principle of exploiting temporal and spatial memory locality to hit 1ns L1 caches instead of stalling 100ns on main RAM
Table of Contents
Structure Diagram
Memory Hierarchy Access Latency (L1 Cache vs L3 vs RAM)
(66, 26)Access Latency (ns) #3
Quick Answer
What is CPU Cache Locality?
The performance principle of exploiting temporal and spatial memory locality to hit 1ns L1 caches instead of stalling 100ns on main RAM. It is a core concept in Architecture & Runtime within CS.
Core Principles
CPU instructions execute in sub-nanoseconds while main RAM takes ~100ns. To bridge this 100× gap, CPUs layer L1 (~1ns) → L2 (~4ns) → L3 (~15ns) caches and fetch memory in contiguous 64-byte cache lines.
- Temporal Locality: Recently accessed data is likely to be reused soon.
- Spatial Locality: Data stored next to recently accessed memory is fetched for free on the same cache line and via hardware prefetchers.
Practical Takeaways
- Iterate in memory layout order: Scanning a 2D array row-by-row instead of column-by-column can be 5–10× faster on the exact same Big-O complexity.
- Prefer contiguous containers: Dynamic arrays and B-Trees frequently beat pointer-chasing linked lists and BSTs in real benchmarks.
- SoA & False Sharing: Data-oriented designs (ECS, columnar DBs) pack hot fields contiguously (Structure of Arrays), while multi-threaded counters pad to 64-byte boundaries to prevent cross-core cache-line ping-pong.
Notable Examples
- Game Engine ECS (Bevy, Unity DOTS): Contiguous component arrays for 60fps simulation.
- Columnar Analytics (DuckDB, ClickHouse, Apache Arrow): Maximizing cache-line utilization and SIMD throughput.