From ef7e09c62b37aa3b84f149afcd41ae4d3d59e498 Mon Sep 17 00:00:00 2001 From: Titian Cernicova-Dragomir Date: Mon, 8 Jul 2024 23:54:38 +0100 Subject: [PATCH] Make Signature monomorphic --- src/compiler/utilities.ts | 93 ++++++++++++++++++++++++++++++++++++--- src/services/services.ts | 34 ++++---------- 2 files changed, 95 insertions(+), 32 deletions(-) diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index 1b972e990f7fb..50f2fbd34bbb3 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -560,6 +560,7 @@ import { TypeElement, TypeFlags, TypeLiteralNode, + TypeMapper, TypeNode, TypeNodeSyntaxKind, TypeParameter, @@ -8209,11 +8210,91 @@ function Type(this: Type, checker: TypeChecker, flags: TypeFlags) { } } -function Signature(this: Signature, checker: TypeChecker, flags: SignatureFlags) { - // Note: if modifying this, be sure to update SignatureObject in src/services/services.ts - this.flags = flags; - if (Debug.isDebugging) { - this.checker = checker; +class SignatureDataImpl { +} +/** @internal */ +export class SignatureImpl { + flags: SignatureFlags; + checker!: TypeChecker; + declaration?: SignatureDeclaration | JSDocSignature; // Originating declaration + typeParameters?: readonly TypeParameter[]; // Type parameters (undefined if non-generic) + parameters: readonly Symbol[]; // Parameters + thisParameter?: Symbol; // symbol of this-type parameter + resolvedReturnType?: Type; // Lazily set by `getReturnTypeOfSignature`. + resolvedTypePredicate?: TypePredicate; + minArgumentCount: number; // Number of non-optional parameters + resolvedMinArgumentCount?: number; // Number of non-optional parameters (excluding trailing `void`) + target?: Signature; // Instantiation target + mapper?: TypeMapper; // Instantiation mapper + compositeSignatures?: Signature[]; // Underlying signatures of a union/intersection signature + compositeKind?: TypeFlags; // TypeFlags.Union if the underlying signatures are from union members, otherwise TypeFlags.Intersection + + constructor(checker: TypeChecker, flags: SignatureFlags) { + // Note: if modifying this, be sure to update SignatureObject in src/services/services.ts + this.flags = flags; + if (Debug.isDebugging) { + this.checker = checker; + } + this.declaration = undefined; + this.typeParameters = undefined; + this.parameters = undefined!; + this.thisParameter = undefined; + this.resolvedReturnType = undefined; + this.resolvedTypePredicate = undefined; + this.minArgumentCount = undefined!; + this.resolvedMinArgumentCount = undefined; + this.target = undefined; + this.mapper = undefined; + this.compositeSignatures = undefined; + this.compositeKind = undefined; + this._data = undefined; + } + // get data(): any { return this; } + _data: any; + get data() { + return this._data ??= new SignatureDataImpl(); + } + get erasedSignatureCache() { + return this.data.erasedSignatureCache; + } + set erasedSignatureCache(value: any) { + this.data.erasedSignatureCache = value; + } + get canonicalSignatureCache() { + return this.data.canonicalSignatureCache; + } + set canonicalSignatureCache(value: any) { + this.data.canonicalSignatureCache = value; + } + get baseSignatureCache() { + return this.data.baseSignatureCache; + } + set baseSignatureCache(value: any) { + this.data.baseSignatureCache = value; + } + get optionalCallSignatureCache() { + return this.data.optionalCallSignatureCache; + } + set optionalCallSignatureCache(value: any) { + this.data.optionalCallSignatureCache = value; + } + get isolatedSignatureType() { + return this.data.isolatedSignatureType; + } + set isolatedSignatureType(value: any) { + this.data.isolatedSignatureType = value; + } + get instantiations() { + return this.data.instantiations; + } + set instantiations(value: any) { + this.data.instantiations = value; + } + get implementationSignatureCache() { + return this.data.implementationSignatureCache; + } + set implementationSignatureCache(value: any) { + this.data.implementationSignatureCache = value; } } @@ -8272,7 +8353,7 @@ export const objectAllocator: ObjectAllocator = { getSourceFileConstructor: () => Node as any, getSymbolConstructor: () => Symbol as any, getTypeConstructor: () => Type as any, - getSignatureConstructor: () => Signature as any, + getSignatureConstructor: () => SignatureImpl as any, getSourceMapSourceConstructor: () => SourceMapSource as any, }; diff --git a/src/services/services.ts b/src/services/services.ts index 101fcebc91a99..d1d2f14fa7914 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -291,6 +291,7 @@ import { SignatureHelp, SignatureHelpItems, SignatureHelpItemsOptions, + SignatureImpl, SignatureKind, singleElementArray, skipTypeChecking, @@ -337,7 +338,6 @@ import { TypeFlags, TypeNode, TypeParameter, - TypePredicate, TypeReference, typeToDisplayParts, UnionOrIntersectionType, @@ -927,37 +927,19 @@ class TypeObject implements Type { } } -class SignatureObject implements Signature { - flags: SignatureFlags; - checker: TypeChecker; - declaration!: SignatureDeclaration; - typeParameters?: TypeParameter[]; - parameters!: Symbol[]; - thisParameter!: Symbol; - resolvedReturnType!: Type; - resolvedTypePredicate: TypePredicate | undefined; - minTypeArgumentCount!: number; - minArgumentCount!: number; - - // Undefined is used to indicate the value has not been computed. If, after computing, the - // symbol has no doc comment, then the empty array will be returned. - documentationComment?: SymbolDisplayPart[]; - jsDocTags?: JSDocTagInfo[]; // same - +class SignatureObject extends SignatureImpl implements Signature { constructor(checker: TypeChecker, flags: SignatureFlags) { - // Note: if modifying this, be sure to update Signature in src/compiler/types.ts - this.flags = flags; + super(checker, flags); this.checker = checker; } - getDeclaration(): SignatureDeclaration { - return this.declaration; + return this.declaration as SignatureDeclaration; } getTypeParameters(): TypeParameter[] | undefined { - return this.typeParameters; + return this.typeParameters as TypeParameter[]; } getParameters(): Symbol[] { - return this.parameters; + return this.parameters as Symbol[]; } getReturnType(): Type { return this.checker.getReturnTypeOfSignature(this); @@ -974,11 +956,11 @@ class SignatureObject implements Signature { } getDocumentationComment(): SymbolDisplayPart[] { - return this.documentationComment || (this.documentationComment = getDocumentationComment(singleElementArray(this.declaration), this.checker)); + return this.data.documentationComment || (this.data.documentationComment = getDocumentationComment(singleElementArray(this.declaration), this.checker)); } getJsDocTags(): JSDocTagInfo[] { - return this.jsDocTags || (this.jsDocTags = getJsDocTagsOfDeclarations(singleElementArray(this.declaration), this.checker)); + return this.data.jsDocTags || (this.data.jsDocTags = getJsDocTagsOfDeclarations(singleElementArray(this.declaration), this.checker)); } }