Skip to content

Commit 827fef8

Browse files
committed
perf(fmt): filter cached unsupported files before scheduling
1 parent a0f8cbc commit 827fef8

2 files changed

Lines changed: 18 additions & 17 deletions

File tree

‎packages/rstack/src/fmt/runner.ts‎

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,6 @@ const runFmtFile = async (
139139
shouldWrite: boolean,
140140
formatFile: FormatFile,
141141
): Promise<FmtFileRun> => {
142-
if (isCachedUnsupported(task)) {
143-
return { outcome: 'unsupported' };
144-
}
145-
146142
const { file, key, cache } = task;
147143
try {
148144
const result = await formatFile(file, shouldWrite, cache);
@@ -243,31 +239,32 @@ const runFmtTasks = async (
243239
maxWorkers?: number,
244240
cache?: RunCache,
245241
): Promise<FmtFilesResult> => {
246-
const tasks = files.map((file) => createRunTask(file, cache));
247-
const pendingFileCount = tasks.reduce(
248-
(count, task) => count + (isCachedUnsupported(task) ? 0 : 1),
249-
0,
250-
);
251-
if (pendingFileCount === 0) {
242+
const tasks: FmtFileRunTask[] = [];
243+
for (const file of files) {
244+
const task = createRunTask(file, cache);
245+
if (!isCachedUnsupported(task)) {
246+
tasks.push(task);
247+
}
248+
}
249+
if (tasks.length === 0) {
252250
return { files: [], processedFileCount: 0 };
253251
}
254252

255253
// One pending file cannot benefit from parallelism, so avoid worker startup and IPC overhead.
256-
if (pendingFileCount === 1) {
254+
if (tasks.length === 1) {
257255
const { formatFile } = await import('./worker.ts');
258256
const formatFileOnMainThread: FormatFile = (file, write, fileCache) =>
259257
formatFile({ file, shouldWrite: write, cache: fileCache });
260-
const results = await runScheduledTasks(
261-
tasks,
258+
const result = await runFmtFile(
259+
tasks[0],
262260
shouldWrite,
263261
formatFileOnMainThread,
264-
1,
265262
);
266-
return collectFmtResults(results, cache);
263+
return collectFmtResults([result], cache);
267264
}
268265

269266
const { createWorkerPool } = await import('./workerPool.ts');
270-
const workerPool = await createWorkerPool(pendingFileCount, maxWorkers);
267+
const workerPool = await createWorkerPool(tasks.length, maxWorkers);
271268

272269
try {
273270
const results = await runScheduledTasks(

‎packages/rstack/tests/fmt/runnerWorkerPreflight.test.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ test('does not start the worker pool when every parser result is cached as unsup
7070

7171
test('rechecks a path-only unsupported entry on the main thread', async () => {
7272
await withTempProject(async (rootPath) => {
73+
const { file: ignored } = await createCachedUnsupportedFile(
74+
rootPath,
75+
'ignored.unknown',
76+
);
7377
const { cache, file } = await createCachedUnsupportedFile(
7478
rootPath,
7579
'script',
@@ -78,7 +82,7 @@ test('rechecks a path-only unsupported entry on the main thread', async () => {
7882

7983
await expect(
8084
runFmtFiles({
81-
files: [file],
85+
files: [ignored, file],
8286
mode: 'check',
8387
cache,
8488
}),

0 commit comments

Comments
 (0)