Skip to content

perf(@angular-devkit/build-angular): render Sass using a pool of workers #20771

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 17, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions packages/angular_devkit/build_angular/src/sass/sass-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@

import { Importer, ImporterReturnType, Options, Result, SassException } from 'sass';
import { MessageChannel, Worker } from 'worker_threads';
import { maxWorkers } from '../utils/environment-options';

/**
* The maximum number of Workers that will be created to execute render requests.
*/
const MAX_RENDER_WORKERS = maxWorkers;

/**
* The callback type for the `dart-sass` asynchronous render function.
Expand All @@ -19,6 +25,7 @@ type RenderCallback = (error?: SassException, result?: Result) => void;
*/
interface RenderRequest {
id: number;
workerIndex: number;
callback: RenderCallback;
importers?: Importer[];
}
Expand All @@ -39,9 +46,11 @@ interface RenderResponseMessage {
* the worker which can be up to two times faster than the asynchronous variant.
*/
export class SassWorkerImplementation {
private worker?: Worker;
private readonly workers: Worker[] = [];
private readonly availableWorkers: number[] = [];
private readonly requests = new Map<number, RenderRequest>();
private idCounter = 1;
private nextWorkerIndex = 0;

/**
* Provides information about the Sass implementation.
Expand Down Expand Up @@ -74,14 +83,23 @@ export class SassWorkerImplementation {
throw new Error('Sass custom functions are not supported.');
}

if (!this.worker) {
this.worker = this.createWorker();
let workerIndex = this.availableWorkers.pop();
if (workerIndex === undefined) {
if (this.workers.length < MAX_RENDER_WORKERS) {
workerIndex = this.workers.length;
this.workers.push(this.createWorker());
} else {
workerIndex = this.nextWorkerIndex++;
if (this.nextWorkerIndex >= this.workers.length) {
this.nextWorkerIndex = 0;
}
}
}

const request = this.createRequest(callback, importer);
const request = this.createRequest(workerIndex, callback, importer);
this.requests.set(request.id, request);

this.worker.postMessage({
this.workers[workerIndex].postMessage({
id: request.id,
hasImporter: !!importer,
options: serializableOptions,
Expand All @@ -96,7 +114,9 @@ export class SassWorkerImplementation {
* is only needed if early cleanup is needed.
*/
close(): void {
this.worker?.terminate();
for (const worker of this.workers) {
void worker.terminate();
}
this.requests.clear();
}

Expand All @@ -117,6 +137,7 @@ export class SassWorkerImplementation {
}

this.requests.delete(response.id);
this.availableWorkers.push(request.workerIndex);

if (response.result) {
// The results are expected to be Node.js `Buffer` objects but will each be transferred as
Expand Down Expand Up @@ -193,11 +214,13 @@ export class SassWorkerImplementation {
}

private createRequest(
workerIndex: number,
callback: RenderCallback,
importer: Importer | Importer[] | undefined,
): RenderRequest {
return {
id: this.idCounter++,
workerIndex,
callback,
importers: !importer || Array.isArray(importer) ? importer : [importer],
};
Expand Down