Skip to content

Commit dd2a694

Browse files
clydinvikerman
authored andcommitted
refactor(@angular-devkit/build-angular): improve performance of parallel bundle processing
1 parent 5393041 commit dd2a694

File tree

5 files changed

+78
-43
lines changed

5 files changed

+78
-43
lines changed

packages/angular_devkit/build_angular/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"find-cache-dir": "3.0.0",
2828
"glob": "7.1.4",
2929
"istanbul-instrumenter-loader": "3.0.1",
30+
"jest-worker": "24.9.0",
3031
"karma-source-map-support": "1.4.0",
3132
"less": "3.10.3",
3233
"less-loader": "5.0.0",
@@ -61,7 +62,6 @@
6162
"webpack-merge": "4.2.2",
6263
"webpack-sources": "1.4.3",
6364
"webpack-subresource-integrity": "1.3.3",
64-
"worker-farm": "1.7.0",
6565
"worker-plugin": "3.2.0"
6666
},
6767
"devDependencies": {
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* @license
3+
* Copyright Google Inc. All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
import JestWorker from 'jest-worker';
9+
import * as os from 'os';
10+
11+
export class ActionExecutor<Input extends { size: number }, Output> {
12+
private largeWorker: JestWorker;
13+
private smallWorker: JestWorker;
14+
15+
private smallThreshold = 32 * 1024;
16+
17+
constructor(actionFile: string, private readonly actionName: string) {
18+
// larger files are processed in a separate process to limit memory usage in the main process
19+
this.largeWorker = new JestWorker(actionFile, {
20+
exposedMethods: [actionName],
21+
});
22+
23+
// small files are processed in a limited number of threads to improve speed
24+
// The limited number also prevents a large increase in memory usage for an otherwise short operation
25+
this.smallWorker = new JestWorker(actionFile, {
26+
exposedMethods: [actionName],
27+
numWorkers: os.cpus().length < 2 ? 1 : 2,
28+
// Will automatically fallback to processes if not supported
29+
enableWorkerThreads: true,
30+
});
31+
}
32+
33+
execute(options: Input): Promise<Output> {
34+
if (options.size > this.smallThreshold) {
35+
return ((this.largeWorker as unknown) as Record<string, (options: Input) => Promise<Output>>)[
36+
this.actionName
37+
](options);
38+
} else {
39+
return ((this.smallWorker as unknown) as Record<string, (options: Input) => Promise<Output>>)[
40+
this.actionName
41+
](options);
42+
}
43+
}
44+
45+
executeAll(options: Input[]): Promise<Output[]> {
46+
return Promise.all(options.map(o => this.execute(o)));
47+
}
48+
49+
stop() {
50+
this.largeWorker.end();
51+
this.smallWorker.end();
52+
}
53+
}

packages/angular_devkit/build_angular/src/browser/index.ts

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import { Observable, from, of } from 'rxjs';
3131
import { bufferCount, catchError, concatMap, map, mergeScan, switchMap } from 'rxjs/operators';
3232
import { ScriptTarget } from 'typescript';
3333
import * as webpack from 'webpack';
34-
import * as workerFarm from 'worker-farm';
3534
import { NgBuildAnalyticsPlugin } from '../../plugins/webpack/analytics';
3635
import { WebpackConfigOptions } from '../angular-cli-files/models/build-options';
3736
import {
@@ -78,6 +77,7 @@ import {
7877
getIndexInputFile,
7978
getIndexOutputFile,
8079
} from '../utils/webpack-browser-config';
80+
import { ActionExecutor } from './action-executor';
8181
import { Schema as BrowserBuilderSchema } from './schema';
8282

8383
const cacache = require('cacache');
@@ -115,7 +115,7 @@ export async function buildBrowserWebpackConfigFromContext(
115115
options: BrowserBuilderSchema,
116116
context: BuilderContext,
117117
host: virtualFs.Host<fs.Stats> = new NodeJsSyncHost(),
118-
): Promise<{ config: webpack.Configuration[], projectRoot: string, projectSourceRoot?: string }> {
118+
): Promise<{ config: webpack.Configuration[]; projectRoot: string; projectSourceRoot?: string }> {
119119
return generateBrowserWebpackConfigFromContext(
120120
options,
121121
context,
@@ -587,35 +587,25 @@ export function buildWebpackBrowser(
587587
}
588588

589589
if (processActions.length > 0) {
590-
await new Promise<void>((resolve, reject) => {
591-
const workerFile = require.resolve('../utils/process-bundle');
592-
const workers = workerFarm(
593-
{
594-
maxRetries: 1,
595-
},
596-
path.extname(workerFile) !== '.ts'
597-
? workerFile
598-
: require.resolve('../utils/process-bundle-bootstrap'),
599-
['process'],
600-
);
601-
let completed = 0;
602-
const workCallback = (error: Error | null, result: ProcessBundleResult) => {
603-
if (error) {
604-
workerFarm.end(workers);
605-
reject(error);
606-
607-
return;
608-
}
609-
610-
processResults.push(result);
611-
if (++completed === processActions.length) {
612-
workerFarm.end(workers);
613-
resolve();
614-
}
615-
};
590+
const workerFile = require.resolve('../utils/process-bundle');
591+
const executor = new ActionExecutor<
592+
ProcessBundleOptions & { size: number },
593+
ProcessBundleResult
594+
>(
595+
path.extname(workerFile) !== '.ts'
596+
? workerFile
597+
: require.resolve('../utils/process-bundle-bootstrap'),
598+
'process',
599+
);
616600

617-
processActions.forEach(action => workers['process'](action, workCallback));
618-
});
601+
try {
602+
const results = await executor.executeAll(
603+
processActions.map(a => ({ ...a, size: a.code.length })),
604+
);
605+
results.forEach(result => processResults.push(result));
606+
} finally {
607+
executor.stop();
608+
}
619609
}
620610

621611
// Runtime must be processed after all other files
@@ -625,7 +615,7 @@ export function buildWebpackBrowser(
625615
runtimeData: processResults,
626616
};
627617
processResults.push(
628-
await import('../utils/process-bundle').then(m => m.processAsync(runtimeOptions)),
618+
await import('../utils/process-bundle').then(m => m.process(runtimeOptions)),
629619
);
630620
}
631621

packages/angular_devkit/build_angular/src/utils/process-bundle.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,7 @@ export const enum CacheKey {
5757
DownlevelMap = 3,
5858
}
5959

60-
export function process(
61-
options: ProcessBundleOptions,
62-
callback: (error: Error | null, result?: ProcessBundleResult) => void,
63-
): void {
64-
processAsync(options).then(result => callback(null, result), error => callback(error));
65-
}
66-
67-
export async function processAsync(options: ProcessBundleOptions): Promise<ProcessBundleResult> {
60+
export async function process(options: ProcessBundleOptions): Promise<ProcessBundleResult> {
6861
if (!options.cacheKeys) {
6962
options.cacheKeys = [];
7063
}

yarn.lock

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5860,7 +5860,7 @@ jasminewd2@^2.1.0:
58605860
resolved "https://registry.yarnpkg.com/jasminewd2/-/jasminewd2-2.2.0.tgz#e37cf0b17f199cce23bea71b2039395246b4ec4e"
58615861
integrity sha1-43zwsX8ZnM4jvqcbIDk5Uka07E4=
58625862

5863-
jest-worker@^24.9.0:
5863+
jest-worker@24.9.0, jest-worker@^24.9.0:
58645864
version "24.9.0"
58655865
resolved "https://registry.yarnpkg.com/jest-worker/-/jest-worker-24.9.0.tgz#5dbfdb5b2d322e98567898238a9697bcce67b3e5"
58665866
integrity sha512-51PE4haMSXcHohnSMdM42anbvZANYTqMrr52tVKPqqsPJMzoP6FYYDVqahX/HrAoKEKz3uUPzSvKs9A3qR4iVw==
@@ -8959,7 +8959,6 @@ sauce-connect-launcher@^1.2.4:
89598959

89608960
"sauce-connect-proxy@https://saucelabs.com/downloads/sc-4.5.4-linux.tar.gz":
89618961
version "0.0.0"
8962-
uid dc5efcd2be24ddb099a85b923d6e754754651fa8
89638962
resolved "https://saucelabs.com/downloads/sc-4.5.4-linux.tar.gz#dc5efcd2be24ddb099a85b923d6e754754651fa8"
89648963

89658964
saucelabs@^1.5.0:
@@ -10890,7 +10889,7 @@ wordwrap@~0.0.2:
1089010889
resolved "https://registry.yarnpkg.com/wordwrap/-/wordwrap-0.0.3.tgz#a3d5da6cd5c0bc0008d37234bbaf1bed63059107"
1089110890
integrity sha1-o9XabNXAvAAI03I0u68b7WMFkQc=
1089210891

10893-
worker-farm@1.7.0, worker-farm@^1.7.0:
10892+
worker-farm@^1.7.0:
1089410893
version "1.7.0"
1089510894
resolved "https://registry.yarnpkg.com/worker-farm/-/worker-farm-1.7.0.tgz#26a94c5391bbca926152002f69b84a4bf772e5a8"
1089610895
integrity sha512-rvw3QTZc8lAxyVrqcSGVm5yP/IJ2UcB3U0graE3LCFoZ0Yn2x4EoVSqJKdB/T5M+FLcRPjz4TDacRf3OCfNUzw==

0 commit comments

Comments
 (0)