From 38905f46ce328b24b581399d7f364edbd834be76 Mon Sep 17 00:00:00 2001 From: Andy Date: Fri, 22 Sep 2017 14:07:07 -0700 Subject: [PATCH] Minor cleanup to symbolWalker (#18549) * Minor cleanup to symbolWalker * Undo inlining --- src/compiler/core.ts | 16 ++++++---- src/compiler/symbolWalker.ts | 58 +++++++++++++++++------------------- 2 files changed, 39 insertions(+), 35 deletions(-) diff --git a/src/compiler/core.ts b/src/compiler/core.ts index 84321b4b624..ffa010c6551 100644 --- a/src/compiler/core.ts +++ b/src/compiler/core.ts @@ -994,11 +994,6 @@ namespace ts { /** * Gets the owned, enumerable property keys of a map-like. - * - * NOTE: This is intended for use with MapLike objects. For Map objects, use - * Object.keys instead as it offers better performance. - * - * @param map A map-like. */ export function getOwnKeys(map: MapLike): string[] { const keys: string[] = []; @@ -1011,6 +1006,17 @@ namespace ts { return keys; } + export function getOwnValues(sparseArray: T[]): T[] { + const values: T[] = []; + for (const key in sparseArray) { + if (hasOwnProperty.call(sparseArray, key)) { + values.push(sparseArray[key]); + } + } + + return values; + } + /** Shims `Array.from`. */ export function arrayFrom(iterator: Iterator, map: (t: T) => U): U[]; export function arrayFrom(iterator: Iterator): T[]; diff --git a/src/compiler/symbolWalker.ts b/src/compiler/symbolWalker.ts index e20ef9f9d98..ac6b60bdc3e 100644 --- a/src/compiler/symbolWalker.ts +++ b/src/compiler/symbolWalker.ts @@ -14,21 +14,29 @@ namespace ts { return getSymbolWalker; function getSymbolWalker(accept: (symbol: Symbol) => boolean = () => true): SymbolWalker { - const visitedTypes = createMap(); // Key is id as string - const visitedSymbols = createMap(); // Key is id as string + const visitedTypes: Type[] = []; // Sparse array from id to type + const visitedSymbols: Symbol[] = []; // Sparse array from id to symbol return { walkType: type => { - visitedTypes.clear(); - visitedSymbols.clear(); - visitType(type); - return { visitedTypes: arrayFrom(visitedTypes.values()), visitedSymbols: arrayFrom(visitedSymbols.values()) }; + try { + visitType(type); + return { visitedTypes: getOwnValues(visitedTypes), visitedSymbols: getOwnValues(visitedSymbols) }; + } + finally { + clear(visitedTypes); + clear(visitedSymbols); + } }, walkSymbol: symbol => { - visitedTypes.clear(); - visitedSymbols.clear(); - visitSymbol(symbol); - return { visitedTypes: arrayFrom(visitedTypes.values()), visitedSymbols: arrayFrom(visitedSymbols.values()) }; + try { + visitSymbol(symbol); + return { visitedTypes: getOwnValues(visitedTypes), visitedSymbols: getOwnValues(visitedSymbols) }; + } + finally { + clear(visitedTypes); + clear(visitedSymbols); + } }, }; @@ -37,11 +45,10 @@ namespace ts { return; } - const typeIdString = type.id.toString(); - if (visitedTypes.has(typeIdString)) { + if (visitedTypes[type.id]) { return; } - visitedTypes.set(typeIdString, type); + visitedTypes[type.id] = type; // Reuse visitSymbol to visit the type's symbol, // but be sure to bail on recuring into the type if accept declines the symbol. @@ -79,18 +86,9 @@ namespace ts { } } - function visitTypeList(types: Type[]): void { - if (!types) { - return; - } - for (let i = 0; i < types.length; i++) { - visitType(types[i]); - } - } - function visitTypeReference(type: TypeReference): void { visitType(type.target); - visitTypeList(type.typeArguments); + forEach(type.typeArguments, visitType); } function visitTypeParameter(type: TypeParameter): void { @@ -98,7 +96,7 @@ namespace ts { } function visitUnionOrIntersectionType(type: UnionOrIntersectionType): void { - visitTypeList(type.types); + forEach(type.types, visitType); } function visitIndexType(type: IndexType): void { @@ -122,7 +120,7 @@ namespace ts { if (signature.typePredicate) { visitType(signature.typePredicate.type); } - visitTypeList(signature.typeParameters); + forEach(signature.typeParameters, visitType); for (const parameter of signature.parameters){ visitSymbol(parameter); @@ -133,8 +131,8 @@ namespace ts { function visitInterfaceType(interfaceT: InterfaceType): void { visitObjectType(interfaceT); - visitTypeList(interfaceT.typeParameters); - visitTypeList(getBaseTypes(interfaceT)); + forEach(interfaceT.typeParameters, visitType); + forEach(getBaseTypes(interfaceT), visitType); visitType(interfaceT.thisType); } @@ -161,11 +159,11 @@ namespace ts { if (!symbol) { return; } - const symbolIdString = getSymbolId(symbol).toString(); - if (visitedSymbols.has(symbolIdString)) { + const symbolId = getSymbolId(symbol); + if (visitedSymbols[symbolId]) { return; } - visitedSymbols.set(symbolIdString, symbol); + visitedSymbols[symbolId] = symbol; if (!accept(symbol)) { return true; }