Skip to content

Commit ed30336

Browse files
committed
Revert "perf(@angular/build): avoid web worker channel overhead in library parallel compilation"
This reverts commit bcdcc09.
1 parent 85e4a03 commit ed30336

3 files changed

Lines changed: 23 additions & 93 deletions

File tree

‎packages/angular/build/src/tools/angular/compilation/parallel-compilation.ts‎

Lines changed: 18 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -66,28 +66,23 @@ export class ParallelCompilation extends AngularCompilation {
6666
);
6767

6868
this.#webWorkerChannel?.port1.close();
69-
this.#webWorkerChannel = undefined;
7069

71-
let webWorkerSignal: Int32Array | undefined;
72-
if (buildType !== 'library') {
73-
// The web worker processing is a synchronous operation and uses shared memory combined with
74-
// the Atomics API to block execution here until a response is received.
75-
const webWorkerChannel = new MessageChannel();
76-
this.#webWorkerChannel = webWorkerChannel;
77-
const signal = new Int32Array(new SharedArrayBuffer(4));
78-
webWorkerSignal = signal;
79-
webWorkerChannel.port1.on('message', ({ workerFile, containingFile }) => {
80-
try {
81-
const workerCodeFile = hostOptions.processWebWorker(workerFile, containingFile);
82-
webWorkerChannel.port1.postMessage({ workerCodeFile });
83-
} catch (error) {
84-
webWorkerChannel.port1.postMessage({ error });
85-
} finally {
86-
Atomics.store(signal, 0, 1);
87-
Atomics.notify(signal, 0);
88-
}
89-
});
90-
}
70+
// The web worker processing is a synchronous operation and uses shared memory combined with
71+
// the Atomics API to block execution here until a response is received.
72+
const webWorkerChannel = new MessageChannel();
73+
this.#webWorkerChannel = webWorkerChannel;
74+
const webWorkerSignal = new Int32Array(new SharedArrayBuffer(4));
75+
webWorkerChannel.port1.on('message', ({ workerFile, containingFile }) => {
76+
try {
77+
const workerCodeFile = hostOptions.processWebWorker(workerFile, containingFile);
78+
webWorkerChannel.port1.postMessage({ workerCodeFile });
79+
} catch (error) {
80+
webWorkerChannel.port1.postMessage({ error });
81+
} finally {
82+
Atomics.store(webWorkerSignal, 0, 1);
83+
Atomics.notify(webWorkerSignal, 0);
84+
}
85+
});
9186

9287
let success = false;
9388
try {
@@ -100,14 +95,12 @@ export class ParallelCompilation extends AngularCompilation {
10095
browserOnlyBuild: this.browserOnlyBuild,
10196
compilerOptionOverrides,
10297
stylesheetPort: stylesheetChannel.port2,
103-
webWorkerPort: this.#webWorkerChannel?.port2,
98+
webWorkerPort: webWorkerChannel.port2,
10499
webWorkerSignal,
105100
},
106101
{
107102
name: 'initialize',
108-
transferList: this.#webWorkerChannel
109-
? [stylesheetChannel.port2, this.#webWorkerChannel.port2]
110-
: [stylesheetChannel.port2],
103+
transferList: [stylesheetChannel.port2, webWorkerChannel.port2],
111104
},
112105
);
113106
success = true;

‎packages/angular/build/src/tools/angular/compilation/parallel-compilation_spec.ts‎

Lines changed: 0 additions & 59 deletions
This file was deleted.

‎packages/angular/build/src/tools/angular/compilation/parallel-worker.ts‎

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import type { PartialMessage } from 'esbuild';
1010
import assert from 'node:assert';
11+
import { randomUUID } from 'node:crypto';
1112
import { type MessagePort, receiveMessageOnPort } from 'node:worker_threads';
1213
import { initializeHash } from '../../../utils/hash';
1314
import { getAndClearCumulativeDurations } from '../../../utils/profiling';
@@ -27,8 +28,8 @@ export interface InitRequest {
2728
fileReplacements?: Record<string, string>;
2829
compilerOptionOverrides?: CompilerOptionOverrides;
2930
stylesheetPort: MessagePort;
30-
webWorkerPort?: MessagePort;
31-
webWorkerSignal?: Int32Array;
31+
webWorkerPort: MessagePort;
32+
webWorkerSignal: Int32Array;
3233
}
3334

3435
let compilation: AngularCompilation | undefined;
@@ -50,9 +51,8 @@ export async function initialize(request: InitRequest): Promise<AngularCompilati
5051
? new JitCompilation(request.browserOnlyBuild)
5152
: new AotCompilation(request.browserOnlyBuild);
5253

53-
let nextRequestId = 0;
5454
const stylesheetRequests = new Map<
55-
number,
55+
string,
5656
[(value: string) => void, (reason: Error) => void]
5757
>();
5858
request.stylesheetPort.on('message', ({ requestId, value, error }) => {
@@ -80,7 +80,7 @@ export async function initialize(request: InitRequest): Promise<AngularCompilati
8080
fileReplacements: request.fileReplacements,
8181
modifiedFiles: currentModifiedFiles,
8282
transformStylesheet(data, containingFile, stylesheetFile, order, className) {
83-
const requestId = ++nextRequestId;
83+
const requestId = randomUUID();
8484
const resultPromise = new Promise<string>((resolve, reject) =>
8585
stylesheetRequests.set(requestId, [resolve, reject]),
8686
);
@@ -97,10 +97,6 @@ export async function initialize(request: InitRequest): Promise<AngularCompilati
9797
return resultPromise;
9898
},
9999
processWebWorker(workerFile, containingFile) {
100-
if (!request.webWorkerPort || !request.webWorkerSignal) {
101-
return workerFile;
102-
}
103-
104100
Atomics.store(request.webWorkerSignal, 0, 0);
105101
request.webWorkerPort.postMessage({ workerFile, containingFile });
106102

0 commit comments

Comments
 (0)