From fef4c73e866eacb2ece7a18d4df22d8e6c9f589c Mon Sep 17 00:00:00 2001 From: Ralksta <16941050+ralksta@users.noreply.github.com> Date: Tue, 21 Apr 2026 22:51:22 +0200 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Cache=20parsed=20YAML=20to?= =?UTF-8?q?=20prevent=20repetitive=20synchronous=20file=20I/O=20(#91)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Implement an in-memory `Map` to cache parsed YAML objects. - Use `structuredClone` to prevent caller mutations from corrupting the cached object state. - Implement negative caching for missing files. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> --- lib/config/parser.ts | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/config/parser.ts b/lib/config/parser.ts index 2820fe6..9b22de7 100644 --- a/lib/config/parser.ts +++ b/lib/config/parser.ts @@ -2,15 +2,39 @@ import fs from 'fs'; import path from 'path'; import yaml from 'js-yaml'; +// ⚡ Bolt Optimization: Cache parsed YAML to prevent repetitive synchronous +// file I/O and expensive parsing during production runtime. +const yamlCache = new Map(); + export function loadYaml(filename: string): T | null { + const isProd = process.env.NODE_ENV === 'production'; + + if (isProd && yamlCache.has(filename)) { + const cached = yamlCache.get(filename); + // ⚡ Bolt Optimization: Use `structuredClone` to prevent caller mutations + // from corrupting the cached object state, while preserving JS native types. + return cached === null ? null : structuredClone(cached as T); + } + const yamlPath = path.join(process.cwd(), 'content', filename); if (!fs.existsSync(yamlPath)) { + // ⚡ Bolt Optimization: Implement negative caching for missing files. + if (isProd) { + yamlCache.set(filename, null); + } return null; } const raw = fs.readFileSync(yamlPath, 'utf8'); - return (yaml.load(raw) || {}) as T; + const parsed = (yaml.load(raw) || {}) as T; + + if (isProd) { + yamlCache.set(filename, parsed); + return structuredClone(parsed); + } + + return parsed; } const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;