<?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>Performance on Deux</title><link>https://deux.top/en/tags/performance/</link><description>Recent content in Performance on Deux</description><image><title>Deux</title><url>https://deux.top/branding/og-default.jpg</url><link>https://deux.top/branding/og-default.jpg</link></image><generator>Hugo</generator><language>en-US</language><copyright>2026 二重奏</copyright><lastBuildDate>Thu, 05 Nov 2026 11:34:56 +0000</lastBuildDate><atom:link href="https://deux.top/en/tags/performance/index.xml" rel="self" type="application/rss+xml"/><item><title>Reducing Build Time from 8s to 2s: A Low-Tech Optimization</title><link>https://deux.top/en/posts/zz-s06/</link><pubDate>Thu, 05 Nov 2026 00:00:00 +0900</pubDate><guid>https://deux.top/en/posts/zz-s06/</guid><description>I investigated why my Hugo build time jumped from 2 to 8 seconds. It turned out to be a single inefficient category query inside a loop, which I fixed by moving the logic outside the loop.</description><content:encoded><![CDATA[<blockquote>
<p>This article was machine-translated by AI and may contain inaccuracies. See the <a href="/zh/posts/zz-s06/">Chinese original</a> for the authoritative version.</p>
</blockquote>
<h2 id="1-eight-seconds-isnt-slow-but-its-not-right">1. Eight seconds isn&rsquo;t slow, but it&rsquo;s not right</h2>
<p>With fewer than thirty articles on the site, <code>hugo --gc --minify</code> running locally takes just over eight seconds. The absolute number isn&rsquo;t terrifying, but ** the same repository was still at two seconds ** last week, and only two or three features have been added since then.</p>
<blockquote>
<p>The slowdown itself isn&rsquo;t scary; what&rsquo;s scary is not knowing which version it started from.</p>
</blockquote>
<p>First, I&rsquo;ll use the most brute-force method: <code>git log</code> check the last ten commits and perform a checkout and build timing for each one.</p>
<table>
	<thead>
			<tr>
					<th>Commit 交</th>
					<th>Build 建</th>
					<th>Suspect</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td>Added progress 条</td>
					<td>2.1s</td>
					<td>Irrelevant</td>
			</tr>
			<tr>
					<td>Added search auth 重</td>
					<td>2.2s</td>
					<td>Irrelevant</td>
			</tr>
			<tr>
					<td>Archive page local 化</td>
					<td><strong>7.9s</strong></td>
					<td>✓ This is it</td>
			</tr>
			<tr>
					<td>Following two 个</td>
					<td>8.0s</td>
					<td>Consistent</td>
			</tr>
	</tbody>
</table>
<p><em>The culprit was identified in a minute.</em> The real time-sink was what came after—why an archive page would cost an extra six seconds.</p>
<h2 id="2-a-few-useless-attempts">2. A few useless attempts</h2>
<p>Following intuition, I tried the following in order:</p>
<ol>
<li>** Thought it was images **: Cleared <code>resources</code> and re-ran, no change.</li>
<li>** Thought it was <code>--gc</code>**: Removed parameters, 8.0 → 7.9, not it.</li>
<li>** thought it was template nesting **: simplifying the archive template to just one <code>&lt;ul&gt;</code>, it&rsquo;s still 7.8</li>
</ol>
<ul>
<li><input checked="" disabled="" type="checkbox"> Clear cache</li>
<li><input checked="" disabled="" type="checkbox"> Run &ndash;gc</li>
<li><input checked="" disabled="" type="checkbox"> Simplify template</li>
<li><input disabled="" type="checkbox"> ~~ Preparing to switch generators ~~ (thankfully didn&rsquo;t pull the trigger)</li>
</ul>
<p>After finishing the third attempt, it occurred to me: ** is that category query line in the template running every single time in the loop **.</p>
<h2 id="iii-that-line-of-code">III. That line of code</h2>
<p>The archive page needs to be grouped by year and month; the original implementation was roughly like this:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-go" data-lang="go"><span class="line"><span class="cl"><span class="p">{{</span><span class="w"> </span><span class="k">range</span><span class="w"> </span><span class="p">.</span><span class="nx">Site</span><span class="p">.</span><span class="nx">RegularPages</span><span class="w"> </span><span class="p">}}</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span><span class="p">{{</span><span class="w"> </span><span class="err">$</span><span class="nx">y</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="p">.</span><span class="nx">Date</span><span class="p">.</span><span class="nx">Format</span><span class="w"> </span><span class="s">&#34;2006&#34;</span><span class="w"> </span><span class="p">}}</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span><span class="p">{{</span><span class="w"> </span><span class="err">$</span><span class="nx">posts</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nf">where</span><span class="w"> </span><span class="p">(</span><span class="nx">where</span><span class="w"> </span><span class="p">.</span><span class="nx">Site</span><span class="p">.</span><span class="nx">RegularPages</span><span class="w"> </span><span class="s">&#34;Section&#34;</span><span class="w"> </span><span class="s">&#34;posts&#34;</span><span class="p">)</span><span class="w"> </span><span class="s">&#34;Date&#34;</span><span class="w"> </span><span class="s">&#34;&gt;&#34;</span><span class="w"> </span><span class="o">...</span><span class="w"> </span><span class="p">}}</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="w">  </span><span class="p">{{</span><span class="w"> </span><span class="k">range</span><span class="w"> </span><span class="err">$</span><span class="nx">posts</span><span class="w"> </span><span class="p">}}</span><span class="err">…</span><span class="p">{{</span><span class="w"> </span><span class="nx">end</span><span class="w"> </span><span class="p">}}</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="p">{{</span><span class="w"> </span><span class="nx">end</span><span class="w"> </span><span class="p">}}</span><span class="w">
</span></span></span></code></pre></div><p>The outer <code>range</code> has N posts, so the inner <code>where</code> ends up ** filtering all site pages N times **. It wasn&rsquo;t noticeable when there were few articles, but once you add multi-language support which triples the page count, the complexity becomes immediately apparent.
This fix isn&rsquo;t clever at all: moving the ** filter outside the loop to perform ** only once.</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-go" data-lang="go"><span class="line"><span class="cl"><span class="p">{{</span><span class="w"> </span><span class="err">$</span><span class="nx">all</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="nx">where</span><span class="w"> </span><span class="p">.</span><span class="nx">Site</span><span class="p">.</span><span class="nx">RegularPages</span><span class="w"> </span><span class="s">&#34;Section&#34;</span><span class="w"> </span><span class="s">&#34;posts&#34;</span><span class="w"> </span><span class="p">}}</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="p">{{</span><span class="w"> </span><span class="err">$</span><span class="nx">groups</span><span class="w"> </span><span class="o">:=</span><span class="w"> </span><span class="err">$</span><span class="nx">all</span><span class="p">.</span><span class="nx">GroupByDate</span><span class="w"> </span><span class="s">&#34;2006-01&#34;</span><span class="w"> </span><span class="p">}}</span><span class="w">
</span></span></span><span class="line"><span class="cl"><span class="p">{{</span><span class="w"> </span><span class="k">range</span><span class="w"> </span><span class="err">$</span><span class="nx">groups</span><span class="w"> </span><span class="p">}}</span><span class="err">…</span><span class="p">{{</span><span class="w"> </span><span class="nx">end</span><span class="w"> </span><span class="p">}}</span><span class="w">
</span></span></span></code></pre></div><p><code>GroupByDate</code> is built into Hugo; I can&rsquo;t believe I didn&rsquo;t think to use it before.</p>
<h2 id="iv-numbers">IV. Numbers</h2>
<table>
	<thead>
			<tr>
					<th>Metric 标</th>
					<th>Before 前</th>
					<th>After</th>
			</tr>
	</thead>
	<tbody>
			<tr>
					<td>Local build 建</td>
					<td>8.0s</td>
					<td><strong>2.1s</strong></td>
			</tr>
			<tr>
					<td>Template exec 行</td>
					<td>6.7s</td>
					<td>0.8s</td>
			</tr>
			<tr>
					<td>Total CI time 长</td>
					<td>41s</td>
					<td>33s</td>
			</tr>
			<tr>
					<td>Lines of code 数</td>
					<td>22</td>
					<td>9</td>
			</tr>
	</tbody>
</table>
<blockquote>
<p>Deleted thirteen lines, saved six seconds. ** This is probably the most cost-effective commit of the week. **</p>
</blockquote>
<h2 id="v-why-its-worth-noting">V. Why it&rsquo;s worth noting</h2>
<p>Because the first three attempts were all wrong, even though they all &ldquo;felt&rdquo; right. Clearing the cache, removing parameters, simplifying templates—each step made sense, each step was tested, and each step was useless.</p>
<p>What actually worked was ** measuring before guessing **: by listing the build time for each commit in a table, the slow version revealed itself. If I had just relied on intuition to optimize images, I&rsquo;d probably still be converting images that haven&rsquo;t even changed into WebP.</p>
<ul>
<li>** Locate first, then act **</li>
<li>Don&rsquo;t optimize what you haven&rsquo;t measured</li>
<li>Check built-in framework functions first before writing your own loops</li>
<li>~~Eight seconds is bearable ~~ It is not bearable</li>
</ul>
<h2 id="vi-wrapping-up">VI. Wrapping Up</h2>
<p>Now <code>Total in 2.1s</code>, it is just like it was before adding the features. The archive page is still the same archive page—year/month grouping, trilingual support, and RSS entry are all there; it just no longer counts the same page list thirty times.</p>
<p>**Less code, a faster site, and no new dependencies added. ** Days like this are worth writing down.</p>
]]></content:encoded></item></channel></rss>