Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
refactor(compiler-cli): pre-compute key
Updates the TCB metadata to pre-compute and store the `TcbReferenceKey`, instead of computing it on the fly.
  • Loading branch information
crisbeto committed Apr 2, 2026
commit d04daa4cb281e9cf62db499461df11e32fed7da7
16 changes: 10 additions & 6 deletions packages/compiler-cli/src/ngtsc/typecheck/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,26 @@ import {ClassDeclaration} from '../../reflection';

export interface TcbReferenceMetadata {
/** The name of the class */
name: string;
readonly name: string;
/** The module path where the symbol is located, or null if local/ambient */
moduleName: string | null;
readonly moduleName: string | null;
/** True if the symbol successfully emitted locally (no external import required) */
isLocal: boolean;
readonly isLocal: boolean;
/** If the reference could not be externally emitted, this string holds the diagnostic reason why */
unexportedDiagnostic: string | null;
readonly unexportedDiagnostic: string | null;

/** Key used to uniquely identify the target of this reference. */
readonly key: TcbReferenceKey;

/**
* Defines the `AbsoluteSourceSpan` of the target's node name, if available.
*/
nodeNameSpan?: AbsoluteSourceSpan;
readonly nodeNameSpan?: AbsoluteSourceSpan;

/**
* The absolute path to the file containing the reference node, if available.
*/
nodeFilePath?: string;
readonly nodeFilePath?: string;
}

export type TcbReferenceKey = string & {__brand: 'TcbReferenceKey'};
Expand Down
23 changes: 7 additions & 16 deletions packages/compiler-cli/src/ngtsc/typecheck/src/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,15 @@ export class Environment extends ReferenceEmitEnvironment {
* type constructor, or to an inline type constructor.
*/
typeCtorFor(dir: TcbDirectiveMetadata): TcbExpr {
const key = getTcbReferenceKey(dir.ref);
if (this.typeCtors.has(key)) {
return new TcbExpr(this.typeCtors.get(key)!);
if (this.typeCtors.has(dir.ref.key)) {
return new TcbExpr(this.typeCtors.get(dir.ref.key)!);
}

if (dir.requiresInlineTypeCtor) {
// The constructor has already been created inline, we just need to construct a reference to
// it.
const typeCtorExpr = `${this.referenceTcbValue(dir.ref).print()}.ngTypeCtor`;
this.typeCtors.set(key, typeCtorExpr);
this.typeCtors.set(dir.ref.key, typeCtorExpr);
return new TcbExpr(typeCtorExpr);
} else {
const fnName = `_ctor${this.nextIds.typeCtor++}`;
Expand All @@ -91,7 +90,7 @@ export class Environment extends ReferenceEmitEnvironment {
const typeParams = dir.typeParameters || undefined;
const typeCtor = generateTypeCtorDeclarationFn(this, meta, nodeTypeRef, typeParams);
this.typeCtorStatements.push(typeCtor);
this.typeCtors.set(key, fnName);
this.typeCtors.set(dir.ref.key, fnName);
return new TcbExpr(fnName);
}
}
Expand All @@ -100,15 +99,14 @@ export class Environment extends ReferenceEmitEnvironment {
* Get an expression referring to an instance of the given pipe.
*/
pipeInst(pipe: TcbPipeMetadata): TcbExpr {
const key = getTcbReferenceKey(pipe.ref);
if (this.pipeInsts.has(key)) {
return new TcbExpr(this.pipeInsts.get(key)!);
if (this.pipeInsts.has(pipe.ref.key)) {
return new TcbExpr(this.pipeInsts.get(pipe.ref.key)!);
}

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

this.pipeInsts.set(key, pipeInstId);
this.pipeInsts.set(pipe.ref.key, pipeInstId);
this.pipeInstStatements.push(declareVariable(new TcbExpr(pipeInstId), pipeType));
return new TcbExpr(pipeInstId);
}
Expand All @@ -117,10 +115,3 @@ export class Environment extends ReferenceEmitEnvironment {
return [...this.pipeInstStatements, ...this.typeCtorStatements];
}
}

export function getTcbReferenceKey(ref: TcbReferenceMetadata): TcbReferenceKey {
if (ref.nodeFilePath !== undefined && ref.nodeNameSpan !== undefined) {
return `${ref.nodeFilePath}#${ref.nodeNameSpan.start}` as TcbReferenceKey;
}
return (ref.moduleName ? `${ref.moduleName}#${ref.name}` : ref.name) as TcbReferenceKey;
}
30 changes: 20 additions & 10 deletions packages/compiler-cli/src/ngtsc/typecheck/src/tcb_adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import {
TcbReferenceMetadata,
TcbInputMapping,
TcbPipeMetadata,
TypeCheckableDirectiveMeta,
TcbTypeParameter,
TcbReferenceKey,
TypeCheckableDirectiveMeta,
} from '../api';
import {Environment} from './environment';
import {ImportFlags, ReferenceEmitKind, Reference} from '../../imports';
Expand Down Expand Up @@ -279,19 +280,28 @@ function extractReferenceMetadata(
isLocal = false;
}

const refMeta: TcbReferenceMetadata = {
const nodeName = ref.node?.name as ts.Identifier | undefined;
const nodeNameSpan = nodeName
? new AbsoluteSourceSpan(nodeName.getStart(), nodeName.getEnd())
: undefined;
const nodeFilePath = nodeName?.getSourceFile().fileName;
let key: TcbReferenceKey;

if (nodeFilePath !== undefined && nodeNameSpan !== undefined) {
key = `${nodeFilePath}#${nodeNameSpan.start}` as TcbReferenceKey;
} else {
key = (moduleName ? `${moduleName}#${name}` : name) as TcbReferenceKey;
}

return {
name,
moduleName,
isLocal,
unexportedDiagnostic,
};
const nodeName = ref.node?.name;
if (nodeName) {
refMeta.nodeNameSpan = new AbsoluteSourceSpan(nodeName.getStart(), nodeName.getEnd());
refMeta.nodeFilePath = nodeName.getSourceFile().fileName;
}

return refMeta;
nodeNameSpan,
nodeFilePath,
key,
} satisfies TcbReferenceMetadata;
}

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