Skip to content

Commit

Permalink
Use native maps when they're available
Browse files Browse the repository at this point in the history
  • Loading branch information
Andy Hanson committed Oct 4, 2016
1 parent 93b8df7 commit 7e05324
Show file tree
Hide file tree
Showing 66 changed files with 1,678 additions and 1,326 deletions.
2 changes: 2 additions & 0 deletions Jakefile.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ function measure(marker) {
}

var compilerSources = [
"dataStructures.ts",
"core.ts",
"performance.ts",
"sys.ts",
Expand Down Expand Up @@ -91,6 +92,7 @@ var compilerSources = [
});

var servicesSources = [
"dataStructures.ts",
"core.ts",
"performance.ts",
"sys.ts",
Expand Down
18 changes: 9 additions & 9 deletions scripts/processDiagnosticMessages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function main(): void {

var inputFilePath = sys.args[0].replace(/\\/g, "/");
var inputStr = sys.readFile(inputFilePath);

var diagnosticMessages: InputDiagnosticMessageTable = JSON.parse(inputStr);

var names = Utilities.getObjectKeys(diagnosticMessages);
Expand All @@ -44,7 +44,7 @@ function main(): void {
function checkForUniqueCodes(messages: string[], diagnosticTable: InputDiagnosticMessageTable) {
const originalMessageForCode: string[] = [];
let numConflicts = 0;

for (const currentMessage of messages) {
const code = diagnosticTable[currentMessage].code;

Expand All @@ -68,19 +68,19 @@ function checkForUniqueCodes(messages: string[], diagnosticTable: InputDiagnosti
}
}

function buildUniqueNameMap(names: string[]): ts.Map<string> {
var nameMap = ts.createMap<string>();
function buildUniqueNameMap(names: string[]): ts.Map<string, string> {
var nameMap = new ts.StringMap<string>();

var uniqueNames = NameGenerator.ensureUniqueness(names, /* isCaseSensitive */ false, /* isFixed */ undefined);

for (var i = 0; i < names.length; i++) {
nameMap[names[i]] = uniqueNames[i];
nameMap.set(names[i], uniqueNames[i]);
}

return nameMap;
}

function buildInfoFileOutput(messageTable: InputDiagnosticMessageTable, nameMap: ts.Map<string>): string {
function buildInfoFileOutput(messageTable: InputDiagnosticMessageTable, nameMap: ts.Map<string, string>): string {
var result =
'// <auto-generated />\r\n' +
'/// <reference path="types.ts" />\r\n' +
Expand All @@ -91,7 +91,7 @@ function buildInfoFileOutput(messageTable: InputDiagnosticMessageTable, nameMap:
for (var i = 0; i < names.length; i++) {
var name = names[i];
var diagnosticDetails = messageTable[name];
var propName = convertPropertyName(nameMap[name]);
var propName = convertPropertyName(nameMap.get(name));

result +=
' ' + propName +
Expand All @@ -107,14 +107,14 @@ function buildInfoFileOutput(messageTable: InputDiagnosticMessageTable, nameMap:
return result;
}

function buildDiagnosticMessageOutput(messageTable: InputDiagnosticMessageTable, nameMap: ts.Map<string>): string {
function buildDiagnosticMessageOutput(messageTable: InputDiagnosticMessageTable, nameMap: ts.Map<string, string>): string {
var result =
'{';
var names = Utilities.getObjectKeys(messageTable);
for (var i = 0; i < names.length; i++) {
var name = names[i];
var diagnosticDetails = messageTable[name];
var propName = convertPropertyName(nameMap[name]);
var propName = convertPropertyName(nameMap.get(name));

result += '\r\n "' + createKey(propName, diagnosticDetails.code) + '"' + ' : "' + name.replace(/[\"]/g, '\\"') + '"';
if (i !== names.length - 1) {
Expand Down
44 changes: 22 additions & 22 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ namespace ts {

let symbolCount = 0;
let Symbol: { new (flags: SymbolFlags, name: string): Symbol };
let classifiableNames: Map<string>;
let classifiableNames: Set<string>;

const unreachableFlow: FlowNode = { flags: FlowFlags.Unreachable };
const reportedUnreachableFlow: FlowNode = { flags: FlowFlags.Unreachable };
Expand All @@ -140,7 +140,7 @@ namespace ts {
options = opts;
languageVersion = getEmitScriptTarget(options);
inStrictMode = !!file.externalModuleIndicator;
classifiableNames = createMap<string>();
classifiableNames = new StringSet();
symbolCount = 0;
skipTransformFlagAggregation = isDeclarationFile(file);

Expand Down Expand Up @@ -190,11 +190,11 @@ namespace ts {
symbol.declarations.push(node);

if (symbolFlags & SymbolFlags.HasExports && !symbol.exports) {
symbol.exports = createMap<Symbol>();
symbol.exports = new StringMap<Symbol>();
}

if (symbolFlags & SymbolFlags.HasMembers && !symbol.members) {
symbol.members = createMap<Symbol>();
symbol.members = new StringMap<Symbol>();
}

if (symbolFlags & SymbolFlags.Value) {
Expand Down Expand Up @@ -332,17 +332,17 @@ namespace ts {
// Otherwise, we'll be merging into a compatible existing symbol (for example when
// you have multiple 'vars' with the same name in the same container). In this case
// just add this node into the declarations list of the symbol.
symbol = symbolTable[name] || (symbolTable[name] = createSymbol(SymbolFlags.None, name));
symbol = getOrUpdate(symbolTable, name, name => createSymbol(SymbolFlags.None, name));

if (name && (includes & SymbolFlags.Classifiable)) {
classifiableNames[name] = name;
classifiableNames.add(name);
}

if (symbol.flags & excludes) {
if (symbol.isReplaceableByMethod) {
// Javascript constructor-declared symbols can be discarded in favor of
// prototype symbols like methods.
symbol = symbolTable[name] = createSymbol(SymbolFlags.None, name);
symbol = setAndReturn(symbolTable, name, createSymbol(SymbolFlags.None, name));
}
else {
if (node.name) {
Expand Down Expand Up @@ -450,7 +450,7 @@ namespace ts {
if (containerFlags & ContainerFlags.IsContainer) {
container = blockScopeContainer = node;
if (containerFlags & ContainerFlags.HasLocals) {
container.locals = createMap<Symbol>();
container.locals = new StringMap<Symbol>();
}
addToContainerChain(container);
}
Expand Down Expand Up @@ -1447,8 +1447,7 @@ namespace ts {

const typeLiteralSymbol = createSymbol(SymbolFlags.TypeLiteral, "__type");
addDeclarationToSymbol(typeLiteralSymbol, node, SymbolFlags.TypeLiteral);
typeLiteralSymbol.members = createMap<Symbol>();
typeLiteralSymbol.members[symbol.name] = symbol;
typeLiteralSymbol.members = createMapWithEntry(symbol.name, symbol);
}

function bindObjectLiteralExpression(node: ObjectLiteralExpression) {
Expand All @@ -1458,7 +1457,7 @@ namespace ts {
}

if (inStrictMode) {
const seen = createMap<ElementKind>();
const seen = new StringMap<ElementKind>();

for (const prop of node.properties) {
if (prop.name.kind !== SyntaxKind.Identifier) {
Expand All @@ -1479,9 +1478,9 @@ namespace ts {
? ElementKind.Property
: ElementKind.Accessor;

const existingKind = seen[identifier.text];
const existingKind = seen.get(identifier.text);
if (!existingKind) {
seen[identifier.text] = currentKind;
seen.set(identifier.text, currentKind);
continue;
}

Expand Down Expand Up @@ -1514,7 +1513,7 @@ namespace ts {
// fall through.
default:
if (!blockScopeContainer.locals) {
blockScopeContainer.locals = createMap<Symbol>();
blockScopeContainer.locals = new StringMap<Symbol>();
addToContainerChain(blockScopeContainer);
}
declareSymbol(blockScopeContainer.locals, undefined, node, symbolFlags, symbolExcludes);
Expand Down Expand Up @@ -1976,7 +1975,7 @@ namespace ts {
}
}

file.symbol.globalExports = file.symbol.globalExports || createMap<Symbol>();
file.symbol.globalExports = file.symbol.globalExports || new StringMap<Symbol>();
declareSymbol(file.symbol.globalExports, file.symbol, node, SymbolFlags.Alias, SymbolFlags.AliasExcludes);
}

Expand Down Expand Up @@ -2021,7 +2020,7 @@ namespace ts {
Debug.assert(isInJavaScriptFile(node));
// Declare a 'member' if the container is an ES5 class or ES6 constructor
if (container.kind === SyntaxKind.FunctionDeclaration || container.kind === SyntaxKind.FunctionExpression) {
container.symbol.members = container.symbol.members || createMap<Symbol>();
container.symbol.members = container.symbol.members || new StringMap<Symbol>();
// It's acceptable for multiple 'this' assignments of the same identifier to occur
declareSymbol(container.symbol.members, container.symbol, node, SymbolFlags.Property, SymbolFlags.PropertyExcludes & ~SymbolFlags.Property);
}
Expand Down Expand Up @@ -2053,14 +2052,14 @@ namespace ts {
constructorFunction.parent = classPrototype;
classPrototype.parent = leftSideOfAssignment;

const funcSymbol = container.locals[constructorFunction.text];
const funcSymbol = container.locals.get(constructorFunction.text);
if (!funcSymbol || !(funcSymbol.flags & SymbolFlags.Function || isDeclarationOfFunctionExpression(funcSymbol))) {
return;
}

// Set up the members collection if it doesn't exist already
if (!funcSymbol.members) {
funcSymbol.members = createMap<Symbol>();
funcSymbol.members = new StringMap<Symbol>();
}

// Declare the method/property
Expand Down Expand Up @@ -2093,7 +2092,7 @@ namespace ts {
bindAnonymousDeclaration(node, SymbolFlags.Class, bindingName);
// Add name of class expression into the map for semantic classifier
if (node.name) {
classifiableNames[node.name.text] = node.name.text;
classifiableNames.add(node.name.text);
}
}

Expand All @@ -2109,14 +2108,15 @@ namespace ts {
// module might have an exported variable called 'prototype'. We can't allow that as
// that would clash with the built-in 'prototype' for the class.
const prototypeSymbol = createSymbol(SymbolFlags.Property | SymbolFlags.Prototype, "prototype");
if (symbol.exports[prototypeSymbol.name]) {
const symbolExport = symbol.exports.get(prototypeSymbol.name);
if (symbolExport) {
if (node.name) {
node.name.parent = node;
}
file.bindDiagnostics.push(createDiagnosticForNode(symbol.exports[prototypeSymbol.name].declarations[0],
file.bindDiagnostics.push(createDiagnosticForNode(symbolExport.declarations[0],
Diagnostics.Duplicate_identifier_0, prototypeSymbol.name));
}
symbol.exports[prototypeSymbol.name] = prototypeSymbol;
symbol.exports.set(prototypeSymbol.name, prototypeSymbol);
prototypeSymbol.parent = symbol;
}

Expand Down
Loading

0 comments on commit 7e05324

Please sign in to comment.