Caches: Maximising CPU Efficiency: The Power of Caches

gravatar
By Ranti
 · 
April 28, 2023
 · 
4 min read

In the fast-paced world of computing, speed is paramount. The central processing unit (CPU) can execute instructions at an astonishing rate, with modern processors reaching speeds of up to 3 gigahertz (3 billion cycles per second). Ideally, a simple instruction like addition can be completed in just one cycle. That means, in an ideal scenario, a single cycle equates to a mere 0.33 nanoseconds – an incredibly short span of time, even in the world of electronics where operations occur at lightning speed.

  • The CPU can execute instructions much faster than the memory can provide instructions and data

3 GHz processor: 3 billion cycles per second

  • ideally, the processor can execute a simple instruction (add) in 1 cycle
$$ 1 \text{ cycle} = \frac{1}{\text{3 billion}} sec = 0.33ns \text{ nanosecond: 1 billionth of a sec}$$

In contrast, the main memory, or RAM, operates on a different timescale. Accessing data in RAM takes between 60 to 100 nanoseconds, which translates to a range of 180 to 300 processor cycles. This stark contrast in speed between the CPU and memory can create a bottleneck, as the CPU often finds itself twiddling its virtual thumbs, waiting for data.


RAM: 60 - 100ns = 180 - 300 cycles

Registers are on the processor, so data in registers can be accessed immediately

  • but lots of data is in memory
  • all instructions are in memory

How to avoid constantly waiting for memory

  • use caches

Cache: very small, but very fast memory that is generally on the CPU chip


This discrepancy in processing speeds stems from the fundamental difference in how the CPU and memory are constructed. Registers, which hold small amounts of data and are integral to the CPU's operation, are nestled right on the processor. This proximity enables lightning-fast access to data stored in registers – an operation that takes only a single clock cycle.

However, a vast trove of data resides in the memory, which the CPU must traverse through the comparatively sluggish channels of the system bus. All instructions and data ultimately reside in this expanse of memory, which is where the challenge arises: how can we prevent the CPU from languishing in downtime, waiting for data to be retrieved?

The solution lies in the strategic use of caches.

Enter Caches: Bridging the Speed Gap

Caches serve as nimble intermediaries between the CPU and the slower main memory. These specialised, high-speed storage units hold frequently accessed data and instructions, essentially acting as a buffer to minimise the time the CPU spends waiting for data.

Caches are designed to exploit a key principle of computing: locality of reference. This principle asserts that programs tend to access a small set of data or instructions repeatedly, often in a localised region of memory. Caches take advantage of this by storing copies of frequently accessed data and instructions from the main memory, making them quickly accessible to the CPU.

When the CPU requests data, the cache is the first port of call. If the desired data is present in the cache, the CPU can fetch it almost instantaneously. This process is akin to having a personal assistant who hands you the documents you need without you having to leave your desk. If the data isn't in the cache, only then does the CPU venture into the main memory.

The Three Levels of Caching

Modern computer systems often employ a hierarchy of caches, with each level catering to specific needs:

  1. L1 Cache (Level 1): Located directly on the CPU chip, the L1 cache is the smallest but also the fastest. It consists of separate caches for instructions and data, allowing both to be accessed simultaneously.
  2. L2 Cache (Level 2): Situated on the same chip or very close to it, the L2 cache is larger than the L1 cache. It serves as a secondary buffer, storing additional data and instructions for quick retrieval.
  3. L3 Cache (Level 3): Positioned further from the CPU, the L3 cache is larger still and serves as a shared resource for multiple CPU cores. It acts as a backup, holding a broader range of data that may not be as frequently accessed.

By incorporating multiple cache levels, computer architects strike a balance between speed and capacity. The L1 cache provides blazingly fast access to the most critical data, while the larger, slightly slower L2 and L3 caches ensure a broader range of frequently accessed data is readily available.

Conclusion: Unleashing CPU Potential

In the relentless pursuit of computing efficiency, caches play a pivotal role. They enable CPUs to operate at their full potential, ensuring that the disparity in speed between the CPU and memory doesn't hinder performance. By intelligently storing and retrieving data, caches transform the way modern processors harness their incredible processing power, making them indispensable components of today's computing landscape.

View