perf(state): cache buildStateFrontmatter disk scan per process (#1967)

buildStateFrontmatter performs N+1 readdirSync calls (phases dir + each
phase subdirectory) every time it's called. Multiple state writes within
a single gsd-tools invocation repeat the same scan unnecessarily.

Add a module-level Map cache keyed by cwd that stores the disk scan
results. The cache auto-clears when the process exits since each
gsd-tools CLI invocation is a short-lived process running one command.

Closes #1967

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Tibsfox
2026-04-10 11:27:39 -07:00
parent 6c2795598a
commit 5638448296

View File

@@ -7,6 +7,11 @@ const path = require('path');
const { escapeRegex, loadConfig, getMilestoneInfo, getMilestonePhaseFilter, normalizeMd, planningDir, planningPaths, output, error, atomicWriteFileSync } = require('./core.cjs'); const { escapeRegex, loadConfig, getMilestoneInfo, getMilestonePhaseFilter, normalizeMd, planningDir, planningPaths, output, error, atomicWriteFileSync } = require('./core.cjs');
const { extractFrontmatter, reconstructFrontmatter } = require('./frontmatter.cjs'); const { extractFrontmatter, reconstructFrontmatter } = require('./frontmatter.cjs');
// Cache disk scan results from buildStateFrontmatter per cwd per process (#1967).
// Avoids re-reading N+1 directories on every state write when the phase structure
// hasn't changed within the same gsd-tools invocation.
const _diskScanCache = new Map();
/** Shorthand — every state command needs this path */ /** Shorthand — every state command needs this path */
function getStatePath(cwd) { function getStatePath(cwd) {
return planningPaths(cwd).state; return planningPaths(cwd).state;
@@ -737,6 +742,10 @@ function buildStateFrontmatter(bodyContent, cwd) {
try { try {
const phasesDir = planningPaths(cwd).phases; const phasesDir = planningPaths(cwd).phases;
if (fs.existsSync(phasesDir)) { if (fs.existsSync(phasesDir)) {
// Use cached disk scan when available — avoids N+1 readdirSync calls
// on repeated buildStateFrontmatter invocations within the same process (#1967)
let cached = _diskScanCache.get(cwd);
if (!cached) {
const isDirInMilestone = getMilestonePhaseFilter(cwd); const isDirInMilestone = getMilestonePhaseFilter(cwd);
const phaseDirs = fs.readdirSync(phasesDir, { withFileTypes: true }) const phaseDirs = fs.readdirSync(phasesDir, { withFileTypes: true })
.filter(e => e.isDirectory()).map(e => e.name) .filter(e => e.isDirectory()).map(e => e.name)
@@ -753,12 +762,20 @@ function buildStateFrontmatter(bodyContent, cwd) {
diskTotalSummaries += summaries; diskTotalSummaries += summaries;
if (plans > 0 && summaries >= plans) diskCompletedPhases++; if (plans > 0 && summaries >= plans) diskCompletedPhases++;
} }
totalPhases = isDirInMilestone.phaseCount > 0 cached = {
totalPhases: isDirInMilestone.phaseCount > 0
? Math.max(phaseDirs.length, isDirInMilestone.phaseCount) ? Math.max(phaseDirs.length, isDirInMilestone.phaseCount)
: phaseDirs.length; : phaseDirs.length,
completedPhases = diskCompletedPhases; completedPhases: diskCompletedPhases,
totalPlans = diskTotalPlans; totalPlans: diskTotalPlans,
completedPlans = diskTotalSummaries; completedPlans: diskTotalSummaries,
};
_diskScanCache.set(cwd, cached);
}
totalPhases = cached.totalPhases;
completedPhases = cached.completedPhases;
totalPlans = cached.totalPlans;
completedPlans = cached.completedPlans;
} }
} catch { /* intentionally empty */ } } catch { /* intentionally empty */ }
} }