Cache lines, tags, index, offset
4.1.10· Coding › Computer Architecture (Deep)
Cache ko address slice kyun karna padta hai?
Main memory (DRAM) bahut badi lekin slow hai (~100 ns). CPU ~0.3 ns/cycle par chalta hai. Agar har load DRAM ka wait karta, toh CPU 99% time idle rehta. Isliye hum CPU ke paas hot data ki tiny copy rakhte hain.
Lekin cache, memory se bahut choti hai, isliye bahut saare memory addresses ko same cache slot share karna padta hai. Hardware ko ek fast, wiring-only tarika chahiye (koi arithmetic nahi, koi search nahi) jo:
- Decide kare ki koi given address kaunse slot mein map hoga → index.
- Confirm kare ki us slot mein jo data baitha hai, woh actually wahi address hai jo hum chahte hain → tag.
- Fetch kiye gaye block ke andar exact byte(s) select kare → offset.
Hardware mein bits ko slice karna free hai (sirf wires), isliye yeh scheme jeeitti hai.
Cache line / block kya hota hai?
Teen fields ko scratch se kaise derive karein
Maan lo ek byte-addressable memory hai -bit addresses ke saath, ek cache jo sets (rows) rakhta hai, aur har line ka size bytes hai.
Step 1 — Offset bits. bytes ke ek block ke andar hum har byte ko naam dena chahte hain. Distinct bytes ki sankhya , isliye hum chahte hain Yeh step kyun? se tak count karne ke liye exactly bits chahiye.
Step 2 — Index bits. Offset hatane ke baad, block number hai . Hum har block number ko sets mein se kisi ek mein map karna chahte hain. Hardware block number ke low bits use karta hai (taaki consecutive blocks sets mein spread ho jaayein): Yeh step kyun? bits, rows ko address karte hain; low bits isliye use hote hain taaki adjacent blocks collide na karein.
Step 3 — Tag bits. Jo bhi address bits bachi hain, woh uniquely identify karti hain ki is set mein map hone wale kitne blocks mein se kaun sa currently stored hai: Yeh step kyun? Index/offset ke liye use na hue saare address bits ko identity verify karne ke liye store aur compare karna padta hai.

Associativity (jahan se aata hai)
Cache mein total lines hoti hain. Unhe ways mein group karo:
- Direct-mapped (-way): har set mein line, isliye .
- -way set-associative: har set mein lines, isliye .
- Fully associative: ek bada set, ⇒ 0 index bits, sab kuch tag hai.
Worked Example 1 — Direct-mapped
Cache: lines, B, direct-mapped, 32-bit addresses.
- Offset bits. Kyun? 64 bytes per line.
- Sets (direct-mapped) ⇒ index bits. Kyun? 1 line per set.
- Tag bits. Kyun? bache hue bits.
Address 0xDEADBEEF = 1101 1110 1010 1101 1011 1110 1110 1111.
Low 14 bits (= 8 index + 6 offset) ko carefully peelо. Address ke aakhri 14 bits hain
11 1110 1110 1111:
- Offset = low 6 bits =
101111=0x2F(line mein byte 47). Kyun? lowest bits. - Index = agle 8 bits =
11111011=0xFB= set 251. Kyun? agle bits. (Boundary dhyan se dekho: offset bottom 6 bits khata hai, isliye index bits 13..6 par hai, jo11111011padhta hai, na ki10111011.) - Tag = top 18 bits, set 251 mein stored value se compare kiya jaata hai.
Worked Example 2 — 4-way set-associative
Same numbers lekin 4-way: , , , 32-bit.
- Offset (unchanged — sirf par depend karta hai).
- Sets ⇒ index bits. Kyun? ab kam sets hain.
- Tag bits. Kyun? index ne 2 bits khoe, tag ne 2 bits paaye.
Worked Example 3 — total bits stored
4-way cache ke liye: har line ek valid bit aur tag bhi store karti hai. Bits per line bits. Total = bits of SRAM. Kyun? overhead (tag+valid) actual silicon cost hai — badi lines overhead ko amortize karti hain lekin agar locality poor ho toh bandwidth waste hoti hai.
Common mistakes (Steel-manned)
Flashcards
Cache single bytes ki jagah puri lines kyun fetch karta hai?
Line size ke liye offset bits ki sankhya?
sets ke liye index bits ki sankhya?
-bit address, sets, -byte lines ke saath tag bits ka formula?
Index ke liye LOW block-number bits kyun use karte hain?
Direct-mapped ka matlab har set mein kitni lines hain?
lines wale -way cache mein kitne sets?
Direct-mapped se -way par jaane par (same size), tag bits mein kitna change?
Fully associative cache mein kitne index bits?
Kya offset size total cache size par depend karta hai?
Kaun sa field row/set select karta hai?
Kaun sa field stored block ki identity confirm karta hai?
64-B lines, 256 sets, 32-bit address ke liye: tag/index/offset?
0xDEADBEEF ke liye 6 offset + 8 index bits ke saath, kaun sa set?
0x2F (byte 47), index 0xFB = set 251.Recall Feynman: 12-saal ke bachche ko samjhao
Socho ek choti shelf apne desk ke paas kuch labeled cubbies ke saath, aur ek bada warehouse door. Jab tum warehouse se koi book laate ho, tum paas ki books ka pura shrink-wrapped pack utha lete ho (yahi ek cache line hai) aur ek cubby mein rakhte ho. Book ka barcode ek lamba number hai. Aakhri kuch digits batate hain ki pack ke andar kaun si book chahiye (offset). Beech ke digits batate hain kaun si cubby mein pack rakhna hai (index). Pehle digits ek sticker hain jo tum cubby par lagate ho taaki baad mein check kar sako "haan, yeh sahi pack hai" (tag). Book dhundhna = cubby par jao (index), sticker check karo (tag), sahi book uthao (offset). Fast, koi searching nahi!
Connections
- Cache associativity & replacement policies
- Spatial and temporal locality
- Memory hierarchy and latency numbers
- Virtual vs physical addressing (VIPT/PIPT)
- Cache coherence (MESI)
- TLB and page tables — same "address split karo, lookup karo, verify karo" pattern