From 41faf73bfc36e4cb7b911e2b8b262ee274d6bd41 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Mon, 8 Nov 2021 07:09:06 +0000 Subject: [PATCH] Switch back to putting Node IDs on Nodes. --- src/compiler/checker.ts | 31 ++++++++++++++----------------- src/compiler/types.ts | 1 + src/compiler/utilities.ts | 3 +++ 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 61c2d1568bb..2dde040f501 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -277,15 +277,13 @@ namespace ts { this.flags = 0; } - const idCache = new Map(); export function getNodeId(node: Node): number { - let result = idCache.get(node); - if (!result) { - result = nextNodeId; - idCache.set(node, result); + let id = node.id; + if (!id) { + node.id = id = nextNodeId; nextNodeId++; } - return result; + return id; } export function getSymbolId(symbol: Symbol): SymbolId { @@ -963,7 +961,7 @@ namespace ts { let flowInvocationCount = 0; let lastFlowNode: FlowNode | undefined; let lastFlowNodeReachable: boolean; - let flowTypeCache: ESMap | undefined; + let flowTypeCache: Type[] | undefined; const emptyStringType = getStringLiteralType(""); const zeroType = getNumberLiteralType(0); @@ -977,7 +975,7 @@ namespace ts { const maximumSuggestionCount = 10; const mergedSymbols: Symbol[] = []; const symbolLinks: SymbolLinks[] = []; - const nodeLinks = new Map(); + const nodeLinks: NodeLinks[] = []; const flowLoopCaches: ESMap[] = []; const flowLoopNodes: FlowNode[] = []; const flowLoopKeys: string[] = []; @@ -1466,11 +1464,8 @@ namespace ts { } function getNodeLinks(node: Node): NodeLinks { - let result = nodeLinks.get(node); - if (!result) { - nodeLinks.set(node, result = new (NodeLinks as any)() as NodeLinks); - } - return result; + const nodeId = getNodeId(node); + return nodeLinks[nodeId] || (nodeLinks[nodeId] = new (NodeLinks as any)()); } function isGlobalSourceFile(node: Node) { @@ -33790,7 +33785,7 @@ namespace ts { } // If a type has been cached for the node, return it. if (node.flags & NodeFlags.TypeCached && flowTypeCache) { - const cachedType = flowTypeCache.get(node); + const cachedType = flowTypeCache[getNodeId(node)]; if (cachedType) { return cachedType; } @@ -33799,8 +33794,8 @@ namespace ts { const type = checkExpression(node); // If control flow analysis was required to determine the type, it is worth caching. if (flowInvocationCount !== startInvocationCount) { - const cache = (flowTypeCache ||= new Map()); - cache.set(node, type); + const cache = (flowTypeCache ||= []); + cache[getNodeId(node)] = type; setNodeFlags(node, node.flags | NodeFlags.TypeCached); } return type; @@ -41603,7 +41598,9 @@ namespace ts { } function getNodeCheckFlags(node: Node): NodeCheckFlags { - return nodeLinks.get(node)?.flags || 0; + const nodeId = node.id || 0; + if (nodeId < 0 || nodeId >= nodeLinks.length) return 0; + return nodeLinks[nodeId]?.flags || 0; } function getEnumMemberValue(node: EnumMember): string | number | undefined { diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 7f843c10365..7e3a236036a 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -857,6 +857,7 @@ namespace ts { /* @internal */ readonly transformFlags: TransformFlags; // Flags for transforms readonly decorators?: NodeArray; // Array of decorators (in document order) readonly modifiers?: ModifiersArray; // Array of modifiers + /* @internal */ id?: NodeId; // Unique id (used to look up NodeLinks) readonly parent: Node; // Parent node (initialized by binding) /* @internal */ original?: Node; // The original node if this is an updated node. /* @internal */ symbol: Symbol; // Symbol declared by node (initialized by binding) diff --git a/src/compiler/utilities.ts b/src/compiler/utilities.ts index ed7707d0091..8caba5b205a 100644 --- a/src/compiler/utilities.ts +++ b/src/compiler/utilities.ts @@ -5821,6 +5821,7 @@ namespace ts { this.pos = pos; this.end = end; this.kind = kind; + this.id = 0; this.flags = NodeFlags.None; this.modifierFlagsCache = ModifierFlags.None; this.transformFlags = TransformFlags.None; @@ -5832,6 +5833,7 @@ namespace ts { this.pos = pos; this.end = end; this.kind = kind; + this.id = 0; this.flags = NodeFlags.None; this.transformFlags = TransformFlags.None; this.parent = undefined!; @@ -5841,6 +5843,7 @@ namespace ts { this.pos = pos; this.end = end; this.kind = kind; + this.id = 0; this.flags = NodeFlags.None; this.transformFlags = TransformFlags.None; this.parent = undefined!;