Skip to content

Commit 4651b93

Browse files
crisbetoatscott
authored andcommitted
refactor(compiler-cli): pre-compute key
Updates the TCB metadata to pre-compute and store the `TcbReferenceKey`, instead of computing it on the fly.
1 parent 8fe025f commit 4651b93

3 files changed

Lines changed: 37 additions & 32 deletions

File tree

packages/compiler-cli/src/ngtsc/typecheck/api/api.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,22 +32,26 @@ import {ClassDeclaration} from '../../reflection';
3232

3333
export interface TcbReferenceMetadata {
3434
/** The name of the class */
35-
name: string;
35+
readonly name: string;
3636
/** The module path where the symbol is located, or null if local/ambient */
37-
moduleName: string | null;
37+
readonly moduleName: string | null;
3838
/** True if the symbol successfully emitted locally (no external import required) */
39-
isLocal: boolean;
39+
readonly isLocal: boolean;
4040
/** If the reference could not be externally emitted, this string holds the diagnostic reason why */
41-
unexportedDiagnostic: string | null;
41+
readonly unexportedDiagnostic: string | null;
42+
43+
/** Key used to uniquely identify the target of this reference. */
44+
readonly key: TcbReferenceKey;
45+
4246
/**
4347
* Defines the `AbsoluteSourceSpan` of the target's node name, if available.
4448
*/
45-
nodeNameSpan?: AbsoluteSourceSpan;
49+
readonly nodeNameSpan?: AbsoluteSourceSpan;
4650

4751
/**
4852
* The absolute path to the file containing the reference node, if available.
4953
*/
50-
nodeFilePath?: string;
54+
readonly nodeFilePath?: string;
5155
}
5256

5357
export type TcbReferenceKey = string & {__brand: 'TcbReferenceKey'};

packages/compiler-cli/src/ngtsc/typecheck/src/environment.ts

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,15 @@ export class Environment extends ReferenceEmitEnvironment {
6464
* type constructor, or to an inline type constructor.
6565
*/
6666
typeCtorFor(dir: TcbDirectiveMetadata): TcbExpr {
67-
const key = getTcbReferenceKey(dir.ref);
68-
if (this.typeCtors.has(key)) {
69-
return new TcbExpr(this.typeCtors.get(key)!);
67+
if (this.typeCtors.has(dir.ref.key)) {
68+
return new TcbExpr(this.typeCtors.get(dir.ref.key)!);
7069
}
7170

7271
if (dir.requiresInlineTypeCtor) {
7372
// The constructor has already been created inline, we just need to construct a reference to
7473
// it.
7574
const typeCtorExpr = `${this.referenceTcbValue(dir.ref).print()}.ngTypeCtor`;
76-
this.typeCtors.set(key, typeCtorExpr);
75+
this.typeCtors.set(dir.ref.key, typeCtorExpr);
7776
return new TcbExpr(typeCtorExpr);
7877
} else {
7978
const fnName = `_ctor${this.nextIds.typeCtor++}`;
@@ -91,7 +90,7 @@ export class Environment extends ReferenceEmitEnvironment {
9190
const typeParams = dir.typeParameters || undefined;
9291
const typeCtor = generateTypeCtorDeclarationFn(this, meta, nodeTypeRef, typeParams);
9392
this.typeCtorStatements.push(typeCtor);
94-
this.typeCtors.set(key, fnName);
93+
this.typeCtors.set(dir.ref.key, fnName);
9594
return new TcbExpr(fnName);
9695
}
9796
}
@@ -100,15 +99,14 @@ export class Environment extends ReferenceEmitEnvironment {
10099
* Get an expression referring to an instance of the given pipe.
101100
*/
102101
pipeInst(pipe: TcbPipeMetadata): TcbExpr {
103-
const key = getTcbReferenceKey(pipe.ref);
104-
if (this.pipeInsts.has(key)) {
105-
return new TcbExpr(this.pipeInsts.get(key)!);
102+
if (this.pipeInsts.has(pipe.ref.key)) {
103+
return new TcbExpr(this.pipeInsts.get(pipe.ref.key)!);
106104
}
107105

108106
const pipeType = this.referenceTcbValue(pipe.ref);
109107
const pipeInstId = `_pipe${this.nextIds.pipeInst++}`;
110108

111-
this.pipeInsts.set(key, pipeInstId);
109+
this.pipeInsts.set(pipe.ref.key, pipeInstId);
112110
this.pipeInstStatements.push(declareVariable(new TcbExpr(pipeInstId), pipeType));
113111
return new TcbExpr(pipeInstId);
114112
}
@@ -117,10 +115,3 @@ export class Environment extends ReferenceEmitEnvironment {
117115
return [...this.pipeInstStatements, ...this.typeCtorStatements];
118116
}
119117
}
120-
121-
export function getTcbReferenceKey(ref: TcbReferenceMetadata): TcbReferenceKey {
122-
if (ref.nodeFilePath !== undefined && ref.nodeNameSpan !== undefined) {
123-
return `${ref.nodeFilePath}#${ref.nodeNameSpan.start}` as TcbReferenceKey;
124-
}
125-
return (ref.moduleName ? `${ref.moduleName}#${ref.name}` : ref.name) as TcbReferenceKey;
126-
}

packages/compiler-cli/src/ngtsc/typecheck/src/tcb_adapter.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ import {
1414
TcbReferenceMetadata,
1515
TcbInputMapping,
1616
TcbPipeMetadata,
17-
TypeCheckableDirectiveMeta,
1817
TcbTypeParameter,
18+
TcbReferenceKey,
19+
TypeCheckableDirectiveMeta,
1920
} from '../api';
2021
import {Environment} from './environment';
2122
import {ImportFlags, ReferenceEmitKind, Reference} from '../../imports';
@@ -279,19 +280,28 @@ function extractReferenceMetadata(
279280
isLocal = false;
280281
}
281282

282-
const refMeta: TcbReferenceMetadata = {
283+
const nodeName = ref.node?.name as ts.Identifier | undefined;
284+
const nodeNameSpan = nodeName
285+
? new AbsoluteSourceSpan(nodeName.getStart(), nodeName.getEnd())
286+
: undefined;
287+
const nodeFilePath = nodeName?.getSourceFile().fileName;
288+
let key: TcbReferenceKey;
289+
290+
if (nodeFilePath !== undefined && nodeNameSpan !== undefined) {
291+
key = `${nodeFilePath}#${nodeNameSpan.start}` as TcbReferenceKey;
292+
} else {
293+
key = (moduleName ? `${moduleName}#${name}` : name) as TcbReferenceKey;
294+
}
295+
296+
return {
283297
name,
284298
moduleName,
285299
isLocal,
286300
unexportedDiagnostic,
287-
};
288-
const nodeName = ref.node?.name;
289-
if (nodeName) {
290-
refMeta.nodeNameSpan = new AbsoluteSourceSpan(nodeName.getStart(), nodeName.getEnd());
291-
refMeta.nodeFilePath = nodeName.getSourceFile().fileName;
292-
}
293-
294-
return refMeta;
301+
nodeNameSpan,
302+
nodeFilePath,
303+
key,
304+
} satisfies TcbReferenceMetadata;
295305
}
296306

297307
function extractNameFromExpr(node: ts.Node): string | null {

0 commit comments

Comments
 (0)