Skip to content
Permalink
Browse files

fix(bazel): improve performance of tsHost.writeFile() (#34331)

Removing from an array incurs O(n^2) cost, and could be mitigated with the use of a Set instead.

PR Close #34331
  • Loading branch information
kyliau authored and AndrewKushnir committed Dec 10, 2019
1 parent b4d49ea commit d7c459a81a4dc54add9da963049f4431248c5f09
Showing with 9 additions and 7 deletions.
  1. +7 −6 packages/bazel/src/ngc-wrapped/index.ts
  2. +2 −1 packages/bazel/src/ngc-wrapped/tsconfig.json
@@ -199,16 +199,15 @@ export function compile({allDepsCompiledWithBazel = true, compilerOpts, tsHost,
throw new Error(`Couldn't find bazel bin in the rootDirs: ${compilerOpts.rootDirs}`);
}

const writtenExpectedOuts = expectedOuts.map(p => p.replace(/\\/g, '/'));
const expectedOutsSet = new Set(expectedOuts.map(p => p.replace(/\\/g, '/')));

const originalWriteFile = tsHost.writeFile.bind(tsHost);
tsHost.writeFile =
(fileName: string, content: string, writeByteOrderMark: boolean,
onError?: (message: string) => void, sourceFiles?: ts.SourceFile[]) => {
const relative = relativeToRootDirs(fileName.replace(/\\/g, '/'), [compilerOpts.rootDir]);
const expectedIdx = writtenExpectedOuts.findIndex(o => o === relative);
if (expectedIdx >= 0) {
writtenExpectedOuts.splice(expectedIdx, 1);
if (expectedOutsSet.has(relative)) {
expectedOutsSet.delete(relative);
originalWriteFile(fileName, content, writeByteOrderMark, onError, sourceFiles);
}
};
@@ -425,8 +424,10 @@ export function compile({allDepsCompiledWithBazel = true, compilerOpts, tsHost,
fs.writeFileSync(bazelOpts.tsickleExternsPath, externs);
}

for (let i = 0; i < writtenExpectedOuts.length; i++) {
originalWriteFile(writtenExpectedOuts[i], '', false);
// There might be some expected output files that are not written by the
// compiler. In this case, just write an empty file.
for (const fileName of expectedOutsSet) {
originalWriteFile(fileName, '', false);
}

return {program, diagnostics};
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"lib": ["es5", "es2015.collection", "es2015.core"],
"types": ["node"]
"types": ["node"],
"downlevelIteration": true
}
}

0 comments on commit d7c459a

Please sign in to comment.
You can’t perform that action at this time.