Worked examples — Write-allocate vs no-allocate
5.4.7 · D3· Hardware › Memory Hierarchy & Caches › Write-allocate vs no-allocate
Yeh page Write-allocate vs no-allocate ki practice ground hai. Parent note ne idea explain kiya; yahan hum har us situation ko grind karte hain jo ek write miss create kar sakta hai aur har baar memory traffic count karte hain.
Kuch bhi karne se pehle, in words ko pin down karte hain taaki koi symbol bina wajah use na ho.

Scenario matrix
Har write-miss situation jo yeh topic aap par throw kar sakta hai, in cells mein se ek mein aati hai. Jo examples follow karte hain woh sab us cell ke saath tagged hain jo wo cover karte hain, aur mil kar yeh sab ko hit karte hain.
| # | Case class | Kya special hai | Policy jo shine kare | Example |
|---|---|---|---|---|
| A | Single isolated write, jald reuse hoga | temporal locality present | write-allocate + write-back | Ex 1 |
| B | Single isolated write, kabhi reuse nahi | zero temporal locality | no-allocate + write-through | Ex 2 |
| C | Ek block par writes ka cluster, phir read | spatial locality | write-allocate | Ex 3 |
| D | Kai blocks mein streaming writes | data cache se bada | no-allocate / Streaming Stores | Ex 4 |
| E | Degenerate: same address par do baar write | kya 2nd hit hai? | dono — hit logic test karta hai | Ex 5 |
| F | Boundary: block edge par straddling write | offset line size ke paas | block granularity reveal karta hai | Ex 6 |
| G | Eviction / dirty writeback | line already dirty, conflict | write-back cost accounting | Ex 7 |
| H | Word-problem (real workload) | ek policy choose karo & justify karo | decision framework | Ex 8 |
| I | Exam twist: forbidden combo | write-back + no-allocate | dikhata hai yeh illegal kyun hai | Ex 9 |
Jab tak alag na bataya jaaye, poore note mein yeh cache use hogi: 4 lines, 8 bytes/line, direct-mapped, shuruaat mein khaali.
Cell A — isolated write, jald reuse hoga
Cell B — isolated write, kabhi reuse nahi hoga
Cell C — ek block par cluster, phir read (spatial locality)

Cell D — kai blocks mein streaming
Statement. 0x00, 0x08, 0x10, 0x18, 0x20, 0x28, 0x30, 0x38 (8 distinct blocks) mein se har ek mein ek baar write karo, kabhi read mat karo. Cache sirf 4 lines hold karti hai. Policies compare karo.
Forecast: sirf 4 lines aur 8 blocks ke saath, kya write-allocate kuch bhi useful rakh paata hai?
Write-allocate + write-back:
- Blocks
0x00,0x08,0x10,0x18indices par map hote hain: 4 fetches (4 reads), 4 dirty lines. 0x20→ index : conflict — dirty line 0 evict hoti hai → 1 write-back, phir fetch ke liye 1 read.0x28(idx1),0x30(idx2),0x38(idx3) ke liye bhi same: har ek 1 writeback + 1 read hai. Itna bura kyun? Hum koi data nahi rakhte (kabhi read nahi karte), phir bhi fetch aur evict karne ki cost pay karte hain — pure overhead. → reads , writes (evictions) . Total = 12.
No-allocate + write-through:
- 8 writes cache completely bypass karte hain → 8 writes, 0 reads. Total = 8.
Verify karo: no-allocate 8 vs write-allocate 12. Streaming mein zero reuse hota hai, toh allocate karna sirf fetch + eviction cost add karta hai. Exactly isliye Streaming Stores aur Write Combining Buffers exist karte hain.
Cell E — degenerate: same address par do baar write
Statement. Write-allocate + write-back ke under 0x28 par 0xAA phir 0xBB write karo. Kya 2nd write hit hai?
Forecast: doosre write par hit ya miss?
- Write 1: miss → block fetch karo (1 read),
0xAAwrite karo, dirty. - Write 2: line 1 ab valid → hit,
0xBBoverwrite karo, abhi bhi dirty (0 traffic). Hit kyun? Write 1 se allocation ne block pehle se hi la diya tha.
Verify karo: reads = 1, writes = 0. Total = 1. Block do baar likha jaata hai lekin memory ek baar hit hoti hai — Dirty Bit "baad mein save zaroori hai" track karta hai bina har write par extra traffic ke.
Cell F — boundary: block edge par straddling write
Statement. Ek program address 0x07 par 2-byte value write karta hai (bytes 0x07 aur 0x08) write-allocate ke under. Kitne blocks touch hote hain?
Forecast: ek block ya do?
- Byte
0x07: block , index 0. - Byte
0x08: block , index 1. Do kyun?0x08agli line ka pehla byte hai; 2-byte write boundary straddle karta hai. - Dono miss hain → 2 fetches (2 reads), dono lines allocate aur dirty hoti hain. Dono fetch kyun? Har byte ek alag line mein hai, aur cache sirf whole lines move karta hai.
Verify karo: reads = 2. Ek chhota sa store do block fetches ki cost le aaya sirf isliye kyunki yeh line edge cross kiya — classic misalignment penalty.
Cell G — dirty line ki eviction
Statement. Write-allocate + write-back ke under: 0x28 (index 1) write karo, phir 0x48 write karo. Total traffic kya hai?
Forecast: 0x48 ka index — aur kya purani dirty line cost karti hai?
0x28write karo: index 1, miss → fetch karo (1 read), dirty line 1.0x48write karo: , index . Same slot, occupied by ek dirty line. Conflict kyun? Alag block, same index — direct-mapped mein har index par sirf ek slot hota hai.- Line 1 evict karo: yeh dirty hai ⇒ write it back (1 write), phir
0x48ke block ke liye fetch karo (1 read), write karo, dirty. Pehle write back kyun? Write-back ne pehle wala0x28change kabhi nahi gumaya; slot reuse hone se pehle yeh save hona zaroori hai, warna data corrupt hoga.
Verify karo: reads = 2, writes = 1. Total = 3. Write-through se compare karo, jo 0x28 write immediately pay karta aur kabhi dirty writeback ki zaroorat nahi hoti.
Cell H — real-world word problem
Statement. Ek logging system sequentially ek file buffer mein 64-byte records append karta hai, disk par flush hote hain aur CPU kabhi re-read nahi karta. Records total 100 MB hain; cache 1 MB hai. Kaun si policy, aur relative traffic estimate karo?
Forecast: allocate karein ya nahi?
- Access pattern: pure sequential writes, koi reads nahi ⇒ zero temporal, aur data (100 MB) ≫ cache (1 MB) ⇒ koi line survive nahi karti reuse hone ke liye. Kyun matter karta hai: yeh streaming signature hai (Cell D at scale).
- Framework apply karo parent se: write-allocate tabhi use karo jab high ho. Yahan yeh ≈ 0 hai.
- Decision: no-allocate (non-temporal stores). Har 64-byte record ke liye ek write.
- Write-allocate additionally har 64-byte block read karta (100 MB ka pointless fetch) aur baad mein evict karta.
- Toh write-allocate traffic ≈ no-allocate ka (har useful write ke liye ek fetch + ek writeback).
Verify karo: no-allocate ≈ 100 MB written; write-allocate ≈ 100 MB read + 100 MB written = 200 MB. Ratio 2:1 no-allocate ke favour mein — "streaming ⇒ bypass cache" rule se match karta hai aur 1 MB cache ki Cache Pollution se bachata hai.
Cell I — exam twist: forbidden combination
Statement. Ek student write-back + no-allocate propose karta hai. Ek write miss trace karo aur contradiction dikhao.
Forecast: kahan toot ta hai?
Apar write miss.- No-allocate kehta hai:
Ako cache mein mat daalo. - Write-back kehta hai: memory tab hi touch karo jab ek dirty cached line evict ho. Yeh deadlock kyun hai: data cache mein nahi hai (step 2), toh write-back ke paas defer karne ke liye kuch nahi — phir bhi write-back abhi memory mein likhne se mana karta hai.
- Iska single escape hai memory mein turant likhna — lekin yeh literally write-through behaviour hai.
Verify karo: combination write-through mein collapse ho jaata hai, toh yeh ek alag valid policy nahi hai. Isliye parent ki identity hold karti hai. Dekho Write-back vs Write-through.
Recap
Recall Pure streaming writes ke liye kaun sa cell jeet ta hai?
No-allocate — zero reuse ka matlab hai allocation sirf fetch + eviction cost add karta hai. (Cell D / Ex 4)
Recall Ex 3 mein write-allocate 1 nahi, 2 reads kyun cost hua?
0x100–0x10C do blocks (0x20 aur 0x21) par span karta tha, har ek ek baar fetch hua.
Prior allocation ke baad write-allocate hit ka traffic count
Dirty line eviction par extra cost kyun aati hai
Allocate for Again (reuse), Around for Away-forever (streaming).