Skip to content

Use Map<true> for sets #16972

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
2 commits merged into from
Jul 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/compiler/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,8 +132,9 @@ namespace ts {
let inStrictMode: boolean;

let symbolCount = 0;

let Symbol: { new (flags: SymbolFlags, name: __String): Symbol };
let classifiableNames: UnderscoreEscapedMap<__String>;
let classifiableNames: UnderscoreEscapedMap<true>;

const unreachableFlow: FlowNode = { flags: FlowFlags.Unreachable };
const reportedUnreachableFlow: FlowNode = { flags: FlowFlags.Unreachable };
Expand All @@ -147,7 +148,7 @@ namespace ts {
options = opts;
languageVersion = getEmitScriptTarget(options);
inStrictMode = bindInStrictMode(file, opts);
classifiableNames = createUnderscoreEscapedMap<__String>();
classifiableNames = createUnderscoreEscapedMap<true>();
symbolCount = 0;
skipTransformFlagAggregation = file.isDeclarationFile;

Expand Down Expand Up @@ -349,7 +350,7 @@ namespace ts {
}

if (name && (includes & SymbolFlags.Classifiable)) {
classifiableNames.set(name, name);
classifiableNames.set(name, true);
}

if (symbol.flags & excludes) {
Expand Down Expand Up @@ -2445,7 +2446,7 @@ namespace ts {
bindAnonymousDeclaration(node, SymbolFlags.Class, bindingName);
// Add name of class expression into the map for semantic classifier
if (node.name) {
classifiableNames.set(node.name.text, node.name.text);
classifiableNames.set(node.name.text, true);
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/compiler/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1117,8 +1117,10 @@ namespace ts {
*
* @param array the array of input elements.
*/
export function arrayToSet<T>(array: T[], makeKey: (value: T) => string): Map<true> {
return arrayToMap<T, true>(array, makeKey, () => true);
export function arrayToSet(array: string[]): Map<true>;
export function arrayToSet<T>(array: T[], makeKey: (value: T) => string): Map<true>;
export function arrayToSet(array: any[], makeKey?: (value: any) => string): Map<true> {
return arrayToMap<any, true>(array, makeKey || (s => s), () => true);
}

export function cloneMap(map: SymbolTable): SymbolTable;
Expand Down
12 changes: 6 additions & 6 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ namespace ts {
let currentSourceFile: SourceFile | undefined;
let nodeIdToGeneratedName: string[]; // Map of generated names for specific nodes.
let autoGeneratedIdToGeneratedName: string[]; // Map of generated names for temp and loop variables.
let generatedNames: Map<string>; // Set of names generated by the NameGenerator.
let generatedNames: Map<true>; // Set of names generated by the NameGenerator.
let tempFlagsStack: TempFlags[]; // Stack of enclosing name generation scopes.
let tempFlags: TempFlags; // TempFlags for the current name generation scope.
let writer: EmitTextWriter;
Expand Down Expand Up @@ -399,7 +399,7 @@ namespace ts {
function reset() {
nodeIdToGeneratedName = [];
autoGeneratedIdToGeneratedName = [];
generatedNames = createMap<string>();
generatedNames = createMap<true>();
tempFlagsStack = [];
tempFlags = TempFlags.Auto;
comments.reset();
Expand Down Expand Up @@ -2228,7 +2228,7 @@ namespace ts {
* Emits any prologue directives at the start of a Statement list, returning the
* number of prologue directives written to the output.
*/
function emitPrologueDirectives(statements: Node[], startWithNewLine?: boolean, seenPrologueDirectives?: Map<String>): number {
function emitPrologueDirectives(statements: Node[], startWithNewLine?: boolean, seenPrologueDirectives?: Map<true>): number {
for (let i = 0; i < statements.length; i++) {
const statement = statements[i];
if (isPrologueDirective(statement)) {
Expand All @@ -2239,7 +2239,7 @@ namespace ts {
}
emit(statement);
if (seenPrologueDirectives) {
seenPrologueDirectives.set(statement.expression.text, statement.expression.text);
seenPrologueDirectives.set(statement.expression.text, true);
}
}
}
Expand All @@ -2258,7 +2258,7 @@ namespace ts {
emitPrologueDirectives((sourceFileOrBundle as SourceFile).statements);
}
else {
const seenPrologueDirectives = createMap<String>();
const seenPrologueDirectives = createMap<true>();
for (const sourceFile of (sourceFileOrBundle as Bundle).sourceFiles) {
setSourceFile(sourceFile);
emitPrologueDirectives(sourceFile.statements, /*startWithNewLine*/ true, seenPrologueDirectives);
Expand Down Expand Up @@ -2896,7 +2896,7 @@ namespace ts {
while (true) {
const generatedName = baseName + i;
if (isUniqueName(generatedName)) {
generatedNames.set(generatedName, generatedName);
generatedNames.set(generatedName, true);
return generatedName;
}
i++;
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ namespace ts {
let commonSourceDirectory: string;
let diagnosticsProducingTypeChecker: TypeChecker;
let noDiagnosticsTypeChecker: TypeChecker;
let classifiableNames: UnderscoreEscapedMap<__String>;
let classifiableNames: UnderscoreEscapedMap<true>;
let modifiedFilePaths: Path[] | undefined;

const cachedSemanticDiagnosticsForFile: DiagnosticCache = {};
Expand Down Expand Up @@ -580,7 +580,7 @@ namespace ts {
if (!classifiableNames) {
// Initialize a checker so that all our files are bound.
getTypeChecker();
classifiableNames = createUnderscoreEscapedMap<__String>();
classifiableNames = createUnderscoreEscapedMap<true>();

for (const sourceFile of files) {
copyEntries(sourceFile.classifiableNames, classifiableNames);
Expand Down
8 changes: 4 additions & 4 deletions src/compiler/transformers/es2015.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ namespace ts {
* set of labels that occurred inside the converted loop
* used to determine if labeled jump can be emitted as is or it should be dispatched to calling code
*/
labels?: Map<string>;
labels?: Map<boolean>;
/*
* collection of labeled jumps that transfer control outside the converted loop.
* maps store association 'label -> labelMarker' where
Expand Down Expand Up @@ -2236,16 +2236,16 @@ namespace ts {
}

function recordLabel(node: LabeledStatement) {
convertedLoopState.labels.set(unescapeLeadingUnderscores(node.label.text), unescapeLeadingUnderscores(node.label.text));
convertedLoopState.labels.set(unescapeLeadingUnderscores(node.label.text), true);
}

function resetLabel(node: LabeledStatement) {
convertedLoopState.labels.set(unescapeLeadingUnderscores(node.label.text), undefined);
convertedLoopState.labels.set(unescapeLeadingUnderscores(node.label.text), false);
}

function visitLabeledStatement(node: LabeledStatement): VisitResult<Statement> {
if (convertedLoopState && !convertedLoopState.labels) {
convertedLoopState.labels = createMap<string>();
convertedLoopState.labels = createMap<boolean>();
}
const statement = unwrapInnermostStatementOfLabel(node, convertedLoopState && recordLabel);
return isIterationStatement(statement, /*lookInLabeledStatements*/ false)
Expand Down
6 changes: 3 additions & 3 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2363,7 +2363,7 @@ namespace ts {
// The first node that causes this file to be a CommonJS module
/* @internal */ commonJsModuleIndicator: Node;

/* @internal */ identifiers: Map<string>;
/* @internal */ identifiers: Map<string>; // Map from a string to an interned string
/* @internal */ nodeCount: number;
/* @internal */ identifierCount: number;
/* @internal */ symbolCount: number;
Expand All @@ -2384,7 +2384,7 @@ namespace ts {
// Stores a line map for the file.
// This field should never be used directly to obtain line map, use getLineMap function instead.
/* @internal */ lineMap: number[];
/* @internal */ classifiableNames?: UnderscoreEscapedMap<__String>;
/* @internal */ classifiableNames?: UnderscoreEscapedMap<true>;
// Stores a mapping 'external module reference text' -> 'resolved file name' | undefined
// It is used to resolve module names in the checker.
// Content of this field should never be used directly - use getResolvedModuleFileName/setResolvedModuleFileName functions instead
Expand Down Expand Up @@ -2490,7 +2490,7 @@ namespace ts {
/* @internal */ getDiagnosticsProducingTypeChecker(): TypeChecker;
/* @internal */ dropDiagnosticsProducingTypeChecker(): void;

/* @internal */ getClassifiableNames(): UnderscoreEscapedMap<__String>;
/* @internal */ getClassifiableNames(): UnderscoreEscapedMap<true>;

/* @internal */ getNodeCount(): number;
/* @internal */ getIdentifierCount(): number;
Expand Down
12 changes: 6 additions & 6 deletions src/server/project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,11 @@ namespace ts.server {
/**
* Set of files names that were updated since the last call to getChangesSinceVersion.
*/
private updatedFileNames: Map<string>;
private updatedFileNames: Map<true>;
/**
* Set of files that was returned from the last call to getChangesSinceVersion.
*/
private lastReportedFileNames: Map<string>;
private lastReportedFileNames: Map<true>;
/**
* Last version that was reported.
*/
Expand Down Expand Up @@ -487,7 +487,7 @@ namespace ts.server {
}

registerFileUpdate(fileName: string) {
(this.updatedFileNames || (this.updatedFileNames = createMap<string>())).set(fileName, fileName);
(this.updatedFileNames || (this.updatedFileNames = createMap<true>())).set(fileName, true);
}

markAsDirty() {
Expand Down Expand Up @@ -611,7 +611,7 @@ namespace ts.server {
}

const missingFilePaths = this.program.getMissingFilePaths();
const missingFilePathsSet = arrayToSet(missingFilePaths, p => p);
const missingFilePathsSet = arrayToSet(missingFilePaths);

// Files that are no longer missing (e.g. because they are no longer required)
// should no longer be watched.
Expand Down Expand Up @@ -742,7 +742,7 @@ namespace ts.server {
}
// compute and return the difference
const lastReportedFileNames = this.lastReportedFileNames;
const currentFiles = arrayToMap(this.getFileNames(), x => x);
const currentFiles = arrayToSet(this.getFileNames());

const added: string[] = [];
const removed: string[] = [];
Expand All @@ -765,7 +765,7 @@ namespace ts.server {
else {
// unknown version - return everything
const projectFileNames = this.getFileNames();
this.lastReportedFileNames = arrayToMap(projectFileNames, x => x);
this.lastReportedFileNames = arrayToSet(projectFileNames);
this.lastReportedVersion = this.projectStructureVersion;
return { info, files: projectFileNames, projectErrors: this.getGlobalProjectErrors() };
}
Expand Down
6 changes: 3 additions & 3 deletions src/services/classifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ namespace ts {
}

/* @internal */
export function getSemanticClassifications(typeChecker: TypeChecker, cancellationToken: CancellationToken, sourceFile: SourceFile, classifiableNames: UnderscoreEscapedMap<__String>, span: TextSpan): ClassifiedSpan[] {
export function getSemanticClassifications(typeChecker: TypeChecker, cancellationToken: CancellationToken, sourceFile: SourceFile, classifiableNames: UnderscoreEscapedMap<true>, span: TextSpan): ClassifiedSpan[] {
return convertClassifications(getEncodedSemanticClassifications(typeChecker, cancellationToken, sourceFile, classifiableNames, span));
}

Expand All @@ -487,7 +487,7 @@ namespace ts {
}

/* @internal */
export function getEncodedSemanticClassifications(typeChecker: TypeChecker, cancellationToken: CancellationToken, sourceFile: SourceFile, classifiableNames: UnderscoreEscapedMap<__String>, span: TextSpan): Classifications {
export function getEncodedSemanticClassifications(typeChecker: TypeChecker, cancellationToken: CancellationToken, sourceFile: SourceFile, classifiableNames: UnderscoreEscapedMap<true>, span: TextSpan): Classifications {
const result: number[] = [];
processNode(sourceFile);

Expand Down Expand Up @@ -557,7 +557,7 @@ namespace ts {
// Only bother calling into the typechecker if this is an identifier that
// could possibly resolve to a type name. This makes classification run
// in a third of the time it would normally take.
if (classifiableNames.get(identifier.text)) {
if (classifiableNames.has(identifier.text)) {
const symbol = typeChecker.getSymbolAtLocation(node);
if (symbol) {
const type = classifySymbol(symbol, getMeaningFromLocation(node));
Expand Down
10 changes: 5 additions & 5 deletions src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ namespace ts.Completions {
return { isGlobalCompletion, isMemberCompletion, isNewIdentifierLocation, entries };
}

function getJavaScriptCompletionEntries(sourceFile: SourceFile, position: number, uniqueNames: Map<string>, target: ScriptTarget): CompletionEntry[] {
function getJavaScriptCompletionEntries(sourceFile: SourceFile, position: number, uniqueNames: Map<true>, target: ScriptTarget): CompletionEntry[] {
const entries: CompletionEntry[] = [];

const nameTable = getNameTable(sourceFile);
Expand All @@ -91,7 +91,7 @@ namespace ts.Completions {
const realName = unescapeLeadingUnderscores(name);

if (!uniqueNames.get(realName)) {
uniqueNames.set(realName, realName);
uniqueNames.set(realName, true);
const displayName = getCompletionEntryDisplayName(realName, target, /*performCharacterChecks*/ true);
if (displayName) {
const entry = {
Expand Down Expand Up @@ -133,17 +133,17 @@ namespace ts.Completions {
};
}

function getCompletionEntriesFromSymbols(symbols: Symbol[], entries: Push<CompletionEntry>, location: Node, performCharacterChecks: boolean, typeChecker: TypeChecker, target: ScriptTarget, log: Log): Map<string> {
function getCompletionEntriesFromSymbols(symbols: Symbol[], entries: Push<CompletionEntry>, location: Node, performCharacterChecks: boolean, typeChecker: TypeChecker, target: ScriptTarget, log: Log): Map<true> {
const start = timestamp();
const uniqueNames = createMap<string>();
const uniqueNames = createMap<true>();
if (symbols) {
for (const symbol of symbols) {
const entry = createCompletionEntry(symbol, location, performCharacterChecks, typeChecker, target);
if (entry) {
const id = entry.name;
if (!uniqueNames.get(id)) {
entries.push(entry);
uniqueNames.set(id, id);
uniqueNames.set(id, true);
}
}
}
Expand Down