From ebcdd85ad06503b3776135f6c50e56c7d0f8a27d Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Thu, 21 May 2015 09:18:55 -0700 Subject: [PATCH] Changing type mapping cache to be a dictionary --- src/compiler/checker.ts | 11 +++++------ src/compiler/types.ts | 7 +------ 2 files changed, 6 insertions(+), 12 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index e49deba6032..528ee97602b 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -3869,14 +3869,13 @@ module ts { // If this type has already been instantiated using this mapper, returned the cached result. This guards against // infinite instantiations of cyclic types, e.g. "var x: { a: T, b: typeof x };" if (mapper.mappings) { - for (let mapping of mapper.mappings) { - if (mapping.type === type) { - return mapping.result; - } + let cached = mapper.mappings[type.id]; + if (cached) { + return cached; } } else { - mapper.mappings = []; + mapper.mappings = {}; } // Instantiate the given type using the given mapper and cache the result let result = createObjectType(TypeFlags.Anonymous, type.symbol); @@ -3888,7 +3887,7 @@ module ts { let numberIndexType = getIndexTypeOfType(type, IndexKind.Number); if (stringIndexType) result.stringIndexType = instantiateType(stringIndexType, mapper); if (numberIndexType) result.numberIndexType = instantiateType(numberIndexType, mapper); - mapper.mappings.push({ type, result }); + mapper.mappings[type.id] = result; return result; } diff --git a/src/compiler/types.ts b/src/compiler/types.ts index 0f810c9fe61..c8d22b26728 100644 --- a/src/compiler/types.ts +++ b/src/compiler/types.ts @@ -1586,15 +1586,10 @@ module ts { Number, } - export interface TypeMapping { - type: Type; - result: Type; - } - /* @internal */ export interface TypeMapper { (t: TypeParameter): Type; - mappings?: TypeMapping[]; // Type mapping cache + mappings?: Map; // Type mapping cache } /* @internal */