Cache

Caching enables us to speed up reads from Computer Memory. However, there are two questions:

  • How do we know if a data item is in the cache?
  • If it is, how can we find it?

Cache (L1 and L2 Cache) is built using SRAM.

Layers of Cache

  1. L1 Cache (separate data and instruction cache) between 16 KB to 128 KB
  2. L2 Cache (unified) between 256 KB to 2 MB per core.
  3. L3 cache

Cache Line sizes:

  • L1 Cache: 32 bytes or 64 bytes.
  • L2 Cache: 64 bytes (common).
  • L3 Cache (if present): 64 bytes.

Caching relies on the Principle of Locality to try to find the desired data in the higher levels of the memory hierarchy, and provides mechanisms to ensure that when the prediction is wrong it finds and uses the proper data from the lower levels of the Memory Hierarchy.

  • The hit rates of the cache prediction on modern computers are often above 95%

For Distributed Systems, look at Bus Snooping.

Cache Structures

There are a few ways to do this, each of these is a particular cache structure:

  1. Direct Mapped Cache
  2. Fully Associative Cache
  3. Set Associative Cache

ECE222 mostly emphasized on 1 and 3.

Read Access

a referenced address is divided into

  • A tag field, which is used to compare with the value of the tag field of the cache
  • A cache index, which is used to select the block

Handling Writes

This seems closely related to the idea of having Cache Coherency.

Basically, if you write the the cache, you also want to make sure that value is written to the memory, else cache and memory would have different values.

  • Writing to both cache and lower level of Memory Hierarchy is a scheme called write-through

The problem is that just using a write-through scheme provides very slow performance. A solution is to use a Write Buffer, which stores data while waiting to be written to memory.

There is also the write-back scheme, where new data is written only to the block in the cache, not to memory. This provides performance increase, but is more complex to implement.

Personal Thoughts

Cache: a safe place for hiding or storing things. Caching can be a relatively interesting problem. I worked on using caching with Angular so we can 20x our load speed, and not having to read from the database every time.

At HackWestern, I wanted to load the image, but since this image was being cached, when I overwrote that file, it was not being detected.