Skip to content

Commit 41faf73

Browse files
Switch back to putting Node IDs on Nodes.
1 parent 5479adf commit 41faf73

File tree

3 files changed

+18
-17
lines changed

3 files changed

+18
-17
lines changed

src/compiler/checker.ts

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -277,15 +277,13 @@ namespace ts {
277277
this.flags = 0;
278278
}
279279

280-
const idCache = new Map<Node, number>();
281280
export function getNodeId(node: Node): number {
282-
let result = idCache.get(node);
283-
if (!result) {
284-
result = nextNodeId;
285-
idCache.set(node, result);
281+
let id = node.id;
282+
if (!id) {
283+
node.id = id = nextNodeId;
286284
nextNodeId++;
287285
}
288-
return result;
286+
return id;
289287
}
290288

291289
export function getSymbolId(symbol: Symbol): SymbolId {
@@ -963,7 +961,7 @@ namespace ts {
963961
let flowInvocationCount = 0;
964962
let lastFlowNode: FlowNode | undefined;
965963
let lastFlowNodeReachable: boolean;
966-
let flowTypeCache: ESMap<Node, Type> | undefined;
964+
let flowTypeCache: Type[] | undefined;
967965

968966
const emptyStringType = getStringLiteralType("");
969967
const zeroType = getNumberLiteralType(0);
@@ -977,7 +975,7 @@ namespace ts {
977975
const maximumSuggestionCount = 10;
978976
const mergedSymbols: Symbol[] = [];
979977
const symbolLinks: SymbolLinks[] = [];
980-
const nodeLinks = new Map<Node, NodeLinks>();
978+
const nodeLinks: NodeLinks[] = [];
981979
const flowLoopCaches: ESMap<string, Type>[] = [];
982980
const flowLoopNodes: FlowNode[] = [];
983981
const flowLoopKeys: string[] = [];
@@ -1466,11 +1464,8 @@ namespace ts {
14661464
}
14671465

14681466
function getNodeLinks(node: Node): NodeLinks {
1469-
let result = nodeLinks.get(node);
1470-
if (!result) {
1471-
nodeLinks.set(node, result = new (NodeLinks as any)() as NodeLinks);
1472-
}
1473-
return result;
1467+
const nodeId = getNodeId(node);
1468+
return nodeLinks[nodeId] || (nodeLinks[nodeId] = new (NodeLinks as any)());
14741469
}
14751470

14761471
function isGlobalSourceFile(node: Node) {
@@ -33790,7 +33785,7 @@ namespace ts {
3379033785
}
3379133786
// If a type has been cached for the node, return it.
3379233787
if (node.flags & NodeFlags.TypeCached && flowTypeCache) {
33793-
const cachedType = flowTypeCache.get(node);
33788+
const cachedType = flowTypeCache[getNodeId(node)];
3379433789
if (cachedType) {
3379533790
return cachedType;
3379633791
}
@@ -33799,8 +33794,8 @@ namespace ts {
3379933794
const type = checkExpression(node);
3380033795
// If control flow analysis was required to determine the type, it is worth caching.
3380133796
if (flowInvocationCount !== startInvocationCount) {
33802-
const cache = (flowTypeCache ||= new Map<Node, Type>());
33803-
cache.set(node, type);
33797+
const cache = (flowTypeCache ||= []);
33798+
cache[getNodeId(node)] = type;
3380433799
setNodeFlags(node, node.flags | NodeFlags.TypeCached);
3380533800
}
3380633801
return type;
@@ -41603,7 +41598,9 @@ namespace ts {
4160341598
}
4160441599

4160541600
function getNodeCheckFlags(node: Node): NodeCheckFlags {
41606-
return nodeLinks.get(node)?.flags || 0;
41601+
const nodeId = node.id || 0;
41602+
if (nodeId < 0 || nodeId >= nodeLinks.length) return 0;
41603+
return nodeLinks[nodeId]?.flags || 0;
4160741604
}
4160841605

4160941606
function getEnumMemberValue(node: EnumMember): string | number | undefined {

src/compiler/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -857,6 +857,7 @@ namespace ts {
857857
/* @internal */ readonly transformFlags: TransformFlags; // Flags for transforms
858858
readonly decorators?: NodeArray<Decorator>; // Array of decorators (in document order)
859859
readonly modifiers?: ModifiersArray; // Array of modifiers
860+
/* @internal */ id?: NodeId; // Unique id (used to look up NodeLinks)
860861
readonly parent: Node; // Parent node (initialized by binding)
861862
/* @internal */ original?: Node; // The original node if this is an updated node.
862863
/* @internal */ symbol: Symbol; // Symbol declared by node (initialized by binding)

src/compiler/utilities.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5821,6 +5821,7 @@ namespace ts {
58215821
this.pos = pos;
58225822
this.end = end;
58235823
this.kind = kind;
5824+
this.id = 0;
58245825
this.flags = NodeFlags.None;
58255826
this.modifierFlagsCache = ModifierFlags.None;
58265827
this.transformFlags = TransformFlags.None;
@@ -5832,6 +5833,7 @@ namespace ts {
58325833
this.pos = pos;
58335834
this.end = end;
58345835
this.kind = kind;
5836+
this.id = 0;
58355837
this.flags = NodeFlags.None;
58365838
this.transformFlags = TransformFlags.None;
58375839
this.parent = undefined!;
@@ -5841,6 +5843,7 @@ namespace ts {
58415843
this.pos = pos;
58425844
this.end = end;
58435845
this.kind = kind;
5846+
this.id = 0;
58445847
this.flags = NodeFlags.None;
58455848
this.transformFlags = TransformFlags.None;
58465849
this.parent = undefined!;

0 commit comments

Comments
 (0)