Skip to content

Commit f2eacc6

Browse files
committed
Use Maps to store visited types and symbols
1 parent 801c1f7 commit f2eacc6

File tree

2 files changed

+19
-14
lines changed

2 files changed

+19
-14
lines changed

src/compiler/symbolWalker.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,34 +13,36 @@ namespace ts {
1313
return getSymbolWalker;
1414

1515
function getSymbolWalker(accept: (symbol: Symbol) => boolean = () => true): SymbolWalker {
16-
let visitedTypes: Type[] = [];
17-
let visitedSymbols: Symbol[] = [];
16+
let visitedTypes = createMap<Type>(); // Key is id as string
17+
let visitedSymbols = createMap<Symbol>(); // Key is id as string
1818

1919
return {
2020
walkType: type =>
2121
{
22-
visitedTypes = [];
23-
visitedSymbols = [];
22+
visitedTypes.clear();
23+
visitedSymbols.clear();
2424
visitType(type);
25-
return { visitedTypes, visitedSymbols };
25+
return { visitedTypes: arrayFrom(visitedTypes.values()), visitedSymbols: arrayFrom(visitedSymbols.values()) };
2626
},
2727
walkSymbol: symbol =>
2828
{
29-
visitedTypes = [];
30-
visitedSymbols = [];
29+
visitedTypes.clear();
30+
visitedSymbols.clear();
3131
visitSymbol(symbol);
32-
return { visitedTypes, visitedSymbols };
32+
return { visitedTypes: arrayFrom(visitedTypes.values()), visitedSymbols: arrayFrom(visitedSymbols.values()) };
3333
},
3434
};
3535

3636
function visitType(type: Type): void {
3737
if (!type) {
3838
return;
3939
}
40-
if (contains(visitedTypes, type)) {
40+
41+
const typeIdString = type.id.toString();
42+
if (visitedTypes.has(typeIdString)) {
4143
return;
4244
}
43-
visitedTypes.push(type);
45+
visitedTypes.set(typeIdString, type);
4446

4547
// Reuse visitSymbol to visit the type's symbol,
4648
// but be sure to bail on recuring into the type if accept declines the symbol.
@@ -134,10 +136,11 @@ namespace ts {
134136
if (!symbol) {
135137
return;
136138
}
137-
if (contains(visitedSymbols, symbol)) {
139+
const symbolIdString = getSymbolId(symbol).toString();
140+
if (visitedSymbols.has(symbolIdString)) {
138141
return;
139142
}
140-
visitedSymbols.push(symbol);
143+
visitedSymbols.set(symbolIdString, symbol);
141144
if (!accept(symbol)) {
142145
return true;
143146
}

src/compiler/types.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2673,8 +2673,10 @@ namespace ts {
26732673

26742674
/* @internal */
26752675
export interface SymbolWalker {
2676-
walkType(root: Type) : { visitedTypes: Type[], visitedSymbols: Symbol[] };
2677-
walkSymbol(root: Symbol) : { visitedTypes: Type[], visitedSymbols: Symbol[] };
2676+
/** Note: Return values are not ordered. */
2677+
walkType(root: Type) : { visitedTypes: ReadonlyArray<Type>, visitedSymbols: ReadonlyArray<Symbol> };
2678+
/** Note: Return values are not ordered. */
2679+
walkSymbol(root: Symbol) : { visitedTypes: ReadonlyArray<Type>, visitedSymbols: ReadonlyArray<Symbol> };
26782680
}
26792681

26802682
export interface SymbolDisplayBuilder {

0 commit comments

Comments
 (0)