Skip to content

Commit dc40a93

Browse files
alan-agius4atscott
authored andcommitted
perf(ngcc): spawn workers lazily (angular#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: angular#35717 PR Close angular#35719
1 parent 0a5a841 commit dc40a93

File tree

1 file changed

+15
-8
lines changed
  • packages/compiler-cli/ngcc/src/execution/cluster

1 file changed

+15
-8
lines changed

packages/compiler-cli/ngcc/src/execution/cluster/master.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ export class ClusterMaster {
4141
}
4242

4343
run(): Promise<void> {
44+
if (this.taskQueue.allTasksCompleted) {
45+
return Promise.resolve();
46+
}
47+
4448
// Set up listeners for worker events (emitted on `cluster`).
4549
cluster.on('online', this.wrapEventHandler(worker => this.onWorkerOnline(worker.id)));
4650

@@ -51,10 +55,8 @@ export class ClusterMaster {
5155
'exit',
5256
this.wrapEventHandler((worker, code, signal) => this.onWorkerExit(worker, code, signal)));
5357

54-
// Start the workers.
55-
for (let i = 0; i < this.workerCount; i++) {
56-
cluster.fork();
57-
}
58+
// Since we have pending tasks at the very minimum we need a single worker.
59+
cluster.fork();
5860

5961
return this.finishedDeferred.promise.then(() => this.stopWorkers(), err => {
6062
this.stopWorkers();
@@ -98,11 +100,16 @@ export class ClusterMaster {
98100
isWorkerAvailable = false;
99101
}
100102

101-
// If there are no available workers or no available tasks, log (for debugging purposes).
102103
if (!isWorkerAvailable) {
103-
this.logger.debug(
104-
`All ${this.taskAssignments.size} workers are currently busy and cannot take on more ` +
105-
'work.');
104+
if (this.taskAssignments.size < this.workerCount) {
105+
this.logger.debug('Spawning another worker process as there is more work to be done.');
106+
cluster.fork();
107+
} else {
108+
// If there are no available workers or no available tasks, log (for debugging purposes).
109+
this.logger.debug(
110+
`All ${this.taskAssignments.size} workers are currently busy and cannot take on more ` +
111+
'work.');
112+
}
106113
} else {
107114
const busyWorkers = Array.from(this.taskAssignments)
108115
.filter(([_workerId, task]) => task !== null)

0 commit comments

Comments
 (0)