From dc40a93317e762540d8a5ca162c92ad5913db80e Mon Sep 17 00:00:00 2001 From: Alan Agius Date: Fri, 28 Feb 2020 07:54:16 +0100 Subject: [PATCH] perf(ngcc): spawn workers lazily (#35719) With this change we spawn workers lazily based on the amount of work that needs to be done. Before this change we spawned the maximum of workers possible. However, in some cases there are less tasks than the max number of workers which resulted in created unnecessary workers Reference: #35717 PR Close #35719 --- .../ngcc/src/execution/cluster/master.ts | 23 ++++++++++++------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/packages/compiler-cli/ngcc/src/execution/cluster/master.ts b/packages/compiler-cli/ngcc/src/execution/cluster/master.ts index 6a80bfd486..0fe1a714d5 100644 --- a/packages/compiler-cli/ngcc/src/execution/cluster/master.ts +++ b/packages/compiler-cli/ngcc/src/execution/cluster/master.ts @@ -41,6 +41,10 @@ export class ClusterMaster { } run(): Promise { + if (this.taskQueue.allTasksCompleted) { + return Promise.resolve(); + } + // Set up listeners for worker events (emitted on `cluster`). cluster.on('online', this.wrapEventHandler(worker => this.onWorkerOnline(worker.id))); @@ -51,10 +55,8 @@ export class ClusterMaster { 'exit', this.wrapEventHandler((worker, code, signal) => this.onWorkerExit(worker, code, signal))); - // Start the workers. - for (let i = 0; i < this.workerCount; i++) { - cluster.fork(); - } + // Since we have pending tasks at the very minimum we need a single worker. + cluster.fork(); return this.finishedDeferred.promise.then(() => this.stopWorkers(), err => { this.stopWorkers(); @@ -98,11 +100,16 @@ export class ClusterMaster { isWorkerAvailable = false; } - // If there are no available workers or no available tasks, log (for debugging purposes). if (!isWorkerAvailable) { - this.logger.debug( - `All ${this.taskAssignments.size} workers are currently busy and cannot take on more ` + - 'work.'); + if (this.taskAssignments.size < this.workerCount) { + this.logger.debug('Spawning another worker process as there is more work to be done.'); + cluster.fork(); + } else { + // If there are no available workers or no available tasks, log (for debugging purposes). + this.logger.debug( + `All ${this.taskAssignments.size} workers are currently busy and cannot take on more ` + + 'work.'); + } } else { const busyWorkers = Array.from(this.taskAssignments) .filter(([_workerId, task]) => task !== null)