Skip to content

Commit 7efa09b

Browse files
frigus02alxhub
authored andcommitted
perf(bazel): use allowedInputs to avoid fs.stat (#46069)
Call tsc_wrapped's fileExists function instead of TypeScript's. tsc_wrapped (used internally by ngc-wrapped) has an optimization under bazel to avoid file system calls where possible. It takes advantage bazelOpts.allowedInputs, which contains a list of all files available for compilation. File system calls can be quite slow depending on the file system. In google3 I saw a 38 seconds compilation, which spent 6 seconds just doing fs.stat calls during module resolution. Those fs.stat calls are entirely gone after with this change. PR Close #46069
1 parent c3205d0 commit 7efa09b

1 file changed

Lines changed: 17 additions & 30 deletions

File tree

packages/bazel/src/ngc-wrapped/index.ts

Lines changed: 17 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -254,37 +254,8 @@ export function compile({
254254
}
255255
};
256256

257-
// Patch fileExists when resolving modules, so that CompilerHost can ask TypeScript to
258-
// resolve non-existing generated files that don't exist on disk, but are
259-
// synthetic and added to the `programWithStubs` based on real inputs.
260-
const generatedFileModuleResolverHost = Object.create(tsHost);
261-
generatedFileModuleResolverHost.fileExists = (fileName: string) => {
262-
const match = NGC_GEN_FILES.exec(fileName);
263-
if (match) {
264-
const [, file, suffix, ext] = match;
265-
// Performance: skip looking for files other than .d.ts or .ts
266-
if (ext !== '.ts' && ext !== '.d.ts') return false;
267-
if (suffix.indexOf('ngstyle') >= 0) {
268-
// Look for foo.css on disk
269-
fileName = file;
270-
} else {
271-
// Look for foo.d.ts or foo.ts on disk
272-
fileName = file + (ext || '');
273-
}
274-
}
275-
return tsHost.fileExists(fileName);
276-
};
277-
278-
function generatedFileModuleResolver(
279-
moduleName: string, containingFile: string,
280-
compilerOptions: ts.CompilerOptions): ts.ResolvedModuleWithFailedLookupLocations {
281-
return ts.resolveModuleName(
282-
moduleName, containingFile, compilerOptions, generatedFileModuleResolverHost);
283-
}
284-
285257
if (!bazelHost) {
286-
bazelHost = new CompilerHost(
287-
files, compilerOpts, bazelOpts, tsHost, fileLoader, generatedFileModuleResolver);
258+
bazelHost = new CompilerHost(files, compilerOpts, bazelOpts, tsHost, fileLoader);
288259
}
289260

290261
if (isInIvyMode) {
@@ -329,8 +300,24 @@ export function compile({
329300
bazelHost.transformTypesToClosure = true;
330301
}
331302

303+
// Patch fileExists when resolving modules, so that CompilerHost can ask TypeScript to
304+
// resolve non-existing generated files that don't exist on disk, but are
305+
// synthetic and added to the `programWithStubs` based on real inputs.
332306
const origBazelHostFileExist = bazelHost.fileExists;
333307
bazelHost.fileExists = (fileName: string) => {
308+
const match = NGC_GEN_FILES.exec(fileName);
309+
if (match) {
310+
const [, file, suffix, ext] = match;
311+
// Performance: skip looking for files other than .d.ts or .ts
312+
if (ext !== '.ts' && ext !== '.d.ts') return false;
313+
if (suffix.indexOf('ngstyle') >= 0) {
314+
// Look for foo.css on disk
315+
fileName = file;
316+
} else {
317+
// Look for foo.d.ts or foo.ts on disk
318+
fileName = file + (ext || '');
319+
}
320+
}
334321
if (NGC_ASSETS.test(fileName)) {
335322
return tsHost.fileExists(fileName);
336323
}

0 commit comments

Comments
 (0)