5.4.4 · HinglishMemory Hierarchy & Caches

Cache line size and tags

1,993 words9 min readRead in English

5.4.4 · Hardware › Memory Hierarchy & Caches

Cache Lines Kyun Exist Karte Hain

Problem: Main memory CPU se ~100× slower hoti hai. Ek ek byte fetch karna wide data bus (64–128 bits) ko waste karta hai aur locality exploit nahi karta.

Solution: Fixed-size ==cache lines== (ya blocks) transfer karo. Modern x86 64-byte lines use karta hai; ARM often 32 ya 64 bytes use karta hai. Ek slow memory access pay karo, lekin phir 64 bytes cache speed pe serve karo.


Ek Address ki Anatomy

Har memory address teen fields mein split hota hai (ek direct-mapped cache ke liye; set-associative "line index" ki jagah "set index" use karta hai):

Yeh decomposition kyun?

  1. Offset se hum line fetch karne ke baad requested byte extract kar sakte hain.
  2. Index hardware ko batata hai ki kahan dekhna hai (jaise ek hash table bucket).
  3. Tag confirm karta hai ki jo humne find kiya woh sahi data hai (collision detection).

Derivation: Har Field ke Liye Kitne Bits?

Diya hua:

  • Address space: bits (e.g., 32-bit ya 64-bit CPU).
  • Cache size: bytes.
  • Line size: bytes (hamesha power of 2).
  • Associativity: ways (direct-mapped ⇒ ).

Step 1: Cache lines ki sankhya = .

Step 2: Sets ki sankhya = . (Direct-mapped ke liye, toh sets = lines.)

Step 3:

i = \log_2\left(\frac{C}{L \cdot A}\right) \quad \text{(index/set bits)}


Line Size Kyun Matter Karta Hai


Tag Storage Overhead

Har cache line ko chahiye:

  • Data: bytes.
  • Tag: bits.
  • Valid bit: 1 bit (kya yeh line initialized hai?).
  • Dirty bit (write-back): 1 bit (kya data modify hua hai?).

Total overhead per line: bits (set-associative ke replacement policy bits ignore karke).


Common Mistakes


Active Recall

Recall Kisi 12-saal ke bachche ko explain karo

Socho tumhara desk (cache) 10 folders (cache lines) rakh sakta hai, aur har folder mein 8 pages (bytes) hain. Tumhare paas ek bada filing cabinet (main memory) hai jisme hazaaron folders hain.

Jab tum folder 237 ka page 5 maangoge, cache sirf page 5 nahi laata—woh poora folder 237 (saare 8 pages) le aata hai. Kyun? Kyunki shayad tumhe aage page 6 aur 7 bhi chahiye honge. Yahi spatial locality hai.

Offset batata hai folder mein kaun sa page chahiye (page 5). Index batata hai desk pe kaun sa slot check karna hai (slot 7). Tag folder pe ek label hai jo kehta hai "yeh cabinet ka folder 237 hai" taaki tum jaano sahi folder mila.

Agar koi folder 450 maange aur woh bhi desk slot 7 pe map hota hai, toh folder 237 ko nikaalna padega (ek conflict). Bade folders (badi line size) matlab desk pe kam folders samayenge, toh zyada conflicts. Chhote folders matlab tum baar baar cabinet ke chakkar kaatoge. 64 pages per folder zyaatar homework ke liye sweet spot hai.


Mnemonic


Connections

  • 5.4.01-Cache-fundamentals – caching kyun zaroori hai (locality principles)
  • 5.4.02-Direct-mapped-caches – in address fields use karne wali sabse simple cache organization
  • 5.4.03-Set-associative-caches – index ban jaata hai "set index", multiple tags per set
  • 5.4.05-Cache-miss-types – line size compulsory vs. conflict misses ko kaise affect karta hai
  • 5.4.08-Write-policies – write-back caches mein dirty bit ka usage
  • 6.2.01-Virtual-memory-pages – page tables mein bhi similar tag-index decomposition hoti hai
  • 3.1.04-Spatial-localitywajah ki cache lines multi-byte hoti hain

#flashcards/hardware

Cache system mein memory address ke teen fields kaun se hote hain?
Tag (kaun sa block), Index (kaun si line/set), Offset (line mein kaun sa byte)
Agar cache mein 1024 lines hain aur 32-byte line size hai, toh kitne offset bits?
bits
Agar cache mein 1024 lines hain aur 32-byte line size hai, toh kitne index bits?
bits
32-bit address space mein 10 index bits aur 5 offset bits hone par kitne tag bits honge?
bits
Badi cache lines tag overhead kyun reduce karti hain?
Same cache size ke liye kum total lines → store karne ke liye kam tags (har line ke saath zyada data per tag)
Bahut badi cache lines (e.g., 256 bytes) ka kya nuksan hai?
Kum total lines → zyada conflict misses; lamba miss penalty; unused data fetch karna bandwidth waste karta hai
"Offset" field kya select karta hai?
Cache line ke andar byte (ya word) (line index aur tag se locate hone ke baad)
"Index" field kya select karta hai?
Check karne ke liye cache line (ya set, set-associative caches mein)
"Tag" field kya karta hai?
Identify karta hai ki is cache line mein kaun sa memory block stored hai (hit/miss detection enable karta hai)
Ek byte ki jagah poora cache line kyun transfer karte hain?
Spatial locality exploit karta hai: adjacent bytes jald hi chahiye honge; slow memory access ko multiple bytes pe amortize karta hai
Modern x86 CPUs mein typical cache line size kya hai?
64 bytes
64-byte lines wali 64 KiB direct-mapped cache mein kitni cache lines hongi?
lines
1024 cache lines ke liye kitne index bits?
bits

Concept Map

motivates

motivates

transfers block per

splits into

splits into

splits into

b = log2 L

selects

identifies

t = N - i - b

i = log2 sets

Spatial locality

Cache line 64 bytes

Slow main memory

One slow memory access

Memory address N bits

Offset b bits

Index i bits

Tag t bits

Cache line slot

Memory block

Hit or miss check

Number of sets C over L times A