<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>Learning About Garbage Collection on Haseeb Majid</title>
    <link>https://haseebmajid.dev/series/learning-about-garbage-collection/</link>
    <description>Haseeb Majid&#39;s blog</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en</language>
    <lastBuildDate>Sun, 06 Dec 2026 00:00:00 +0000</lastBuildDate><atom:link href="https://haseebmajid.dev/series/learning-about-garbage-collection/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Learning Notes: Prefetching Hides the Wait</title>
      <link>https://haseebmajid.dev/posts/2026-12-06-til-prefetching-hides-the-wait/</link>
      <pubDate>Sun, 06 Dec 2026 00:00:00 +0000</pubDate>
      <guid>https://haseebmajid.dev/posts/2026-12-06-til-prefetching-hides-the-wait/</guid>
      <description>&lt;p&gt;&lt;em&gt;Learning Notes are short posts about the bit I got stuck on, the example I tried, and what made it click.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;I&amp;rsquo;m learning more about garbage collection, starting with Go&amp;rsquo;s Green Tea collector. I got stuck on
prefetching: how does the CPU know what to fetch if it hasn&amp;rsquo;t read the data yet?&lt;/p&gt;
&lt;p&gt;Say we have a struct &lt;code&gt;B&lt;/code&gt; with a pointer &lt;code&gt;B.Next&lt;/code&gt; to another struct &lt;code&gt;C&lt;/code&gt;. Reading &lt;code&gt;B.Next&lt;/code&gt; gives us C&amp;rsquo;s address,
not C&amp;rsquo;s contents. So we can ask the CPU to start fetching C while it scans B&amp;rsquo;s other fields.&lt;/p&gt;</description><content:encoded><![CDATA[<p><em>Learning Notes are short posts about the bit I got stuck on, the example I tried, and what made it click.</em></p>
<p>I&rsquo;m learning more about garbage collection, starting with Go&rsquo;s Green Tea collector. I got stuck on
prefetching: how does the CPU know what to fetch if it hasn&rsquo;t read the data yet?</p>
<p>Say we have a struct <code>B</code> with a pointer <code>B.Next</code> to another struct <code>C</code>. Reading <code>B.Next</code> gives us C&rsquo;s address,
not C&rsquo;s contents. So we can ask the CPU to start fetching C while it scans B&rsquo;s other fields.</p>
<style>
.prefetch-replay{scroll-margin-top:6rem}
.prefetch-timeline{display:block;width:100%;height:auto;margin:.5rem 0 1rem;font:600 22px var(--lab-mono);fill:var(--lab-text)}
.prefetch-timeline .run-label{font-size:23px;font-weight:800}
.prefetch-timeline .tick-rule{stroke:var(--lab-rule);stroke-dasharray:3 7}
.prefetch-timeline .tick-focus{fill:transparent;stroke:transparent;stroke-width:3;transition:fill .18s ease,stroke .18s ease}
.prefetch-timeline .active-tick .tick-focus{fill:var(--code-bg);stroke:var(--lab-accent)}
.prefetch-timeline .active-tick .tick-label{fill:var(--lab-accent);font-weight:800}
.prefetch-timeline .work rect{fill:var(--lab-accent)}
.prefetch-timeline .work text{fill:var(--lab-bg)}
.prefetch-timeline .fetch rect{fill:var(--lab-card);stroke:var(--lab-accent);stroke-width:2}
.prefetch-timeline .wait rect{fill:var(--lab-card);stroke:var(--lab-muted);stroke-width:2;stroke-dasharray:6 5}
.prefetch-timeline .idle{fill:var(--lab-muted)}
.prefetch-timeline .idle rect{fill:none}
.prefetch-replay .step-buttons{display:flex;flex-wrap:wrap;gap:.5rem}
.prefetch-replay button{border:1px solid var(--lab-rule);border-radius:.3rem;padding:.35rem .65rem;color:var(--lab-text)}
.prefetch-replay button[aria-disabled=true]{opacity:.5}
.prefetch-replay [hidden]{display:none}
@media(prefers-reduced-motion:reduce){.prefetch-timeline .tick-focus{transition:none}}
</style>
<figure class="prefetch-replay">
<h3>Watch the fetch overlap the scan</h3>
  <svg class="prefetch-timeline" viewBox="0 0 640 356" role="img" aria-labelledby="prefetch-title-0 prefetch-desc-0">
    <title id="prefetch-title-0">CPU and memory activity with and without prefetching</title>
    <desc id="prefetch-desc-0">Without prefetch, the CPU scans B for one tick, then waits for C for two ticks. With prefetch, the two-tick fetch starts during the scan, so the CPU can read C in the third tick.</desc>
    <g class="timeline-step" data-step="0"><rect class="tick-focus" x="117" y="4" width="166" height="334" rx="8"/><text class="tick-label" text-anchor="middle" x="200" y="31">Tick 1</text></g>
    <g class="timeline-step" data-step="1"><rect class="tick-focus" x="285" y="4" width="166" height="334" rx="8"/><text class="tick-label" text-anchor="middle" x="368" y="31">Tick 2</text></g>
    <g class="timeline-step" data-step="2"><rect class="tick-focus" x="453" y="4" width="166" height="334" rx="8"/><text class="tick-label" text-anchor="middle" x="536" y="31">Tick 3</text></g>
    <path class="tick-rule" d="M116 45V337"/>
    <path class="tick-rule" d="M284 45V337"/>
    <path class="tick-rule" d="M452 45V337"/>
    <path class="tick-rule" d="M620 45V337"/>
    <text class="run-label" x="7" y="73">Without prefetch</text><text x="7" y="113">CPU</text><text x="7" y="166">Memory</text>
    <g class="work"><rect x="121" y="85" width="158" height="42" rx="5"/><text text-anchor="middle" x="200.0" y="113">Scan B</text></g>
    <g class="wait"><rect x="289" y="85" width="326" height="42" rx="5"/><text text-anchor="middle" x="452.0" y="113">Wait</text></g>
    <g class="fetch"><rect x="289" y="138" width="326" height="42" rx="5"/><text text-anchor="middle" x="452.0" y="166">Fetch C</text></g>
    <g class="idle"><rect x="121" y="138" width="158" height="42" rx="5"/><text text-anchor="middle" x="200.0" y="166">Idle</text></g>
    <text class="run-label" x="7" y="219">With prefetch</text><text x="7" y="259">CPU</text><text x="7" y="312">Memory</text>
    <g class="work"><rect x="121" y="231" width="158" height="42" rx="5"/><text text-anchor="middle" x="200.0" y="259">Scan B</text></g>
    <g class="wait"><rect x="289" y="231" width="158" height="42" rx="5"/><text text-anchor="middle" x="368.0" y="259">Wait</text></g>
    <g class="fetch"><rect x="121" y="284" width="326" height="42" rx="5"/><text text-anchor="middle" x="284.0" y="312">Fetch C</text></g>
    <g class="idle"><rect x="457" y="284" width="158" height="42" rx="5"/><text text-anchor="middle" x="536.0" y="312">Idle</text></g>
    <g class="work"><rect x="457" y="231" width="158" height="42" rx="5"/><text text-anchor="middle" x="536.0" y="259">Read C</text></g>
  </svg>
<figcaption>
  <p>Both runs start just after reading C’s address from B.</p>
<p class="step-caption" aria-live="polite" hidden></p>
<ol class="step-transcript">
<li>Both runs scan B. With prefetch, memory starts fetching C during that same tick.</li>
<li>Without prefetch, C’s fetch is just starting. With prefetch, it’s already halfway done.</li>
<li>The prefetch run can read C while the other run is still waiting.</li>
</ol>
<div class="step-buttons" hidden><button type="button">← Previous</button><button type="button">Next →</button><button type="button">Restart</button></div>
</figcaption>
</figure>

<p>The bit that made it click for me: the fetch still takes two ticks. We just spent one of those ticks doing work
on B instead of waiting for C.</p>
<p>If we ask too late, we still have to wait. If we ask too early, C might get kicked out of the cache before we
read it. What we want is enough time to do other work while C is on its way.</p>
<p>That&rsquo;s it!</p>
]]></content:encoded>
    </item>
    
  </channel>
</rss>
