-
Notifications
You must be signed in to change notification settings - Fork 12k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(@angular/cli): enable Node.js compile code cache when available
The Angular CLI will now enable the Node.js compile cache when available for use. Node.js v22.8 and higher currently provide support for this feature. The compile cache stores the v8 intermediate forms of JavaScript code for the Angular CLI itself. This provides a speed up to initialization on subsequent uses the Angular CLI. The Node.js cache is stored in a temporary directory in a globally accessible location so that all Node.js instances of a compatible version can share the cache. The code cache can be disabled if preferred via `NODE_DISABLE_COMPILE_CACHE=1`. Based on initial profiling, this change provides an ~6% production build time improvement for a newly generated project once the cache is available. ``` Benchmark 1: NODE_DISABLE_COMPILE_CACHE=1 node ./node_modules/.bin/ng build Time (mean ± σ): 2.617 s ± 0.016 s [User: 3.795 s, System: 1.284 s] Range (min … max): 2.597 s … 2.640 s 10 runs Benchmark 2: node ./node_modules/.bin/ng build Time (mean ± σ): 2.475 s ± 0.017 s [User: 3.555 s, System: 1.354 s] Range (min … max): 2.454 s … 2.510 s 10 runs Summary node ./node_modules/.bin/ng build ran 1.06 ± 0.01 times faster than NODE_DISABLE_COMPILE_CACHE=1 node ./node_modules/.bin/ng build ```
- Loading branch information
Showing
8 changed files
with
81 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC 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.dev/license | ||
*/ | ||
|
||
import { getCompileCacheDir } from 'node:module'; | ||
import { Piscina } from 'piscina'; | ||
|
||
export type WorkerPoolOptions = ConstructorParameters<typeof Piscina>[0]; | ||
|
||
export class WorkerPool extends Piscina { | ||
constructor(options: WorkerPoolOptions) { | ||
const piscinaOptions: WorkerPoolOptions = { | ||
minThreads: 1, | ||
idleTimeout: 1000, | ||
// Web containers do not support transferable objects with receiveOnMessagePort which | ||
// is used when the Atomics based wait loop is enable. | ||
useAtomics: !process.versions.webcontainer, | ||
recordTiming: false, | ||
...options, | ||
}; | ||
|
||
// Enable compile code caching if enabled for the main process (only exists on Node.js v22.8+). | ||
// Skip if running inside Bazel via a RUNFILES environment variable check. The cache does not work | ||
// well with Bazel's hermeticity requirements. | ||
const compileCacheDirectory = process.env['RUNFILES'] ? undefined : getCompileCacheDir?.(); | ||
if (compileCacheDirectory) { | ||
if (typeof piscinaOptions.env === 'object') { | ||
piscinaOptions.env['NODE_COMPILE_CACHE'] = compileCacheDirectory; | ||
} else { | ||
// Default behavior of `env` option is to copy current process values | ||
piscinaOptions.env = { | ||
...process.env, | ||
'NODE_COMPILE_CACHE': compileCacheDirectory, | ||
}; | ||
} | ||
} | ||
|
||
super(piscinaOptions); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
f249e7e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
packages/angular/build/src/tools/angular/compilation/parallel-compilation.ts