If you are a tech enthusiast, you might have heard the word Cache in multiple contexts. Be it web browsers or apps, caches are used everywhere, But what does a cache have to do with a CPU? Can it have a significant effect on the performance of your system?
If you find yourself asking similar questions, you have come to the right place because, in this article, we will be talking about all there is to a CPUs’ cache and if it makes your system run faster.
What is cache memory?
To put it simply, cache memory is an astonishingly fast volatile memory used to supply data to the CPU. This memory is placed on the CPU itself and is a critical parameter for the performance of a processor. But what makes cache memory so fast?
Well, to answer this question, there is a fundamental difference in the way cache memory and RAM are made. Cache memory uses SRAM(Static RAM ) to store data, whereas RAM uses DRAM(Dynamic RAM).
SRAM cells use transistors to store data, whereas DRAM uses capacitors and transistors to store data. Due to the use of capacitors in DRAM cells, they need to be refreshed frequently as capacitors lose charge over time. During a refresh cycle, the data in the RAM cannot be accessed, making it slower. On the other hand, SRAM does not lose charge and hence is given the name static RAM.
In addition to this, cache memory is placed on the CPU die. Due to this, the CPU can access data faster as it is placed closer to the CPU. On the other hand, the RAM is connected to the CPU through the motherboard, making RAM access slower.
Looking at all the advantages of cache memory, one might think that if the cache is so fast, why not just use it instead of RAM. The problem with SRAM is that it is very costly and takes a lot of space on the die compared to RAM. Due to this large size and cost of the cache memory, it is broken down into three separate blocks, namely L1, L2 and L3 cache.
All these caches have different sizes and operate at different speeds—more on that in the next section.
Why do we need cache memory?
Before understanding how a cache works, it is important to understand why it is required. The main reason for the requirement of caches is that the system memory is not fast enough for the processor.
To put things into perspective, a present-day CPU operates at 4Ghz frequency during a turbo boost cycle. During such a cycle, it can process instructions in a quarter of a nanosecond. On the contrary, RAM, which stores data temporarily on the system, takes about 8.8 nanoseconds to send data to the processor.
Looking at the numbers, one can see a considerable gap between the speed at which the data is proceeded by the CPU and the rate at which the RAM can supply it to the CPU. To bridge this gap, a special type of memory is used by a CPU called cache memory.
L1 vs L2 vs L3 cache: Explained
As mentioned in the last section, cache memory is fast, but it has several drawbacks. To overcome these drawbacks, the cache storage on the CPU is divided into three tiers.
The L1 is the fastest but the smallest of all caches. This cache is also placed closest to the processor and provides data to the CPU at the highest possible rate. Next up is the L2 cache, which is larger but slower when compared to the L1 cache. In multiprocessor CPUs, the L1 and L2 caches are independent to a processor core, and the L1 cache is further divided into the data cache and the instruction cache.
Another cache is available for the CPU, and it is the biggest of them all but slowest too — the L3 cache. In the case of multiprocessor systems, the L3 cache is shared by processor cores, and in some cases, it is placed outside the processor die.
The Intel Core i7-10700K CPU has a 64KB L1 cache per core, a 256KB L2 cache, and a 16MB shared L3 cache. The size of the L3 cache has increased drastically over the last few years, but the size of the L1 cache has risen only by a few KBs.
The reason for this is that if the size of the L1 cache increases, the CPU takes a lot of time to find the data, defeating the purpose of a fast cache. In addition to this, a fast L1 cache takes up a lot of space on the CPU die, reducing the area for the placement of other components.
Also read: What is DNS cache and how does it work?
How does data move in your system?
Now that we have a brief overview of how caching systems are used in modern machines, we can look at how data flows in a computer.
The RAM and cache memory on your system is volatile storage systems; hence they cannot store data once the power is turned off. Therefore non-volatile storage mediums like hard drives are required to store your systems data.
These non-volatile systems use magnetic disks or flash memory to store data and send data to the RAM when a program is opened on the system. Thereafter the CPU requests the RAM to transfer data to the cache memory; this data has files and instructions that could be used by the user in the future or are required by the CPU in real-time.
The L1 cache gets the data that the CPU is currently working on; the L2 cache stores files used recently by the processor and could be required in the future. The L3 cache stores data which multiple cores could use for performing tasks
What happens when data is not in the cache?
We know that data is supplied from the cache memory to the CPU and how critical a cache is to a CPU. But what happens when data the CPU requires is not available in the cache.
When something like this happens, it is known as a cache miss, and the system starts to look for the data the CPU needs and move it to the cache. There are three types of cache misses that can occur in the system, and they are as follows.
- Instruction cache miss: In this case, the L1 instruction cache does not have the instruction which needs to be executed by the processor. This cache miss causes the most significant delay as the CPU cannot perform any task as it doesn’t know which instruction to perform. When such a miss occurs, the system starts looking for the instruction in the RAM or the main memory.
- Data read miss: If this cache miss occurs, the CPU cannot get the data it has to work on. This happens when the L1 data cache does not have data. This cache miss has a medium effect on system performance as the CPU can work on other data till the system fetches information the CPU needs.
- Data write miss: Here, the data processed by the processor cannot be written into the cache; this can happen if the L1 data cache is filled. This cache miss causes the slightest delay as the data can be held by the processor in a register and then sent to cache later.
Also read: What is the difference between GPU and FPGA?
How much cache do you need?
Cache memory is quintessential for any CPU, but how much of it do you need? As mentioned earlier, the size of the L1 and L2 cache remains the same in most systems. The only cache you need to look at is the L3 cache, and a bigger cache will offer better performance.
Some high-grade CPU systems can have cache sizes up to 64 MB, but such cache sizes are too costly and are used in server applications. If you are an avid gamer and run a multi-core machine, then a 16MB cache is more than enough for all your requirements.
Also read: How to get Google Maps API key?