Understanding Cache Memory

Understanding Cache Memory

Introduction

You may have often seen or heard the term "cache" while using your smartphone or buying a new laptop. Ever wondered what it is and why do we even need it? A cache memory (often referred to as "cache") is a high-speed memory used to reduce the communication time between CPU and main memory.

How does a computer processor work?

We often hear the word processor but don't know what it exactly does. A processor takes instructions stored in the memory and executes them. These instructions are what makes your programs run on the computer. Each program can be thought of as a sequence of instructions to be executed by the processor. There's more to it but in our context, this should suffice.

Need for cache

The invention of anything has a sole purpose behind it - Need. Computer Scientists have contributed decades of their research solely to find out how computing can be made faster. When we think of making computing faster, the first thought would be to simply increase the processor speed (in technical terms, increasing the clock speed of the processor), allowing it to process an instruction in lesser time.

However, there is a catch!

By increasing processor speed, we run into two problems -

  1. High power consumption
  2. Heating of the processor.

The cost of overcoming these problems greatly outweighs the benefit obtained. We can tweak some of the existing systems in order to increase the performance. One of such tweak is the Introduction of a cache memory in the system.

Where the problem arises

Think of a processor as a renowned chef. The chef excels at making various dishes and recipes. In order to cook the food, the chef needs to buy the groceries. Where does the chef buy the groceries from? The grocery store. This grocery store will be our main memory.

Suppose the chef has to cook 7 dishes in a day. He goes to the grocery store and purchases the ingredients of a single dish, comes home, cooks that single dish. After finishing with the first dish, he visits the grocery store again, purchases the ingredients of the second dish, comes home, and cooks the second dish. This is continued for all the 7 dishes.

noncache.png

Seems very time-consuming right? What can be done so that chef cooks all 7 dishes in lesser time?

Obviously, the better approach is to make a list of all the ingredients that would be needed to cook all the 7 dishes and buy them once and store all the ingredients at home. In this way, when he is done with the first dish, he can immediately start with the second without having to visit the grocery store again.

cache.png

This concept is known as caching.

Cache memory

Cache memory is a high-speed memory which means fetching data from cache would be faster as compared to our main memory. However, it is also costlier. As a result, there is a small size cache provided to the processors to aid them in fetching the data faster. How a limited size cache makes sure it has all the data that would be needed is a topic for some other day.