From 129e0720af3aece7c319687ccc82e33db04b022d Mon Sep 17 00:00:00 2001 From: Ricardo Date: Tue, 10 Mar 2026 14:44:51 +0100 Subject: [PATCH] perf: batch unfurl pre-fetch to reduce peak memory Replace unbounded Promise.all on 545 interaction URLs with batches of 50. Add GC calls after the markdown walk and between batches to free parsed content and resolved promise data before the render phase. Logs RSS every 5th batch for memory monitoring. Same pattern as the OG image batch spawning fix. Confab-Link: http://localhost:8080/sessions/0b241cd6-aff2-4fec-853c-2b5a61e61946 --- eleventy.config.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/eleventy.config.js b/eleventy.config.js index ff134e0..7e3b068 100644 --- a/eleventy.config.js +++ b/eleventy.config.js @@ -1205,10 +1205,26 @@ export default function (eleventyConfig) { } }; walk(contentDir); + // Free parsed markdown content before starting network-heavy prefetch + if (typeof global.gc === "function") global.gc(); if (urls.size === 0) return; - console.log(`[unfurl] Pre-fetching ${urls.size} interaction URLs...`); - await Promise.all([...urls].map((url) => prefetchUrl(url))); + const urlArray = [...urls]; + const UNFURL_BATCH = 50; + const totalBatches = Math.ceil(urlArray.length / UNFURL_BATCH); + console.log(`[unfurl] Pre-fetching ${urlArray.length} interaction URLs (${totalBatches} batches of ${UNFURL_BATCH})...`); + let fetched = 0; + for (let i = 0; i < urlArray.length; i += UNFURL_BATCH) { + const batch = urlArray.slice(i, i + UNFURL_BATCH); + const batchNum = Math.floor(i / UNFURL_BATCH) + 1; + await Promise.all(batch.map((url) => prefetchUrl(url))); + fetched += batch.length; + if (typeof global.gc === "function") global.gc(); + if (batchNum === 1 || batchNum % 5 === 0 || batchNum === totalBatches) { + const rss = (process.memoryUsage.rss() / 1024 / 1024).toFixed(0); + console.log(`[unfurl] Batch ${batchNum}/${totalBatches} (${fetched}/${urlArray.length}) | RSS: ${rss} MB`); + } + } console.log(`[unfurl] Pre-fetch complete.`); });