Cache Fundamentals: How Small, Fast Memory Predicts What You Need Next

A cache works because programs tend to access the same or nearby data repeatedly rather than randomly. This article explains the principle of locality that makes caching effective, how a direct-mapped cache locates data using an address, and what happens on a cache hit versus a cache miss.

Cache MemoryLocality of ReferenceDirect-Mapped Cache

~3 دقیقه مطالعه · آخرین به‌روزرسانی ۱۵ شهریور ۱۴۰۵

Why Caching Works: The Principle of Locality

A cache is only useful because real programs do not access memory in a purely random pattern. Instead, they exhibit Locality of Reference, which comes in two related forms: Temporal Locality, meaning a recently accessed memory location is likely to be accessed again soon, and Spatial Locality, meaning memory locations near a recently accessed one are also likely to be accessed soon, such as the next elements in an array.

What a Cache Actually Stores

A Cache is a small, fast memory that holds copies of recently used data from main memory, positioned between the processor and main memory in the hierarchy discussed earlier in this series. When the processor requests data already present in the cache, it can be retrieved far faster than fetching it from main memory.

Locating Data: The Direct-Mapped Cache

The simplest cache organization is called a Direct-Mapped Cache, where each memory address maps to exactly one specific location in the cache, determined by a portion of the address bits.

Memory address broken into fields:
[ Tag | Index | Block Offset ]

  • The Index bits select which cache location to check.
  • The Tag bits are stored alongside the data and compared against the requested address to confirm the correct data is present, since multiple different memory addresses can map to the same index.
  • The Block Offset bits select the specific byte within a larger stored block, since caches typically store data in fixed-size chunks rather than single bytes.

Cache Hits and Cache Misses

When the processor requests data and finds it already present in the cache with a matching tag, this is called a Cache Hit, and the data is returned quickly. When the requested data is not found, this is called a Cache Miss, and the processor must retrieve the data from a slower level of the memory hierarchy, then store a copy in the cache for potential future use.

On a cache hit:
Return data immediately from cache

On a cache miss:
Fetch data from main memory
Store a copy in the cache
Return data to the processor

Why Block Size Matters

Because of spatial locality, caches do not store just a single requested byte on a miss; they retrieve and store an entire Block (also called a Cache Line) of nearby memory at once, anticipating that neighboring data will likely be needed soon. Choosing an appropriate block size involves a tradeoff: larger blocks exploit spatial locality more effectively but take longer to transfer on a miss and can waste cache space if the extra data is not actually used.

Why Understanding These Basics Matters

Every more advanced caching topic discussed later in this series — measuring and improving cache performance, more flexible mapping strategies, and multi-level cache hierarchies — builds directly on these core concepts of locality, direct mapping, and the hit/miss distinction introduced here.

نوشته و پژوهش‌شده توسط دکتر شاهین صیامی

مقالات مرتبط

Common Misconceptions About Parallel Computing and the Book's Final Lessons

After covering everything from thread-level parallelism to warehouse-scale computing, it is worth correcting persistent misconceptions about parallel systems that even experienced engineers sometimes hold. This article addresses common fallacies about scaling and parallel hardware, then closes out the parallel processing chapter by tying together the full journey from a single instruction to a building full of cooperating machines.

ادامه

Real Stuff: Benchmarking CPUs Against GPUs and Multiprocessor Matrix Multiply

Comparing a CPU and a GPU fairly requires a model that accounts for both computational throughput and memory bandwidth limits together. This article introduces the roofline model used to compare real hardware like the Intel Core i7 and NVIDIA Tesla GPU, then shows how matrix multiplication is accelerated across multiple processors as the final practical application of this chapter's parallel concepts.

ادامه

Benchmarking Multiprocessors and Modeling Parallel Performance

Measuring the performance of a parallel system requires different tools and metrics than measuring a single-core processor. This article covers the specialized benchmarks used to evaluate multiprocessor systems, explains how to model scaling behavior as more processors are added, and revisits Amdahl's Law in the context of real-world performance measurement.

ادامه

Cluster Networking: Connecting to the World Outside

A cluster of machines is only useful if it can communicate efficiently both internally and with the outside world. This article covers the networking layers involved in cluster communication, the tradeoffs between latency and bandwidth at scale, and how clusters connect to external networks and users.

ادامه

Clusters, Warehouse-Scale Computers, and Network Topologies

Beyond a single chip, parallelism extends to entire buildings full of independent computers working together. This article explains the shift from shared memory multiprocessing to clusters of separate machines, introduces the concept of warehouse-scale computing, and covers the network topologies that connect these independent machines efficiently.

ادامه

An Introduction to GPUs: Massive Parallelism for Data-Heavy Workloads

A GPU takes the SIMD idea covered earlier in this series to an extreme scale, running thousands of lightweight threads simultaneously to process massive amounts of independent data. This article explains why GPUs are architecturally so different from CPUs, how their thread execution model works, and what kinds of workloads benefit most from this design.

ادامه