New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
perf: AOT compilation optimizations #38539
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
packages/compiler-cli/src/ngtsc/annotations/src/typecheck_scopes.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC 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 {CssSelector, SchemaMetadata, SelectorMatcher} from '@angular/compiler'; | ||
import * as ts from 'typescript'; | ||
|
||
import {Reference} from '../../imports'; | ||
import {DirectiveMeta, flattenInheritedDirectiveMetadata, MetadataReader} from '../../metadata'; | ||
import {ClassDeclaration} from '../../reflection'; | ||
import {ComponentScopeReader} from '../../scope'; | ||
|
||
/** | ||
* The scope that is used for type-check code generation of a component template. | ||
*/ | ||
export interface TypeCheckScope { | ||
/** | ||
* A `SelectorMatcher` instance that contains the flattened directive metadata of all directives | ||
* that are in the compilation scope of the declaring NgModule. | ||
*/ | ||
matcher: SelectorMatcher<DirectiveMeta>; | ||
|
||
/** | ||
* The pipes that are available in the compilation scope. | ||
*/ | ||
pipes: Map<string, Reference<ClassDeclaration<ts.ClassDeclaration>>>; | ||
|
||
/** | ||
* The schemas that are used in this scope. | ||
*/ | ||
schemas: SchemaMetadata[]; | ||
} | ||
|
||
/** | ||
* Computes scope information to be used in template type checking. | ||
*/ | ||
export class TypeCheckScopes { | ||
/** | ||
* Cache of flattened directive metadata. Because flattened metadata is scope-invariant it's | ||
* cached individually, such that all scopes refer to the same flattened metadata. | ||
*/ | ||
private flattenedDirectiveMetaCache = new Map<ClassDeclaration, DirectiveMeta>(); | ||
JoostK marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* Cache of the computed type check scope per NgModule declaration. | ||
*/ | ||
private scopeCache = new Map<ClassDeclaration, TypeCheckScope>(); | ||
|
||
constructor(private scopeReader: ComponentScopeReader, private metaReader: MetadataReader) {} | ||
|
||
/** | ||
* Computes the type-check scope information for the component declaration. If the NgModule | ||
* contains an error, then 'error' is returned. If the component is not declared in any NgModule, | ||
* an empty type-check scope is returned. | ||
*/ | ||
getTypeCheckScope(node: ClassDeclaration): TypeCheckScope|'error' { | ||
const matcher = new SelectorMatcher<DirectiveMeta>(); | ||
const pipes = new Map<string, Reference<ClassDeclaration<ts.ClassDeclaration>>>(); | ||
|
||
const scope = this.scopeReader.getScopeForComponent(node); | ||
if (scope === null) { | ||
return {matcher, pipes, schemas: []}; | ||
} else if (scope === 'error') { | ||
return scope; | ||
} | ||
|
||
if (this.scopeCache.has(scope.ngModule)) { | ||
return this.scopeCache.get(scope.ngModule)!; | ||
} | ||
|
||
for (const meta of scope.compilation.directives) { | ||
if (meta.selector !== null) { | ||
const extMeta = this.getInheritedDirectiveMetadata(meta.ref); | ||
matcher.addSelectables(CssSelector.parse(meta.selector), extMeta); | ||
} | ||
} | ||
|
||
for (const {name, ref} of scope.compilation.pipes) { | ||
if (!ts.isClassDeclaration(ref.node)) { | ||
throw new Error(`Unexpected non-class declaration ${ | ||
ts.SyntaxKind[ref.node.kind]} for pipe ${ref.debugName}`); | ||
} | ||
pipes.set(name, ref as Reference<ClassDeclaration<ts.ClassDeclaration>>); | ||
} | ||
|
||
const typeCheckScope: TypeCheckScope = {matcher, pipes, schemas: scope.schemas}; | ||
this.scopeCache.set(scope.ngModule, typeCheckScope); | ||
return typeCheckScope; | ||
} | ||
|
||
private getInheritedDirectiveMetadata(ref: Reference<ClassDeclaration>): DirectiveMeta { | ||
const clazz = ref.node; | ||
if (this.flattenedDirectiveMetaCache.has(clazz)) { | ||
return this.flattenedDirectiveMetaCache.get(clazz)!; | ||
} | ||
|
||
const meta = flattenInheritedDirectiveMetadata(this.metaReader, ref); | ||
this.flattenedDirectiveMetaCache.set(clazz, meta); | ||
return meta; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if instead, we make
expression
inside the directive metadata into a closure which produces the expression only on demand? That would fix the performance issues and simplify all the downstream logic, I think.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually quite like the way it currently is. The result of the closure would ideally be memoized to avoid repeated computations. This is certainly something we could do; I just think I prefer this straightforward post-processing of the matched directives. Pipes need to be processed in the same way anyway, so currently it's nicely symmetrical. Additionally, the
SelectorMatcher
is currently context-free so could theoretically also be cached (but the benefits of that are quite small).If you feel like we should continue capturing all directives in the resolve metadata (for subsequent compilation step to use) then it has to become a closure. Currently, storing only the reduced set is actually beneficial to perf downstream as there will be less directives to match in
TemplateDefinitionBuilder
(actually that becomes a noop as all directives will be matched) and the linker work also benefits from not having to do directive matching itself.