Skip to content

Commit

Permalink
perf(ngcc): spawn workers lazily (#35719) (#35832)
Browse files Browse the repository at this point in the history
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

PR Close #35832
  • Loading branch information
alan-agius4 authored and atscott committed Mar 3, 2020
1 parent 12e52a7 commit 525dc6a
Showing 1 changed file with 15 additions and 8 deletions.
23 changes: 15 additions & 8 deletions packages/compiler-cli/ngcc/src/execution/cluster/master.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ export class ClusterMaster {
}

run(): Promise<void> {
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)));

Expand All @@ -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();
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 525dc6a

Please sign in to comment.