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
53
packages/compiler-cli/src/ngtsc/incremental/api.ts
Normal file
53
packages/compiler-cli/src/ngtsc/incremental/api.ts
Normal file
@ -0,0 +1,53 @@
|
||||
/**
|
||||
* @license
|
||||
* Copyright Google Inc. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by an MIT-style license that can be
|
||||
* found in the LICENSE file at https://angular.io/license
|
||||
*/
|
||||
|
||||
import * as ts from 'typescript';
|
||||
|
||||
/**
|
||||
* Interface of the incremental build engine.
|
||||
*
|
||||
* `W` is a generic type representing a unit of work. This is generic to avoid a cyclic dependency
|
||||
* between the incremental engine API definition and its consumer(s).
|
||||
*/
|
||||
export interface IncrementalBuild<W> {
|
||||
/**
|
||||
* Retrieve the prior analysis work, if any, done for the given source file.
|
||||
*/
|
||||
priorWorkFor(sf: ts.SourceFile): W[]|null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tracks dependencies between source files or resources in the application.
|
||||
*/
|
||||
export interface DependencyTracker<T extends{fileName: string} = ts.SourceFile> {
|
||||
/**
|
||||
* Record that the file `from` depends on the file `on`.
|
||||
*/
|
||||
addDependency(from: T, on: T): void;
|
||||
|
||||
/**
|
||||
* Record that the file `from` depends on the resource file `on`.
|
||||
*/
|
||||
addResourceDependency(from: T, on: string): void;
|
||||
|
||||
/**
|
||||
* Record that the file `from` depends on the file `on` as well as `on`'s direct dependencies.
|
||||
*
|
||||
* This operation is reified immediately, so if future dependencies are added to `on` they will
|
||||
* not automatically be added to `from`.
|
||||
*/
|
||||
addTransitiveDependency(from: T, on: T): void;
|
||||
|
||||
/**
|
||||
* Record that the file `from` depends on the resource dependencies of `resourcesOf`.
|
||||
*
|
||||
* This operation is reified immediately, so if future resource dependencies are added to
|
||||
* `resourcesOf` they will not automatically be added to `from`.
|
||||
*/
|
||||
addTransitiveResources(from: T, resourcesOf: T): void;
|
||||
}
|
Reference in New Issue
Block a user