Cache Basics and Cache Mapping Techniques
Modern processors execute billions of instructions every second. However, processor speed has increased much faster than main memory (RAM) speed. This difference, known as the memory wall, creates a performance bottleneck because the CPU often has to wait for data from slower memory. To solve this problem, computer systems use cache memory—a small, high-speed memory located between the CPU and the main memory.
In this article, we will understand the fundamentals of cache memory, the principle of locality, cache organization, and the three major cache mapping techniques.
What is Cache Memory?
Cache memory is a small, high-speed memory that stores copies of frequently accessed data from the main memory. Since accessing cache is much faster than accessing RAM, the CPU first checks whether the required data is available in the cache.
If the data is found in the cache, it is called a cache hit. Otherwise, the CPU fetches the data from the main memory, resulting in a cache miss.
CPU │ ▼ Cache Memory │ ▼ Main Memory
The primary objective of cache memory is to reduce the Average Memory Access Time (AMAT).
Why Do We Need Cache Memory?
Without cache memory, every memory request would go directly to RAM, causing significant delays.
For example,
- CPU access time: ~1 ns
- Main memory access time: ~100 ns
This means the CPU would spend most of its time waiting for data.
Cache memory bridges this speed gap by storing recently and frequently used information.
Principle of Locality
Cache memory works because most programs do not access memory randomly. Instead, they follow predictable patterns known as the Principle of Locality.
There are two important types of locality.
1. Temporal Locality
Temporal locality states that recently accessed data is likely to be accessed again in the near future.
Example
for(int i=0; i<1000; i++) { sum += A[0]; }
The value A[0] is accessed repeatedly.
Keeping it in cache avoids multiple accesses to main memory.
2. Spatial Locality
Spatial locality states that memory locations near a recently accessed location are likely to be accessed soon.
Example
for(int i=0; i<1000; i++) { sum += A[i]; }
After accessing A[0], the processor will likely access A[1], A[2], and so on.
Therefore, cache loads an entire block instead of a single byte.
Cache Line (Cache Block)
The smallest unit transferred between the main memory and the cache is called a cache line or cache block.
Byte0 Byte1 Byte2 Byte3 ... Byte(B−1) |---- Cache Block ----| Block Size = B Bytes
Instead of fetching a single byte, the processor fetches an entire block.
Common cache block sizes are:
- 32 Bytes
- 64 Bytes
- 128 Bytes
Important Cache Terminology
Cache Size (C)
The total storage capacity of the cache.
Example:
Cache Size = 32 KB
Block Size (B)
The number of bytes stored in one cache line.
Example:
Block Size = 64 Bytes
Number of Cache Blocks
The number of blocks present in the cache is
Number of Blocks=BCExample
Cache Size = 32 KB Block Size = 64 Bytes Blocks = 32768 / 64 = 512
Memory Address Breakdown
Every memory address is divided into three fields.
Tag
Used to identify whether the requested block is actually stored in the selected cache location.
Index
Selects the cache line or cache set.
Block Offset
Selects the required byte inside the cache block.
Cache Mapping Techniques
A cache cannot store the entire main memory.
Therefore, we need a mapping technique to determine where a memory block should be placed inside the cache.
There are three major cache mapping methods.
- Direct Mapping
- Set Associative Mapping
- Fully Associative Mapping
1. Direct Mapped Cache
In a Direct Mapped Cache, every memory block maps to exactly one cache line.
Notice that multiple memory blocks compete for the same cache line.
Advantages
- Very simple implementation
- Fast hardware
- Low cost
- Easy indexing
Disadvantages
- High conflict misses
- Frequent replacement when multiple blocks map to the same line
2. Set Associative Cache
Set Associative Cache is a compromise between Direct Mapping and Fully Associative Mapping.
The cache is divided into sets.
Each set contains multiple cache lines called ways.
However, it can be stored in any way inside that set.
The mapping equation is
Set Index=(Block Number)mod(Number of Sets)
Advantages
- Fewer conflict misses
- Better cache utilization
- Good balance between performance and hardware complexity
Disadvantages
- Slightly slower than Direct Mapping
- Requires multiple tag comparisons
3. Fully Associative Cache
In a Fully Associative Cache, any memory block can be placed in any cache line.
Memory Block │ ▼ Any Cache Line
There is no fixed mapping.
The hardware searches all cache lines simultaneously.
Advantages
- Lowest conflict misses
- Highest flexibility
- Best cache utilization
Disadvantages
- Expensive hardware
- Requires comparison with every cache line
- High power consumption
Comparison of Cache Mapping Techniques
Cache Hit and Cache Miss
Whenever the CPU requests data, two outcomes are possible.
Cache Hit
The requested data is found in the cache.
Result:
- Fast access
- Low latency
- Better performance
Cache Miss
The requested data is not available in the cache.
Result:
- Slower access
- CPU waits for RAM
- Performance decreases
Goal of Cache Memory
The primary goal of cache memory is to reduce the Average Memory Access Time (AMAT) by increasing the cache hit rate.
A well-designed cache should:
- Maximize cache hits
- Minimize cache misses
- Reduce CPU waiting time
- Improve overall system performance
Key Takeaways
- Cache memory is a small, fast memory placed between the CPU and main memory.
- It stores frequently used data to reduce memory access time.
- Cache performance relies on Temporal Locality and Spatial Locality.
- Data is transferred in units called cache blocks (cache lines).
- Memory addresses are divided into Tag, Index, and Block Offset.
- Direct Mapped Cache is simple and fast but suffers from conflict misses.
- Set Associative Cache offers a balance between performance and hardware complexity.
- Fully Associative Cache provides maximum flexibility with the least conflict misses but requires the most hardware.
- The effectiveness of a cache is measured by its cache hit rate, which directly impacts processor performance.
Understanding these cache fundamentals is essential before moving on to advanced topics such as cache replacement policies (LRU, FIFO, Random), write-through vs. write-back caches, multi-level cache hierarchies (L1, L2, L3), and Average Memory Access Time (AMAT), which are typically covered in the next stages of computer architecture and cache design.
- Get link
- X
- Other Apps
Labels
Cache- Get link
- X
- Other Apps
Comments
Post a Comment