From 931092e46e705c9f8d3e2170d89ef7abeea623f8 Mon Sep 17 00:00:00 2001 From: neverland Date: Fri, 7 Aug 2026 19:05:24 +0800 Subject: [PATCH] perf(fmt): avoid decoding cache hits --- packages/rstack/src/fmt/worker.ts | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/packages/rstack/src/fmt/worker.ts b/packages/rstack/src/fmt/worker.ts index 8b42b922..50c29fc2 100644 --- a/packages/rstack/src/fmt/worker.ts +++ b/packages/rstack/src/fmt/worker.ts @@ -24,20 +24,26 @@ const formatFile = async ({ cache, }: FormatFileTask): Promise => { let source: string | undefined; + let sourceBuffer: Buffer | undefined; let contentHash: string | undefined; const readSource = (shouldHash = !shouldWrite): string => { + if (sourceBuffer !== undefined) { + return sourceBuffer.toString('utf8'); + } + if (!cache || !shouldHash) { return readFileSync(file.path, 'utf8'); } - const content = readFileSync(file.path); - contentHash = hashContent(content); - return content.toString('utf8'); + sourceBuffer = readFileSync(file.path); + contentHash = hashContent(sourceBuffer); + return sourceBuffer.toString('utf8'); }; if (cache?.entry && cache.entry[1] === cache.optionsHash) { - source = readSource(true); + sourceBuffer = readFileSync(file.path); + contentHash = hashContent(sourceBuffer); const { entry } = cache; if (entry[0] === contentHash && (!shouldWrite || entry[2] === 'clean')) { return { status: entry[2] === 'clean' ? 'unchanged' : 'changed' };