Skip to content

Commit

Permalink
fix(bazel): improve performance of tsHost.writeFile() (#34331)
Browse files Browse the repository at this point in the history
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 11, 2019
1 parent b4d49ea commit d7c459a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
13 changes: 7 additions & 6 deletions packages/bazel/src/ngc-wrapped/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
};
Expand Down Expand Up @@ -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};
Expand Down
3 changes: 2 additions & 1 deletion packages/bazel/src/ngc-wrapped/tsconfig.json
Original file line number Diff line number Diff line change
@@ -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.