5.4.7Memory Hierarchy & Caches

Write-allocate vs no-allocate

2,938 words13 min readdifficulty · medium1 backlinks

What is Write-Allocate?

Write-allocate (also called fetch-on-write) is a cache policy that says: on a write miss, fetch the block from memory into the cache, THEN perform the write.

Why fetch the entire block?

Because caches operate on block granularity, not individual bytes. The cache line size is typically 64 bytes. Even if you're writing just 1 byte, the cache needs the full block to maintain coherence and support future reads to neighboring addresses.

What is No-Allocate?

No-allocate (also called write-around or write-no-allocate) is a cache policy that says: on a write miss, write directly to the next level of memory WITHOUT bringing the block into cache.

The written data goes "around" the cache.

Why Does This Matter?

The choice between write-allocate and no-allocate affects:

  1. Cache pollution: No-allocate avoids wasting cache space on data that won't be reused
  2. Memory bandwidth: Write-allocate requires an extra memory read on write miss
  3. Latency: Write-allocate has higher latency on write misses (read + write vs just write)
  4. Hit rate on subsequent accesses: Write-allocate increases hits if data is reused soon

Typical Pairing with Write Policies

Derivation: Why Write-back Needs Write-allocate

Let's think through what happens with write-back + no-allocate:

  1. Write miss occurs to address A
  2. No-allocate policy: Don't bring A into cache
  3. Write-back policy: Only write to memory when evicting dirty line

Problem: How do we handle the write to A? We can't store it in cache (no-allocate), and we can't defer it (write-back means write later). We're forced to write immediately memory, which is actually write-through behavior!

Therefore, write-back requires write-allocate to be consistent.

When to Use Each Policy

Write-allocate advantages:

  • Better for workloads with high temporal/spatial locality
  • Reduces memory traffic when block is reused
  • Enables write-back optimization (batch dirty writes)

No-allocate advantages:

  • Better for streaming writes (large sequential data written once)
  • Avoids cache pollution from one-time writes
  • Lower latency on write miss (no fetch delay)
  • Used in streaming stores or uncached writes in some ISAs

Performance Tradeoffs

| Metric | Write-Allocate | No-Allocate | |-----|-------------| | Write miss latency | Higher (fetch + write) | Lower (write only) | | Memory read traffic | Higher (fetches on miss) | Lower (no fetches) | | Memory write traffic | Lower (write-back batching) | Higher (every miss) | | Cache utilization | Higher (blocks cached) | Lower (less pollution) | | Best for | High locality | Low locality |

Recall Feynman Explanation (Simple)

Imagine your desk is your cache, and your bookshelf is memory.

Write-allocate is like this: You need to write notes on a book that's not on your desk. So you:

  1. Get the book from the shelf (fetch)
  2. Put it on your desk (allocate)
  3. Write your notes in it
  4. Keep it on your desk in case you need it again soon

No-allocate is like this: You need to write notes on a book that's not on your desk. So you:

  1. Walk over to the shelf
  2. Write your notes directly in the book on the shelf
  3. Leave it there (don't bring it to your desk)

Which is better?

  • If you're going to use that book again in the next 5 minutes → bring it to your desk (write-allocate)
  • If you're just writing one quick note and won't touch it again → write it on the shelf directly (no-allocate)

Your desk has limited space, so you don't want to fill it with books you won't use again!

Connections

  • Write-back vs Write-through: The write propagation policy that pairs with allocate decisions
  • Cache Line Size: Determines the granularity of allocation on write-allocate
  • Temporal vs Spatial Locality: Principles that guide the choice between policies
  • Streaming Stores: ISA instructions that force no-allocate behavior
  • Cache Pollution: Problem that no-allocate helps avoid
  • Write Combining Buffers: Optimization that batches writes before memory access
  • Dirty Bit: Tracking mechanism required for write-back + write-allocate

#flashcards/hardware

What happens on a write miss with write-allocate policy?
Fetch the entire block from memory into cache, then perform the write in the cached block. Mark the line as valid (and dirty if write-back).
What happens on a write miss with no-allocate policy?
Write the data directly to the next level of memory without allocating a cache line or fetching the block.
Why does write-back require write-allocate?
Write-back defers writes until eviction, which requires the data to be in cache. If we don't allocate on write miss, we have nowhere to defer the write to, forcing immediate memory write (which contradicts write-back).
What is the standard pairing for write-through caches?
Write-through can pair with EITHER write-allocate or no-allocate, depending on expected temporal locality of writes.
When is no-allocate better than write-allocate?
When writes have low temporal locality (streaming writes, one-time initialization, I/O operations). Avoids cache pollution and fetch bandwidth waste.
What is the main disadvantage of write-allocate on write miss?
Higher latency due to fetching the block from memory before writing, and increased memory read traffic.
What is cache pollution in the context of write policies?
When write-allocate brings blocks into cache that won't be reused, evicting potentially useful data and wasting cache space.
What is a non-temporal store instruction?
An ISA instruction (like x86 movnti) that writes to memory with no-allocate behavior, bypassing the cache to avoid pollution.
Why does write-allocate fetch the entire block even for a single-byte write?
Caches operate at block granularity. Other bytes in the block might be accessed later, so the cache needs the full block to maintain coherence and support spatial locality.
What optimization can skip the fetch in write-allocate, and what should it NOT be confused with?
Full-line-write optimization (write-validate): if a write covers the entire cache line, skip the fetch and directly overwrite. Do NOT confuse it with write-combining, which merges adjacent writes into bursts before issuing to memory (a write-buffering technique, not a fill policy).

Concept Map

option A

option B

fetches block from memory

bypasses cache

guides choice

extra read then write

avoids reuse waste

required by

needs data in cache for dirty bits

pairs with

can also pair with

Write miss occurs

Write-allocate / fetch-on-write

No-allocate / write-around

Block loaded into cache

Write direct to memory

Temporal locality

Higher latency & bandwidth

Less cache pollution

Write-back policy

Write-through policy

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, is topic ka core sawaal bahut simple hai: jab hum kisi memory location pe likhna (write) chahte hain aur wo location cache mein present nahi hai (yani write miss ho gaya), tab hum kya karein? Do choices hain. Write-allocate kehta hai ki pehle us block ko memory se cache mein le aao, phir likho — jaise grocery shopping mein milk cart mein daal ke rakhte ho future ke liye. No-allocate kehta hai ki bas seedha memory mein likh do, cache ko bother mat karo — jaise saman seedha car ki dikki mein daal diya. Ye poora decision ek cheez pe depend karta hai: temporal locality, yani kya ye data thodi der baad phir se chahiye hoga ya nahi?

Ab yaad rakho ek important cheez — cache hamesha poore block (typically 64 bytes) pe kaam karta hai, na ki single byte pe. Toh agar tum sirf 1 byte likhna chahte ho aur write-allocate use kar rahe ho, tab bhi cache ko pura block memory se fetch karna padega. Isliye write-allocate mein ek extra memory read ka cost aata hai, aur latency zyada hoti hai. Lekin agar wahi data ya uske neighbouring addresses jaldi phir se access hone waale hain, toh ye extra cost worth it hai kyunki aage ke accesses cache hit ban jaate hain. No-allocate ka fayda ye hai ki wo cache ko un data se pollute nahi karta jo dobara use nahi hone waala, aur cache space bachta hai.

Sabse important baat jo exam aur real hardware dono mein aati hai: write-back policy hamesha write-allocate ke saath hi pair hoti hai. Iska logic samajh lo — write-back ka matlab hai ki hum data ko turant memory mein nahi likhte, cache mein rakh ke dirty bit track karte hain aur baad mein evict hone pe likhte hain. Ab agar tum no-allocate use karoge, toh write miss pe data cache mein aayega hi nahi, matlab tumhe turant memory mein likhna padega — jo actually write-through ban gaya, aur write-back ka pura point khatam! Isliye write-back ko kaam karne ke liye write-allocate chahiye hi chahiye. Dusri taraf write-through dono ke saath chal sakta hai — high locality data ke liye write-allocate, aur low locality data ke liye no-allocate. Ye combinations samajhna zaroori hai kyunki yahi memory hierarchy ki performance ko real mein decide karte hain.

Go deeper — visual, from zero

Test yourself — Memory Hierarchy & Caches

Connections