perf(compiler-cli): cache results of `absoluteFromSourceFile` (#41475)

The compiler frequently translates TypeScript source file `fileName` strings
into absolute paths, via a `fs.resolve()` operation. This is often done via
the helper function `absoluteFromSourceFile`.

This commit adds a caching mechanism whereby the `AbsoluteFsPath` of a
source file is patched onto the object under an Angular-specific symbol
property, allowing the compiler to avoid resolving the path on subsequent
calls.

PR Close #41475
This commit is contained in:
Alex Rickabaugh 2021-04-06 08:54:35 -04:00 committed by Zach Arend
parent 71b8c9ab29
commit fab1a6468e
1 changed files with 13 additions and 3 deletions

View File

@ -29,11 +29,21 @@ export function absoluteFrom(path: string): AbsoluteFsPath {
return fs.resolve(path);
}
const ABSOLUTE_PATH = Symbol('AbsolutePath');
/**
* Extract an `AbsoluteFsPath` from a `ts.SourceFile`.
* Extract an `AbsoluteFsPath` from a `ts.SourceFile`-like object.
*/
export function absoluteFromSourceFile(sf: ts.SourceFile): AbsoluteFsPath {
return fs.resolve(sf.fileName);
export function absoluteFromSourceFile(sf: {fileName: string}): AbsoluteFsPath {
const sfWithPatch = sf as {fileName: string, [ABSOLUTE_PATH]?: AbsoluteFsPath};
if (sfWithPatch[ABSOLUTE_PATH] === undefined) {
sfWithPatch[ABSOLUTE_PATH] = fs.resolve(sfWithPatch.fileName);
}
// Non-null assertion needed since TS doesn't narrow the type of fields that use a symbol as a key
// apparently.
return sfWithPatch[ABSOLUTE_PATH]!;
}
/**