Principle of Locality
What You'll Learn
You'll discover why programs naturally access the same data and nearby data over and over—a pattern that makes caching effective.
The Core Idea
The Principle of Locality explains a fundamental pattern in how programs access memory: they tend to reuse the same data items repeatedly and access data stored near each other. This isn't random—it's built into how we write and run programs.
Temporal Locality
Temporal locality means "nearby in time." When a program accesses a memory location, it's likely to access that same location again soon.
Analogy: Think of a chef using salt while cooking. Once they grab the salt shaker, they'll probably need it again moments later—not just once, but multiple times during meal preparation.
Why it happens:
- Loop variables get read and updated repeatedly
- Function calls reuse the same instructions
- Frequently accessed variables stay "hot"
Spatial Locality
Spatial locality means "nearby in space." When a program accesses one memory location, it's likely to access adjacent locations soon after.
Analogy: When reading a book, you don't jump randomly between pages—you read the current page, then the next one, then the one after that.
Why it happens:
- Array elements are processed sequentially
- Instructions execute in order (mostly)
- Related data structures are stored together
Why This Matters
The Principle of Locality is the reason caches work! By keeping recently-used data (temporal) and nearby data (spatial) in fast cache memory, we can dramatically improve performance. Programs with good locality patterns run faster because they hit the cache more often.
Key Takeaway: Programs exhibit temporal locality (reusing the same data soon) and spatial locality (accessing nearby data), making caching an effective strategy for bridging the memory gap.