Files
Angular-Docs-Cn/packages/compiler-cli/ngcc/src/execution/cluster/worker.ts
T
George Kalpakas e36e6c85ef perf(ngcc): process tasks in parallel in async mode (#32427)
`ngcc` supports both synchronous and asynchronous execution. The default
mode when using `ngcc` programmatically (which is how `@angular/cli` is
using it) is synchronous. When running `ngcc` from the command line
(i.e. via the `ivy-ngcc` script), it runs in async mode.

Previously, the work would be executed in the same way in both modes.

This commit improves the performance of `ngcc` in async mode by
processing tasks in parallel on multiple processes. It uses the Node.js
built-in [`cluster` module](https://nodejs.org/api/cluster.html) to
launch a cluster of Node.js processes and take advantage of multi-core
systems.

Preliminary comparisons indicate a 1.8x to 2.6x speed improvement when
processing the angular.io app (apparently depending on the OS, number of
available cores, system load, etc.). Further investigation is needed to
better understand these numbers and identify potential areas of
improvement.

Inspired by/Based on @alxhub's prototype: alxhub/angular@cb631bdb1
Original design doc: https://hackmd.io/uYG9CJrFQZ-6FtKqpnYJAA?view

Jira issue: [FW-1460](https://angular-team.atlassian.net/browse/FW-1460)

PR Close #32427
2019-09-09 15:55:13 -04:00

58 lines
1.6 KiB
TypeScript

/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
/// <reference types="node" />
import * as cluster from 'cluster';
import {CompileFn, CreateCompileFn} from '../api';
import {MessageToWorker} from './api';
import {sendMessageToMaster} from './utils';
/**
* A cluster worker is responsible for processing one task (i.e. one format property for a specific
* entry-point) at a time and reporting results back to the cluster master.
*/
export class ClusterWorker {
private compile: CompileFn;
constructor(createCompileFn: CreateCompileFn) {
if (cluster.isMaster) {
throw new Error('Tried to instantiate `ClusterWorker` on the master process.');
}
this.compile =
createCompileFn((_task, outcome) => sendMessageToMaster({type: 'task-completed', outcome}));
}
run(): Promise<void> {
// Listen for `ProcessTaskMessage`s and process tasks.
cluster.worker.on('message', (msg: MessageToWorker) => {
try {
switch (msg.type) {
case 'process-task':
return this.compile(msg.task);
default:
throw new Error(
`Invalid message received on worker #${cluster.worker.id}: ${JSON.stringify(msg)}`);
}
} catch (err) {
sendMessageToMaster({
type: 'error',
error: (err instanceof Error) ? (err.stack || err.message) : err,
});
}
});
// Return a promise that is never resolved.
return new Promise(() => undefined);
}
}