Skip to content

Commit 11c5c4e

Browse files
authored
Merge pull request #10359 from Microsoft/optimizeMoreMaps
Migrate more MapLikes to Maps
2 parents c4d1f15 + c014655 commit 11c5c4e

31 files changed

+433
-420
lines changed

src/compiler/checker.ts

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -284,33 +284,33 @@ namespace ts {
284284
NullFacts = TypeofEQObject | TypeofNEString | TypeofNENumber | TypeofNEBoolean | TypeofNESymbol | TypeofNEFunction | TypeofNEHostObject | EQNull | EQUndefinedOrNull | NEUndefined | Falsy,
285285
}
286286

287-
const typeofEQFacts: MapLike<TypeFacts> = {
287+
const typeofEQFacts = createMap({
288288
"string": TypeFacts.TypeofEQString,
289289
"number": TypeFacts.TypeofEQNumber,
290290
"boolean": TypeFacts.TypeofEQBoolean,
291291
"symbol": TypeFacts.TypeofEQSymbol,
292292
"undefined": TypeFacts.EQUndefined,
293293
"object": TypeFacts.TypeofEQObject,
294294
"function": TypeFacts.TypeofEQFunction
295-
};
295+
});
296296

297-
const typeofNEFacts: MapLike<TypeFacts> = {
297+
const typeofNEFacts = createMap({
298298
"string": TypeFacts.TypeofNEString,
299299
"number": TypeFacts.TypeofNENumber,
300300
"boolean": TypeFacts.TypeofNEBoolean,
301301
"symbol": TypeFacts.TypeofNESymbol,
302302
"undefined": TypeFacts.NEUndefined,
303303
"object": TypeFacts.TypeofNEObject,
304304
"function": TypeFacts.TypeofNEFunction
305-
};
305+
});
306306

307-
const typeofTypesByName: MapLike<Type> = {
307+
const typeofTypesByName = createMap<Type>({
308308
"string": stringType,
309309
"number": numberType,
310310
"boolean": booleanType,
311311
"symbol": esSymbolType,
312312
"undefined": undefinedType
313-
};
313+
});
314314

315315
let jsxElementType: ObjectType;
316316
/** Things we lazy load from the JSX namespace */
@@ -403,8 +403,8 @@ namespace ts {
403403
result.parent = symbol.parent;
404404
if (symbol.valueDeclaration) result.valueDeclaration = symbol.valueDeclaration;
405405
if (symbol.constEnumOnlyModule) result.constEnumOnlyModule = true;
406-
if (symbol.members) result.members = cloneSymbolTable(symbol.members);
407-
if (symbol.exports) result.exports = cloneSymbolTable(symbol.exports);
406+
if (symbol.members) result.members = cloneMap(symbol.members);
407+
if (symbol.exports) result.exports = cloneMap(symbol.exports);
408408
recordMergedSymbol(result, symbol);
409409
return result;
410410
}
@@ -447,14 +447,6 @@ namespace ts {
447447
}
448448
}
449449

450-
function cloneSymbolTable(symbolTable: SymbolTable): SymbolTable {
451-
const result = createMap<Symbol>();
452-
for (const id in symbolTable) {
453-
result[id] = symbolTable[id];
454-
}
455-
return result;
456-
}
457-
458450
function mergeSymbolTable(target: SymbolTable, source: SymbolTable) {
459451
for (const id in source) {
460452
let targetSymbol = target[id];
@@ -1450,7 +1442,7 @@ namespace ts {
14501442
return;
14511443
}
14521444
visitedSymbols.push(symbol);
1453-
const symbols = cloneSymbolTable(symbol.exports);
1445+
const symbols = cloneMap(symbol.exports);
14541446
// All export * declarations are collected in an __export symbol by the binder
14551447
const exportStars = symbol.exports["__export"];
14561448
if (exportStars) {
@@ -1655,12 +1647,12 @@ namespace ts {
16551647
}
16561648

16571649
// If symbol is directly available by its name in the symbol table
1658-
if (isAccessible(lookUp(symbols, symbol.name))) {
1650+
if (isAccessible(symbols[symbol.name])) {
16591651
return [symbol];
16601652
}
16611653

16621654
// Check if symbol is any of the alias
1663-
return forEachValue(symbols, symbolFromSymbolTable => {
1655+
return forEachProperty(symbols, symbolFromSymbolTable => {
16641656
if (symbolFromSymbolTable.flags & SymbolFlags.Alias
16651657
&& symbolFromSymbolTable.name !== "export="
16661658
&& !getDeclarationOfKind(symbolFromSymbolTable, SyntaxKind.ExportSpecifier)) {
@@ -6629,7 +6621,7 @@ namespace ts {
66296621
const maybeCache = maybeStack[depth];
66306622
// If result is definitely true, copy assumptions to global cache, else copy to next level up
66316623
const destinationCache = (result === Ternary.True || depth === 0) ? relation : maybeStack[depth - 1];
6632-
copyMap(maybeCache, destinationCache);
6624+
copyProperties(maybeCache, destinationCache);
66336625
}
66346626
else {
66356627
// A false result goes straight into global cache (when something is false under assumptions it
@@ -7941,7 +7933,7 @@ namespace ts {
79417933
// check. This gives us a quicker out in the common case where an object type is not a function.
79427934
const resolved = resolveStructuredTypeMembers(type);
79437935
return !!(resolved.callSignatures.length || resolved.constructSignatures.length ||
7944-
hasProperty(resolved.members, "bind") && isTypeSubtypeOf(type, globalFunctionType));
7936+
resolved.members["bind"] && isTypeSubtypeOf(type, globalFunctionType));
79457937
}
79467938

79477939
function getTypeFacts(type: Type): TypeFacts {
@@ -8517,14 +8509,14 @@ namespace ts {
85178509
// We narrow a non-union type to an exact primitive type if the non-union type
85188510
// is a supertype of that primitive type. For example, type 'any' can be narrowed
85198511
// to one of the primitive types.
8520-
const targetType = getProperty(typeofTypesByName, literal.text);
8512+
const targetType = typeofTypesByName[literal.text];
85218513
if (targetType && isTypeSubtypeOf(targetType, type)) {
85228514
return targetType;
85238515
}
85248516
}
85258517
const facts = assumeTrue ?
8526-
getProperty(typeofEQFacts, literal.text) || TypeFacts.TypeofEQHostObject :
8527-
getProperty(typeofNEFacts, literal.text) || TypeFacts.TypeofNEHostObject;
8518+
typeofEQFacts[literal.text] || TypeFacts.TypeofEQHostObject :
8519+
typeofNEFacts[literal.text] || TypeFacts.TypeofNEHostObject;
85288520
return getTypeWithFacts(type, facts);
85298521
}
85308522

@@ -18266,7 +18258,7 @@ namespace ts {
1826618258
// otherwise - check if at least one export is value
1826718259
symbolLinks.exportsSomeValue = hasExportAssignment
1826818260
? !!(moduleSymbol.flags & SymbolFlags.Value)
18269-
: forEachValue(getExportsOfModule(moduleSymbol), isValue);
18261+
: forEachProperty(getExportsOfModule(moduleSymbol), isValue);
1827018262
}
1827118263

1827218264
return symbolLinks.exportsSomeValue;

src/compiler/commandLineParser.ts

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ namespace ts {
6161
},
6262
{
6363
name: "jsx",
64-
type: {
64+
type: createMap({
6565
"preserve": JsxEmit.Preserve,
6666
"react": JsxEmit.React
67-
},
67+
}),
6868
paramType: Diagnostics.KIND,
6969
description: Diagnostics.Specify_JSX_code_generation_Colon_preserve_or_react,
7070
},
@@ -91,24 +91,24 @@ namespace ts {
9191
{
9292
name: "module",
9393
shortName: "m",
94-
type: {
94+
type: createMap({
9595
"none": ModuleKind.None,
9696
"commonjs": ModuleKind.CommonJS,
9797
"amd": ModuleKind.AMD,
9898
"system": ModuleKind.System,
9999
"umd": ModuleKind.UMD,
100100
"es6": ModuleKind.ES6,
101101
"es2015": ModuleKind.ES2015,
102-
},
102+
}),
103103
description: Diagnostics.Specify_module_code_generation_Colon_commonjs_amd_system_umd_or_es2015,
104104
paramType: Diagnostics.KIND,
105105
},
106106
{
107107
name: "newLine",
108-
type: {
108+
type: createMap({
109109
"crlf": NewLineKind.CarriageReturnLineFeed,
110110
"lf": NewLineKind.LineFeed
111-
},
111+
}),
112112
description: Diagnostics.Specify_the_end_of_line_sequence_to_be_used_when_emitting_files_Colon_CRLF_dos_or_LF_unix,
113113
paramType: Diagnostics.NEWLINE,
114114
},
@@ -250,12 +250,12 @@ namespace ts {
250250
{
251251
name: "target",
252252
shortName: "t",
253-
type: {
253+
type: createMap({
254254
"es3": ScriptTarget.ES3,
255255
"es5": ScriptTarget.ES5,
256256
"es6": ScriptTarget.ES6,
257257
"es2015": ScriptTarget.ES2015,
258-
},
258+
}),
259259
description: Diagnostics.Specify_ECMAScript_target_version_Colon_ES3_default_ES5_or_ES2015,
260260
paramType: Diagnostics.VERSION,
261261
},
@@ -284,10 +284,10 @@ namespace ts {
284284
},
285285
{
286286
name: "moduleResolution",
287-
type: {
287+
type: createMap({
288288
"node": ModuleResolutionKind.NodeJs,
289289
"classic": ModuleResolutionKind.Classic,
290-
},
290+
}),
291291
description: Diagnostics.Specify_module_resolution_strategy_Colon_node_Node_js_or_classic_TypeScript_pre_1_6,
292292
},
293293
{
@@ -392,7 +392,7 @@ namespace ts {
392392
type: "list",
393393
element: {
394394
name: "lib",
395-
type: {
395+
type: createMap({
396396
// JavaScript only
397397
"es5": "lib.es5.d.ts",
398398
"es6": "lib.es2015.d.ts",
@@ -417,7 +417,7 @@ namespace ts {
417417
"es2016.array.include": "lib.es2016.array.include.d.ts",
418418
"es2017.object": "lib.es2017.object.d.ts",
419419
"es2017.sharedmemory": "lib.es2017.sharedmemory.d.ts"
420-
},
420+
}),
421421
},
422422
description: Diagnostics.Specify_library_files_to_be_included_in_the_compilation_Colon
423423
},
@@ -486,18 +486,17 @@ namespace ts {
486486
/* @internal */
487487
export function createCompilerDiagnosticForInvalidCustomType(opt: CommandLineOptionOfCustomType): Diagnostic {
488488
const namesOfType: string[] = [];
489-
forEachKey(opt.type, key => {
489+
for (const key in opt.type) {
490490
namesOfType.push(` '${key}'`);
491-
});
492-
491+
}
493492
return createCompilerDiagnostic(Diagnostics.Argument_for_0_option_must_be_Colon_1, `--${opt.name}`, namesOfType);
494493
}
495494

496495
/* @internal */
497496
export function parseCustomTypeOption(opt: CommandLineOptionOfCustomType, value: string, errors: Diagnostic[]) {
498497
const key = trimString((value || "")).toLowerCase();
499498
const map = opt.type;
500-
if (hasProperty(map, key)) {
499+
if (key in map) {
501500
return map[key];
502501
}
503502
else {
@@ -551,11 +550,11 @@ namespace ts {
551550
s = s.slice(s.charCodeAt(1) === CharacterCodes.minus ? 2 : 1).toLowerCase();
552551

553552
// Try to translate short option names to their full equivalents.
554-
if (hasProperty(shortOptionNames, s)) {
553+
if (s in shortOptionNames) {
555554
s = shortOptionNames[s];
556555
}
557556

558-
if (hasProperty(optionNameMap, s)) {
557+
if (s in optionNameMap) {
559558
const opt = optionNameMap[s];
560559

561560
if (opt.isTSConfigOnly) {
@@ -811,7 +810,7 @@ namespace ts {
811810
const optionNameMap = arrayToMap(optionDeclarations, opt => opt.name);
812811

813812
for (const id in jsonOptions) {
814-
if (hasProperty(optionNameMap, id)) {
813+
if (id in optionNameMap) {
815814
const opt = optionNameMap[id];
816815
defaultOptions[opt.name] = convertJsonOption(opt, jsonOptions[id], basePath, errors);
817816
}
@@ -848,7 +847,7 @@ namespace ts {
848847

849848
function convertJsonOptionOfCustomType(opt: CommandLineOptionOfCustomType, value: string, errors: Diagnostic[]) {
850849
const key = value.toLowerCase();
851-
if (hasProperty(opt.type, key)) {
850+
if (key in opt.type) {
852851
return opt.type[key];
853852
}
854853
else {
@@ -1011,7 +1010,7 @@ namespace ts {
10111010
removeWildcardFilesWithLowerPriorityExtension(file, wildcardFileMap, supportedExtensions, keyMapper);
10121011

10131012
const key = keyMapper(file);
1014-
if (!hasProperty(literalFileMap, key) && !hasProperty(wildcardFileMap, key)) {
1013+
if (!(key in literalFileMap) && !(key in wildcardFileMap)) {
10151014
wildcardFileMap[key] = file;
10161015
}
10171016
}
@@ -1076,7 +1075,7 @@ namespace ts {
10761075
if (match) {
10771076
const key = useCaseSensitiveFileNames ? match[0] : match[0].toLowerCase();
10781077
const flags = watchRecursivePattern.test(name) ? WatchDirectoryFlags.Recursive : WatchDirectoryFlags.None;
1079-
const existingFlags = getProperty(wildcardDirectories, key);
1078+
const existingFlags = wildcardDirectories[key];
10801079
if (existingFlags === undefined || existingFlags < flags) {
10811080
wildcardDirectories[key] = flags;
10821081
if (flags === WatchDirectoryFlags.Recursive) {
@@ -1088,11 +1087,9 @@ namespace ts {
10881087

10891088
// Remove any subpaths under an existing recursively watched directory.
10901089
for (const key in wildcardDirectories) {
1091-
if (hasProperty(wildcardDirectories, key)) {
1092-
for (const recursiveKey of recursiveKeys) {
1093-
if (key !== recursiveKey && containsPath(recursiveKey, key, path, !useCaseSensitiveFileNames)) {
1094-
delete wildcardDirectories[key];
1095-
}
1090+
for (const recursiveKey of recursiveKeys) {
1091+
if (key !== recursiveKey && containsPath(recursiveKey, key, path, !useCaseSensitiveFileNames)) {
1092+
delete wildcardDirectories[key];
10961093
}
10971094
}
10981095
}
@@ -1115,7 +1112,7 @@ namespace ts {
11151112
for (let i = ExtensionPriority.Highest; i < adjustedExtensionPriority; i++) {
11161113
const higherPriorityExtension = extensions[i];
11171114
const higherPriorityPath = keyMapper(changeExtension(file, higherPriorityExtension));
1118-
if (hasProperty(literalFiles, higherPriorityPath) || hasProperty(wildcardFiles, higherPriorityPath)) {
1115+
if (higherPriorityPath in literalFiles || higherPriorityPath in wildcardFiles) {
11191116
return true;
11201117
}
11211118
}

0 commit comments

Comments
 (0)