Skip to content

Commit a01779f

Browse files
authored
perf(fmt): avoid decoding cache hits (#236)
1 parent 5e6cdc1 commit a01779f

1 file changed

Lines changed: 10 additions & 4 deletions

File tree

packages/rstack/src/fmt/worker.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,26 @@ const formatFile = async ({
2424
cache,
2525
}: FormatFileTask): Promise<FmtWorkerResult> => {
2626
let source: string | undefined;
27+
let sourceBuffer: Buffer | undefined;
2728
let contentHash: string | undefined;
2829

2930
const readSource = (shouldHash = !shouldWrite): string => {
31+
if (sourceBuffer !== undefined) {
32+
return sourceBuffer.toString('utf8');
33+
}
34+
3035
if (!cache || !shouldHash) {
3136
return readFileSync(file.path, 'utf8');
3237
}
3338

34-
const content = readFileSync(file.path);
35-
contentHash = hashContent(content);
36-
return content.toString('utf8');
39+
sourceBuffer = readFileSync(file.path);
40+
contentHash = hashContent(sourceBuffer);
41+
return sourceBuffer.toString('utf8');
3742
};
3843

3944
if (cache?.entry && cache.entry[1] === cache.optionsHash) {
40-
source = readSource(true);
45+
sourceBuffer = readFileSync(file.path);
46+
contentHash = hashContent(sourceBuffer);
4147
const { entry } = cache;
4248
if (entry[0] === contentHash && (!shouldWrite || entry[2] === 'clean')) {
4349
return { status: entry[2] === 'clean' ? 'unchanged' : 'changed' };

0 commit comments

Comments
 (0)