[READ-ONLY] Mirror of https://github.com/vitest-dev/vitest. Next generation testing framework powered by Vite. vitest.dev
test testing-tools vite
12

Configure Feed

Select the types of activity you want to include in your feed.

perf: avoid spawning extra workers if no tests will run there (#8446)

authored by

Vladimir and committed by
GitHub
(Aug 19, 2025, 8:48 AM +0300) 3fb3e803 dd6186e1

+38 -14
+6 -6
packages/vitest/src/node/pool.ts
··· 175 175 const groupedSpecifications: Record<string, TestSpecification[]> = {} 176 176 const groups = new Set<number>() 177 177 178 - const factories: Record<LocalPool, () => ProcessPool> = { 179 - vmThreads: () => createVmThreadsPool(ctx, options), 180 - vmForks: () => createVmForksPool(ctx, options), 181 - threads: () => createThreadsPool(ctx, options), 182 - forks: () => createForksPool(ctx, options), 178 + const factories: Record<LocalPool, (specs: TestSpecification[]) => ProcessPool> = { 179 + vmThreads: specs => createVmThreadsPool(ctx, options, specs), 180 + vmForks: specs => createVmForksPool(ctx, options, specs), 181 + threads: specs => createThreadsPool(ctx, options, specs), 182 + forks: specs => createForksPool(ctx, options, specs), 183 183 typescript: () => createTypecheckPool(ctx), 184 184 } 185 185 ··· 241 241 242 242 if (pool in factories) { 243 243 const factory = factories[pool] 244 - pools[pool] ??= factory() 244 + pools[pool] ??= factory(specs) 245 245 return pools[pool]![method](specs, invalidate) 246 246 } 247 247
+8 -2
packages/vitest/src/node/pools/forks.ts
··· 5 5 import type { Vitest } from '../core' 6 6 import type { PoolProcessOptions, ProcessPool, RunWithFiles } from '../pool' 7 7 import type { TestProject } from '../project' 8 + import type { TestSpecification } from '../spec' 8 9 import type { SerializedConfig } from '../types/config' 9 10 import EventEmitter from 'node:events' 10 11 import * as nodeos from 'node:os' ··· 65 66 export function createForksPool( 66 67 vitest: Vitest, 67 68 { execArgv, env }: PoolProcessOptions, 69 + specifications: TestSpecification[], 68 70 ): ProcessPool { 69 71 const numCpus 70 72 = typeof nodeos.availableParallelism === 'function' ··· 75 77 ? Math.max(Math.floor(numCpus / 2), 1) 76 78 : Math.max(numCpus - 1, 1) 77 79 80 + const recommendedCount = vitest.config.watch 81 + ? threadsCount 82 + : Math.min(threadsCount, specifications.length) 83 + 78 84 const poolOptions = vitest.config.poolOptions?.forks ?? {} 79 85 80 86 const maxThreads 81 - = poolOptions.maxForks ?? vitest.config.maxWorkers ?? threadsCount 87 + = poolOptions.maxForks ?? vitest.config.maxWorkers ?? recommendedCount 82 88 const minThreads 83 - = poolOptions.minForks ?? vitest.config.minWorkers ?? Math.min(threadsCount, maxThreads) 89 + = poolOptions.minForks ?? vitest.config.minWorkers ?? Math.min(recommendedCount, maxThreads) 84 90 85 91 const worker = resolve(vitest.distPath, 'workers/forks.js') 86 92
+8 -2
packages/vitest/src/node/pools/threads.ts
··· 5 5 import type { Vitest } from '../core' 6 6 import type { PoolProcessOptions, ProcessPool, RunWithFiles } from '../pool' 7 7 import type { TestProject } from '../project' 8 + import type { TestSpecification } from '../spec' 8 9 import type { SerializedConfig } from '../types/config' 9 10 import type { WorkerContext } from '../types/worker' 10 11 import * as nodeos from 'node:os' ··· 46 47 export function createThreadsPool( 47 48 vitest: Vitest, 48 49 { execArgv, env }: PoolProcessOptions, 50 + specifications: TestSpecification[], 49 51 ): ProcessPool { 50 52 const numCpus 51 53 = typeof nodeos.availableParallelism === 'function' ··· 56 58 ? Math.max(Math.floor(numCpus / 2), 1) 57 59 : Math.max(numCpus - 1, 1) 58 60 61 + const recommendedCount = vitest.config.watch 62 + ? threadsCount 63 + : Math.min(threadsCount, specifications.length) 64 + 59 65 const poolOptions = vitest.config.poolOptions?.threads ?? {} 60 66 61 67 const maxThreads 62 - = poolOptions.maxThreads ?? vitest.config.maxWorkers ?? threadsCount 68 + = poolOptions.maxThreads ?? vitest.config.maxWorkers ?? recommendedCount 63 69 const minThreads 64 - = poolOptions.minThreads ?? vitest.config.minWorkers ?? Math.min(threadsCount, maxThreads) 70 + = poolOptions.minThreads ?? vitest.config.minWorkers ?? Math.min(recommendedCount, maxThreads) 65 71 66 72 const worker = resolve(vitest.distPath, 'workers/threads.js') 67 73
+8 -2
packages/vitest/src/node/pools/vmForks.ts
··· 5 5 import type { Vitest } from '../core' 6 6 import type { PoolProcessOptions, ProcessPool, RunWithFiles } from '../pool' 7 7 import type { TestProject } from '../project' 8 + import type { TestSpecification } from '../spec' 8 9 import type { ResolvedConfig, SerializedConfig } from '../types/config' 9 10 import EventEmitter from 'node:events' 10 11 import * as nodeos from 'node:os' ··· 72 73 export function createVmForksPool( 73 74 vitest: Vitest, 74 75 { execArgv, env }: PoolProcessOptions, 76 + specifications: TestSpecification[], 75 77 ): ProcessPool { 76 78 const numCpus 77 79 = typeof nodeos.availableParallelism === 'function' ··· 82 84 ? Math.max(Math.floor(numCpus / 2), 1) 83 85 : Math.max(numCpus - 1, 1) 84 86 87 + const recommendedCount = vitest.config.watch 88 + ? threadsCount 89 + : Math.min(threadsCount, specifications.length) 90 + 85 91 const poolOptions = vitest.config.poolOptions?.vmForks ?? {} 86 92 87 93 const maxThreads 88 - = poolOptions.maxForks ?? vitest.config.maxWorkers ?? threadsCount 94 + = poolOptions.maxForks ?? vitest.config.maxWorkers ?? recommendedCount 89 95 const minThreads 90 - = poolOptions.maxForks ?? vitest.config.minWorkers ?? Math.min(threadsCount, maxThreads) 96 + = poolOptions.maxForks ?? vitest.config.minWorkers ?? Math.min(recommendedCount, maxThreads) 91 97 92 98 const worker = resolve(vitest.distPath, 'workers/vmForks.js') 93 99
+8 -2
packages/vitest/src/node/pools/vmThreads.ts
··· 5 5 import type { Vitest } from '../core' 6 6 import type { PoolProcessOptions, ProcessPool, RunWithFiles } from '../pool' 7 7 import type { TestProject } from '../project' 8 + import type { TestSpecification } from '../spec' 8 9 import type { ResolvedConfig, SerializedConfig } from '../types/config' 9 10 import type { WorkerContext } from '../types/worker' 10 11 import * as nodeos from 'node:os' ··· 49 50 export function createVmThreadsPool( 50 51 vitest: Vitest, 51 52 { execArgv, env }: PoolProcessOptions, 53 + specifications: TestSpecification[], 52 54 ): ProcessPool { 53 55 const numCpus 54 56 = typeof nodeos.availableParallelism === 'function' ··· 59 61 ? Math.max(Math.floor(numCpus / 2), 1) 60 62 : Math.max(numCpus - 1, 1) 61 63 64 + const recommendedCount = vitest.config.watch 65 + ? threadsCount 66 + : Math.min(threadsCount, specifications.length) 67 + 62 68 const poolOptions = vitest.config.poolOptions?.vmThreads ?? {} 63 69 64 70 const maxThreads 65 - = poolOptions.maxThreads ?? vitest.config.maxWorkers ?? threadsCount 71 + = poolOptions.maxThreads ?? vitest.config.maxWorkers ?? recommendedCount 66 72 const minThreads 67 - = poolOptions.minThreads ?? vitest.config.minWorkers ?? Math.min(threadsCount, maxThreads) 73 + = poolOptions.minThreads ?? vitest.config.minWorkers ?? Math.min(recommendedCount, maxThreads) 68 74 69 75 const worker = resolve(vitest.distPath, 'workers/vmThreads.js') 70 76