perf(ivy): reuse prior analysis work during incremental builds (#34288)
Previously, the compiler performed an incremental build by analyzing and resolving all classes in the program (even unchanged ones) and then using the dependency graph information to determine which .js files were stale and needed to be re-emitted. This algorithm produced "correct" rebuilds, but the cost of re-analyzing the entire program turned out to be higher than anticipated, especially for component-heavy compilations. To achieve performant rebuilds, it is necessary to reuse previous analysis results if possible. Doing this safely requires knowing when prior work is viable and when it is stale and needs to be re-done. The new algorithm implemented by this commit is such: 1) Each incremental build starts with knowledge of the last known good dependency graph and analysis results from the last successful build, plus of course information about the set of files changed. 2) The previous dependency graph's information is used to determine the set of source files which have "logically" changed. A source file is considered logically changed if it or any of its dependencies have physically changed (on disk) since the last successful compilation. Any logically unchanged dependencies have their dependency information copied over to the new dependency graph. 3) During the `TraitCompiler`'s loop to consider all source files in the program, if a source file is logically unchanged then its previous analyses are "adopted" (and their 'register' steps are run). If the file is logically changed, then it is re-analyzed as usual. 4) Then, incremental build proceeds as before, with the new dependency graph being used to determine the set of files which require re-emitting. This analysis reuse avoids template parsing operations in many circumstances and significantly reduces the time it takes ngtsc to rebuild a large application. Future work will increase performance even more, by tackling a variety of other opportunities to reuse or avoid work. PR Close #34288
This commit is contained in:

committed by
Kara Erickson

parent
50cdc0ac1b
commit
74edde0a94
@ -257,13 +257,8 @@ export class NgtscProgram implements api.Program {
|
||||
await Promise.all(promises);
|
||||
|
||||
this.perfRecorder.stop(analyzeSpan);
|
||||
this.compilation.resolve();
|
||||
|
||||
this.recordNgModuleScopeDependencies();
|
||||
|
||||
// At this point, analysis is complete and the compiler can now calculate which files need to be
|
||||
// emitted, so do that.
|
||||
this.incrementalDriver.recordSuccessfulAnalysis();
|
||||
this.resolveCompilation(this.compilation);
|
||||
}
|
||||
|
||||
listLazyRoutes(entryRoute?: string|undefined): api.LazyRoute[] {
|
||||
@ -339,17 +334,22 @@ export class NgtscProgram implements api.Program {
|
||||
this.perfRecorder.stop(analyzeFileSpan);
|
||||
}
|
||||
this.perfRecorder.stop(analyzeSpan);
|
||||
this.compilation.resolve();
|
||||
|
||||
this.recordNgModuleScopeDependencies();
|
||||
|
||||
// At this point, analysis is complete and the compiler can now calculate which files need to
|
||||
// be emitted, so do that.
|
||||
this.incrementalDriver.recordSuccessfulAnalysis();
|
||||
this.resolveCompilation(this.compilation);
|
||||
}
|
||||
return this.compilation;
|
||||
}
|
||||
|
||||
private resolveCompilation(compilation: TraitCompiler): void {
|
||||
compilation.resolve();
|
||||
|
||||
this.recordNgModuleScopeDependencies();
|
||||
|
||||
// At this point, analysis is complete and the compiler can now calculate which files need to
|
||||
// be emitted, so do that.
|
||||
this.incrementalDriver.recordSuccessfulAnalysis(compilation);
|
||||
}
|
||||
|
||||
emit(opts?: {
|
||||
emitFlags?: api.EmitFlags,
|
||||
cancellationToken?: ts.CancellationToken,
|
||||
@ -616,7 +616,8 @@ export class NgtscProgram implements api.Program {
|
||||
this.aliasingHost = new FileToModuleAliasingHost(this.fileToModuleHost);
|
||||
}
|
||||
|
||||
const evaluator = new PartialEvaluator(this.reflector, checker, this.incrementalDriver);
|
||||
const evaluator =
|
||||
new PartialEvaluator(this.reflector, checker, this.incrementalDriver.depGraph);
|
||||
const dtsReader = new DtsMetadataReader(checker, this.reflector);
|
||||
const localMetaRegistry = new LocalMetadataRegistry();
|
||||
const localMetaReader: MetadataReader = localMetaRegistry;
|
||||
@ -654,7 +655,7 @@ export class NgtscProgram implements api.Program {
|
||||
this.options.preserveWhitespaces || false, this.options.i18nUseExternalIds !== false,
|
||||
this.options.enableI18nLegacyMessageIdFormat !== false, this.moduleResolver,
|
||||
this.cycleAnalyzer, this.refEmitter, this.defaultImportTracker,
|
||||
this.closureCompilerEnabled, this.incrementalDriver),
|
||||
this.incrementalDriver.depGraph, this.closureCompilerEnabled),
|
||||
// TODO(alxhub): understand why the cast here is necessary (something to do with `null` not
|
||||
// being assignable to `unknown` when wrapped in `Readonly`).
|
||||
new DirectiveDecoratorHandler(
|
||||
@ -674,7 +675,7 @@ export class NgtscProgram implements api.Program {
|
||||
];
|
||||
|
||||
return new TraitCompiler(
|
||||
handlers, this.reflector, this.perfRecorder,
|
||||
handlers, this.reflector, this.perfRecorder, this.incrementalDriver,
|
||||
this.options.compileNonExportedClasses !== false, this.dtsTransforms);
|
||||
}
|
||||
|
||||
@ -684,38 +685,39 @@ export class NgtscProgram implements api.Program {
|
||||
*/
|
||||
private recordNgModuleScopeDependencies() {
|
||||
const recordSpan = this.perfRecorder.start('recordDependencies');
|
||||
const depGraph = this.incrementalDriver.depGraph;
|
||||
|
||||
for (const scope of this.scopeRegistry !.getCompilationScopes()) {
|
||||
const file = scope.declaration.getSourceFile();
|
||||
const ngModuleFile = scope.ngModule.getSourceFile();
|
||||
|
||||
// A change to any dependency of the declaration causes the declaration to be invalidated,
|
||||
// which requires the NgModule to be invalidated as well.
|
||||
const deps = this.incrementalDriver.getFileDependencies(file);
|
||||
this.incrementalDriver.trackFileDependencies(deps, ngModuleFile);
|
||||
depGraph.addTransitiveDependency(ngModuleFile, file);
|
||||
|
||||
// A change to the NgModule file should cause the declaration itself to be invalidated.
|
||||
this.incrementalDriver.trackFileDependency(ngModuleFile, file);
|
||||
depGraph.addDependency(file, ngModuleFile);
|
||||
|
||||
// A change to any directive/pipe in the compilation scope should cause the declaration to be
|
||||
// invalidated.
|
||||
for (const directive of scope.directives) {
|
||||
const dirSf = directive.ref.node.getSourceFile();
|
||||
const meta = this.metaReader !.getDirectiveMetadata(new Reference(scope.declaration));
|
||||
if (meta !== null && meta.isComponent) {
|
||||
// If a component's template changes, it might have affected the import graph, and thus the
|
||||
// remote scoping feature which is activated in the event of potential import cycles. Thus,
|
||||
// the module depends not only on the transitive dependencies of the component, but on its
|
||||
// resources as well.
|
||||
depGraph.addTransitiveResources(ngModuleFile, file);
|
||||
|
||||
// When a directive in scope is updated, the declaration needs to be recompiled as e.g.
|
||||
// a selector may have changed.
|
||||
this.incrementalDriver.trackFileDependency(dirSf, file);
|
||||
|
||||
// When any of the dependencies of the declaration changes, the NgModule scope may be
|
||||
// affected so a component within scope must be recompiled. Only components need to be
|
||||
// recompiled, as directives are not dependent upon the compilation scope.
|
||||
if (directive.isComponent) {
|
||||
this.incrementalDriver.trackFileDependencies(deps, dirSf);
|
||||
// A change to any directive/pipe in the compilation scope should cause the component to be
|
||||
// invalidated.
|
||||
for (const directive of scope.directives) {
|
||||
// When a directive in scope is updated, the component needs to be recompiled as e.g. a
|
||||
// selector may have changed.
|
||||
depGraph.addTransitiveDependency(file, directive.ref.node.getSourceFile());
|
||||
}
|
||||
for (const pipe of scope.pipes) {
|
||||
// When a pipe in scope is updated, the component needs to be recompiled as e.g. the
|
||||
// pipe's name may have changed.
|
||||
depGraph.addTransitiveDependency(file, pipe.ref.node.getSourceFile());
|
||||
}
|
||||
}
|
||||
for (const pipe of scope.pipes) {
|
||||
// When a pipe in scope is updated, the declaration needs to be recompiled as e.g.
|
||||
// the pipe's name may have changed.
|
||||
this.incrementalDriver.trackFileDependency(pipe.ref.node.getSourceFile(), file);
|
||||
}
|
||||
}
|
||||
this.perfRecorder.stop(recordSpan);
|
||||
|
Reference in New Issue
Block a user