Skip to content

Commit a792bf1

Browse files
clydinthePunderWoman
authored andcommitted
perf(compiler-cli): minimize filesystem calls when generating shims (#47682)
Previously when a file was being analyzed to determine if a shim should be generated, up to two calls to the host `fileExists` function per file per generator were made. In the default host, each `fileExists` call made two underlying file system calls. Following these calls, the file was then read via `getSourceFile`. However, `getSourceFile` will return `undefined` if the requested file does not exist. As a result, `getSourceFile` can be used directly to request both potential file names and leverage the return value to determine if the file does not exist. This avoids the need to call `fileExists` at all. PR Close #47682
1 parent ea16a98 commit a792bf1

File tree

2 files changed

+16
-22
lines changed

2 files changed

+16
-22
lines changed

packages/compiler-cli/src/ngtsc/shims/src/adapter.ts

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -168,31 +168,26 @@ export class ShimAdapter {
168168
// This _might_ be a shim, if an underlying base file exists. The base file might be .ts or
169169
// .tsx.
170170
let baseFileName = absoluteFrom(prefix + '.ts');
171-
if (!this.delegate.fileExists(baseFileName)) {
171+
// Retrieve the original file for which the shim will be generated.
172+
let inputFile = this.delegate.getSourceFile(baseFileName, ts.ScriptTarget.Latest);
173+
if (inputFile === undefined) {
172174
// No .ts file by that name - try .tsx.
173175
baseFileName = absoluteFrom(prefix + '.tsx');
174-
if (!this.delegate.fileExists(baseFileName)) {
175-
// This isn't a shim after all since there is no original file which would have triggered
176-
// its generation, even though the path is right. There are a few reasons why this could
177-
// occur:
178-
//
179-
// * when resolving an import to an .ngfactory.d.ts file, the module resolution algorithm
180-
// will first look for an .ngfactory.ts file in its place, which will be requested here.
181-
// * when the user writes a bad import.
182-
// * when a file is present in one compilation and removed in the next incremental step.
183-
//
184-
// Note that this does not add the filename to `notShims`, so this path is not cached.
185-
// That's okay as these cases above are edge cases and do not occur regularly in normal
186-
// operations.
187-
return undefined;
188-
}
176+
inputFile = this.delegate.getSourceFile(baseFileName, ts.ScriptTarget.Latest);
189177
}
190-
191-
// Retrieve the original file for which the shim will be generated.
192-
const inputFile = this.delegate.getSourceFile(baseFileName, ts.ScriptTarget.Latest);
193178
if (inputFile === undefined || isShim(inputFile)) {
194-
// Something strange happened here. This case is also not cached in `notShims`, but this
195-
// path is not expected to occur in reality so this shouldn't be a problem.
179+
// This isn't a shim after all since there is no original file which would have triggered
180+
// its generation, even though the path is right. There are a few reasons why this could
181+
// occur:
182+
//
183+
// * when resolving an import to an .ngfactory.d.ts file, the module resolution algorithm
184+
// will first look for an .ngfactory.ts file in its place, which will be requested here.
185+
// * when the user writes a bad import.
186+
// * when a file is present in one compilation and removed in the next incremental step.
187+
//
188+
// Note that this does not add the filename to `notShims`, so this path is not cached.
189+
// That's okay as these cases above are edge cases and do not occur regularly in normal
190+
// operations.
196191
return undefined;
197192
}
198193

packages/compiler-cli/test/perform_watch_spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ describe('perform watch', () => {
121121
expectNoDiagnostics(config.options, host.diagnostics);
122122

123123
expect(fileExistsSpy!).not.toHaveBeenCalledWith(mainTsPath);
124-
expect(fileExistsSpy!).toHaveBeenCalledWith(utilTsPath);
125124
expect(getSourceFileSpy!).not.toHaveBeenCalledWith(mainTsPath, jasmine.anything());
126125
expect(getSourceFileSpy!).toHaveBeenCalledWith(utilTsPath, jasmine.anything());
127126

0 commit comments

Comments
 (0)