Course contentsShow
← Computer Science
Lesson 1126 of 2,87226. Memory Hierarchy and CachingFree lesson

Direct-Mapped Caches

Learn how direct-mapped caches assign each memory block to exactly one cache location using modulo arithmetic.

Direct-Mapped Caches

What you'll learn: How direct-mapped caches use a simple modulo calculation to assign each memory block to exactly one cache location.

The Core Idea

In a direct-mapped cache, every block of main memory maps to exactly one specific location in the cache. There's no choice involved—given a memory address, you can immediately calculate which cache line will hold it using modulo arithmetic.

Think of it like assigned seating at a theater where your seat number is determined by a simple rule: seat = ticket_number % total_seats. If you have ticket #137 and there are 8 seats, you sit in seat 1 (137 % 8 = 1). Every time someone with ticket #137, #145, or #153 arrives, they all must use that same seat 1.

How It Works

Given a memory address and a cache with N lines:

  1. Calculate the cache index: index = (block_address) % N
  2. Store (or look for) that block at cache line index

For example, with 8 cache lines:

  • Memory block 0 → cache line 0
  • Memory block 1 → cache line 1
  • Memory block 8 → cache line 0 (wraps around)
  • Memory block 9 → cache line 1
  • Memory block 27 → cache line 3 (27 % 8 = 3)

The Trade-off

This scheme is extremely fast—just one modulo operation tells you exactly where to look. However, if your program frequently accesses blocks 0 and 8, they'll keep evicting each other from cache line 0, causing conflict misses even when other cache lines sit empty.

Key Takeaway: Direct-mapped caches use modulo arithmetic to assign each memory block to one fixed cache location, maximizing speed but risking conflicts when multiple blocks compete for the same spot.