Academic Program

How Does Cache Work?

How Does Cache work?

Using the cache is optional, but it is critically important if you want to maximize performance.

You can manually load cache or have it automatically load itself as you execute code.

Once data is in the cache, it can be read and modified without accessing main memory. If the cache is large enough to hold all the data needed by your application, it may never have to update main memory. This enables maximum performance and reduces the power consumed by accessing main memory.


 Cache Hit

When the CPU fetches data from cached memory, the system checks if the required data exists in the cache. If it exists, the data is read directly from the cache with no performance penalty. This is called a cache hit.


Cache Miss

If required data does not exist in the cache, hardware fills the cache with data from main memory. The CPU must wait for the cache fill to complete before reading data. This is called a cache miss. A cache miss incurs a performance penalty proportional to the time required to perform the cache fill.


Dirty Cache

If the CPU modifies a location in the cache without writing it back to main memory, that cache line is said to be dirty. The cache controller keeps track of these dirty lines.


Update Main Memory Before Evicting Data from the Cache

At some point, the hardware may need to make room in the cache for new data. Before modified (dirty) data can be evicted from the cache, it must be written back to main memory. The hardware will do this automatically.

There are two methods the hardware uses to update main memory:

  1. Immediately update main memory when the cache changes (write-through).
  2. Wait to update main memory when data is evicted from the cache (write-back).

You control which method is used.