COA Notes
Direct-mapped, fully associative, and set-associative cache mapping with examples and trade-offs.
Introduction
When a block of data arrives from main memory, WHERE in the cache should it go? This seemingly simple question has three answers, each with different trade-offs. The mapping technique determines which cache locations a memory block can occupy, affecting hit rate, hardware complexity, and access speed. Understanding these techniques is essential for cache design and for writing cache-friendly code.
The Problem
Main memory is much larger than cache. If memory has 1 million blocks and cache has 1,000 lines, each cache line must serve multiple memory blocks. The mapping technique defines the relationship between memory blocks and cache lines.
Direct-Mapped Cache
Concept
Each memory block maps to exactly one cache line. The mapping is determined by the block address modulo the number of cache lines:
Example
Cache with 8 lines, memory blocks 0-31:
| Block 0 | Line 0 Block 8 → Line 0 Block 16 → Line 0 |
| Block 1 | Line 1 Block 9 → Line 1 Block 17 → Line 1 |
| Block 2 | Line 2 Block 10 → Line 2 Block 18 → Line 2 |
| Block 7 | Line 7 Block 15 → Line 7 Block 23 → Line 7 |
Address Breakdown
Lookup Process
- Extract index → go directly to that cache line
- Compare tag with stored tag
- If match AND valid bit = 1 → HIT (return data at offset)
- If no match → MISS (fetch from memory, replace this line)
Advantages
- Simple hardware: Only one comparator needed (one possible location)
- Fast access: No searching — go directly to the indexed line
- Low power: Only one tag to check
Disadvantages
- Conflict misses: Multiple blocks mapping to same line thrash each other
- Example: Alternating access to blocks 0 and 8 causes 100% miss rate (both map to line 0!)
Fully Associative Cache
Concept
A memory block can go in ANY cache line. No fixed mapping — search all lines.
Lookup Process
- Compare requested tag with ALL stored tags simultaneously
- If any match → HIT (return data from that line)
- If no match → MISS (fetch, replace using LRU/random policy)
Address Breakdown
| Tag | Block Offset |
|---|---|
| (all remaining bits) |
No index field! Any block can go anywhere.
Advantages
- No conflict misses: Any block fits anywhere — maximum flexibility
- Best hit rate: Only compulsory and capacity misses occur
Disadvantages
- Expensive hardware: Need one comparator PER cache line (compare tag with ALL lines in parallel)
- Slow: Many parallel comparisons add delay
- Impractical for large caches: 1000 lines = 1000 comparators
Practical Use
- Very small caches (TLB with 32-64 entries)
- Specialized caches where maximum hit rate is critical
Set-Associative Cache
Concept
A compromise: the cache is divided into sets, each containing multiple ways. A memory block maps to a specific SET (like direct-mapped) but can go in ANY WAY within that set (like fully associative within the set).
N-way set-associative: Each set has N lines (N ways)
| │ Set 0 | [Way 0] [Way 1] │ |
| │ Set 1 | [Way 0] [Way 1] │ |
| │ Set 2 | [Way 0] [Way 1] │ |
| │ Set 3 | [Way 0] [Way 1] │ |
Address Breakdown
Lookup Process
- Extract set index → identify the correct set
- Compare tag with ALL ways in that set (N comparisons)
- If any way matches → HIT
- If no match → MISS (replace one way in the set using replacement policy)
Relationships
- 1-way set-associative = Direct-mapped (1 way per set = each set has one line)
- N-way with 1 set = Fully associative (one set containing all lines)
- N-way with k sets = Standard set-associative (most common)
Common Configurations
| Configuration | Ways | Sets | Total Lines |
|---|---|---|---|
| Direct-mapped | 1 | 1024 | 1024 |
| 2-way SA | 2 | 512 | 1024 |
| 4-way SA | 4 | 256 | 1024 |
| 8-way SA | 8 | 128 | 1024 |
| Fully assoc. | 1024 | 1 | 1024 |
Why N-way Reduces Conflicts
In direct-mapped, blocks 0 and 8 fight for the same line. In 2-way, they can both be in the same SET (one in way 0, one in way 1) — no conflict!
Comparison Table
| Feature | Direct-Mapped | Set-Associative | Fully Associative |
|---|---|---|---|
| Placement | One fixed location | One set, any way | Anywhere |
| Comparators | 1 | N (ways per set) | All lines |
| Conflict misses | High | Moderate | None |
| Hit rate | Lowest | Middle | Highest |
| Hardware cost | Lowest | Middle | Highest |
| Speed | Fastest | Middle | Slowest |
| Typical use | L1 (simple, fast) | L2, L3 | TLB |
Practical Design Choices
Modern processors typically use:
- L1 Cache: 4-8 way set-associative (balance speed and hit rate)
- L2 Cache: 8-16 way set-associative
- L3 Cache: 12-20 way set-associative (maximize hit rate, latency less critical)
- TLB: Fully associative (small, critical for every access)
Replacement Policies (for associative caches)
When all ways in a set are full and a new block arrives, which way to evict?
- LRU (Least Recently Used): Evict the way accessed longest ago. Best hit rate but complex tracking.
- Pseudo-LRU: Approximation of LRU with simpler hardware (tree-based)
- Random: Surprisingly good performance, trivial hardware
- FIFO: Evict oldest loaded block
Key Takeaways
- Direct-mapped: Simple and fast but suffers conflict misses (one location per block)
- Fully associative: Best hit rate but impractical for large caches (too many comparators)
- Set-associative: The practical compromise — reduces conflicts while limiting hardware
- Higher associativity → fewer conflict misses but slower access and more power
- Modern L1 caches use 4-8 way; L2/L3 use 8-20 way
- Set index determines WHICH set; tag comparison determines WHICH way (if any) hits
- The choice of mapping technique directly impacts miss rate and access time
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Cache Mapping Techniques.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Computer Organization & Architecture topic.
Search Terms
computer-organization, computer organization & architecture, computer, organization, memory, cache, mapping, techniques
Related Computer Organization & Architecture Topics