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
This commit is contained in:
		
							parent
							
								
									8572911e94
								
							
						
					
					
						commit
						08ea38f197
					
				| @ -199,16 +199,15 @@ export function compile({allDepsCompiledWithBazel = true, compilerOpts, tsHost, | |||||||
|     throw new Error(`Couldn't find bazel bin in the rootDirs: ${compilerOpts.rootDirs}`); |     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); |   const originalWriteFile = tsHost.writeFile.bind(tsHost); | ||||||
|   tsHost.writeFile = |   tsHost.writeFile = | ||||||
|       (fileName: string, content: string, writeByteOrderMark: boolean, |       (fileName: string, content: string, writeByteOrderMark: boolean, | ||||||
|        onError?: (message: string) => void, sourceFiles?: ts.SourceFile[]) => { |        onError?: (message: string) => void, sourceFiles?: ts.SourceFile[]) => { | ||||||
|         const relative = relativeToRootDirs(fileName.replace(/\\/g, '/'), [compilerOpts.rootDir]); |         const relative = relativeToRootDirs(fileName.replace(/\\/g, '/'), [compilerOpts.rootDir]); | ||||||
|         const expectedIdx = writtenExpectedOuts.findIndex(o => o === relative); |         if (expectedOutsSet.has(relative)) { | ||||||
|         if (expectedIdx >= 0) { |           expectedOutsSet.delete(relative); | ||||||
|           writtenExpectedOuts.splice(expectedIdx, 1); |  | ||||||
|           originalWriteFile(fileName, content, writeByteOrderMark, onError, sourceFiles); |           originalWriteFile(fileName, content, writeByteOrderMark, onError, sourceFiles); | ||||||
|         } |         } | ||||||
|       }; |       }; | ||||||
| @ -425,8 +424,10 @@ export function compile({allDepsCompiledWithBazel = true, compilerOpts, tsHost, | |||||||
|     fs.writeFileSync(bazelOpts.tsickleExternsPath, externs); |     fs.writeFileSync(bazelOpts.tsickleExternsPath, externs); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   for (let i = 0; i < writtenExpectedOuts.length; i++) { |   // There might be some expected output files that are not written by the
 | ||||||
|     originalWriteFile(writtenExpectedOuts[i], '', false); |   // compiler. In this case, just write an empty file.
 | ||||||
|  |   for (const fileName of expectedOutsSet) { | ||||||
|  |     originalWriteFile(fileName, '', false); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   return {program, diagnostics}; |   return {program, diagnostics}; | ||||||
|  | |||||||
| @ -1,6 +1,7 @@ | |||||||
| { | { | ||||||
|   "compilerOptions": { |   "compilerOptions": { | ||||||
|     "lib": ["es5", "es2015.collection", "es2015.core"], |     "lib": ["es5", "es2015.collection", "es2015.core"], | ||||||
|     "types": ["node"] |     "types": ["node"], | ||||||
|  |     "downlevelIteration": true | ||||||
|   } |   } | ||||||
| } | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user