Merge pull request #2177 from Microsoft/intern

Don't intern all strings and numbers.  Just the ones used as declaration names
This commit is contained in:
CyrusNajmabadi 2015-03-02 14:36:17 -08:00
commit 0de61cbecc
9 changed files with 52 additions and 86 deletions

View File

@ -58,9 +58,10 @@ module ts {
}
export interface SourceFile {
version: string;
scriptSnapshot: IScriptSnapshot;
nameTable: Map<string>;
/* @internal */ version: string;
/* @internal */ scriptSnapshot: IScriptSnapshot;
/* @internal */ nameTable: Map<string>;
getNamedDeclarations(): Declaration[];
getLineAndCharacterOfPosition(pos: number): LineAndCharacter;
getLineStarts(): number[];
@ -4138,27 +4139,6 @@ module ts {
return getReferencesForNode(node, program.getSourceFiles(), /*searchOnlyInCurrentFile*/ false, findInStrings, findInComments);
}
function initializeNameTable(sourceFile: SourceFile): void {
var nameTable: Map<string> = {};
walk(sourceFile);
sourceFile.nameTable = nameTable;
function walk(node: Node) {
switch (node.kind) {
case SyntaxKind.Identifier:
nameTable[(<Identifier>node).text] = (<Identifier>node).text;
break;
case SyntaxKind.StringLiteral:
case SyntaxKind.NumericLiteral:
nameTable[(<LiteralExpression>node).text] = (<LiteralExpression>node).text;
break;
default:
forEachChild(node, walk);
}
}
}
function getReferencesForNode(node: Node, sourceFiles: SourceFile[], searchOnlyInCurrentFile: boolean, findInStrings: boolean, findInComments: boolean): ReferenceEntry[] {
// Labels
if (isLabelName(node)) {
@ -4225,13 +4205,9 @@ module ts {
forEach(sourceFiles, sourceFile => {
cancellationToken.throwIfCancellationRequested();
if (!sourceFile.nameTable) {
initializeNameTable(sourceFile)
}
var nameTable = getNameTable(sourceFile);
Debug.assert(sourceFile.nameTable !== undefined);
if (lookUp(sourceFile.nameTable, internedName)) {
if (lookUp(nameTable, internedName)) {
result = result || [];
getReferencesInNode(sourceFile, symbol, declaredName, node, searchMeaning, findInStrings, findInComments, result);
}
@ -5775,6 +5751,52 @@ module ts {
};
}
/* @internal */
export function getNameTable(sourceFile: SourceFile): Map<string> {
if (!sourceFile.nameTable) {
initializeNameTable(sourceFile)
}
return sourceFile.nameTable;
}
function initializeNameTable(sourceFile: SourceFile): void {
var nameTable: Map<string> = {};
walk(sourceFile);
sourceFile.nameTable = nameTable;
function walk(node: Node) {
switch (node.kind) {
case SyntaxKind.Identifier:
nameTable[(<Identifier>node).text] = (<Identifier>node).text;
break;
case SyntaxKind.StringLiteral:
case SyntaxKind.NumericLiteral:
// We want to store any numbers/strings if they were a name that could be
// related to a declaration. So, if we have 'import x = require("something")'
// then we want 'something' to be in the name table. Similarly, if we have
// "a['propname']" then we want to store "propname" in the name table.
if (isDeclarationName(node) ||
node.parent.kind === SyntaxKind.ExternalModuleReference ||
isArgumentOfElementAccessExpression(node)) {
nameTable[(<LiteralExpression>node).text] = (<LiteralExpression>node).text;
}
break;
default:
forEachChild(node, walk);
}
}
}
function isArgumentOfElementAccessExpression(node: Node) {
return node &&
node.parent &&
node.parent.kind === SyntaxKind.ElementAccessExpression &&
(<ElementAccessExpression>node.parent).argumentExpression === node;
}
/// Classifier
export function createClassifier(): Classifier {
var scanner = createScanner(ScriptTarget.Latest, /*skipTrivia*/ false);

View File

@ -1518,9 +1518,6 @@ declare module "typescript" {
getDocumentationComment(): SymbolDisplayPart[];
}
interface SourceFile {
version: string;
scriptSnapshot: IScriptSnapshot;
nameTable: Map<string>;
getNamedDeclarations(): Declaration[];
getLineAndCharacterOfPosition(pos: number): LineAndCharacter;
getLineStarts(): number[];

View File

@ -4896,17 +4896,6 @@ declare module "typescript" {
interface SourceFile {
>SourceFile : SourceFile
version: string;
>version : string
scriptSnapshot: IScriptSnapshot;
>scriptSnapshot : IScriptSnapshot
>IScriptSnapshot : IScriptSnapshot
nameTable: Map<string>;
>nameTable : Map<string>
>Map : Map<T>
getNamedDeclarations(): Declaration[];
>getNamedDeclarations : () => Declaration[]
>Declaration : Declaration

View File

@ -1549,9 +1549,6 @@ declare module "typescript" {
getDocumentationComment(): SymbolDisplayPart[];
}
interface SourceFile {
version: string;
scriptSnapshot: IScriptSnapshot;
nameTable: Map<string>;
getNamedDeclarations(): Declaration[];
getLineAndCharacterOfPosition(pos: number): LineAndCharacter;
getLineStarts(): number[];

View File

@ -5042,17 +5042,6 @@ declare module "typescript" {
interface SourceFile {
>SourceFile : SourceFile
version: string;
>version : string
scriptSnapshot: IScriptSnapshot;
>scriptSnapshot : IScriptSnapshot
>IScriptSnapshot : IScriptSnapshot
nameTable: Map<string>;
>nameTable : Map<string>
>Map : Map<T>
getNamedDeclarations(): Declaration[];
>getNamedDeclarations : () => Declaration[]
>Declaration : Declaration

View File

@ -1550,9 +1550,6 @@ declare module "typescript" {
getDocumentationComment(): SymbolDisplayPart[];
}
interface SourceFile {
version: string;
scriptSnapshot: IScriptSnapshot;
nameTable: Map<string>;
getNamedDeclarations(): Declaration[];
getLineAndCharacterOfPosition(pos: number): LineAndCharacter;
getLineStarts(): number[];

View File

@ -4992,17 +4992,6 @@ declare module "typescript" {
interface SourceFile {
>SourceFile : SourceFile
version: string;
>version : string
scriptSnapshot: IScriptSnapshot;
>scriptSnapshot : IScriptSnapshot
>IScriptSnapshot : IScriptSnapshot
nameTable: Map<string>;
>nameTable : Map<string>
>Map : Map<T>
getNamedDeclarations(): Declaration[];
>getNamedDeclarations : () => Declaration[]
>Declaration : Declaration

View File

@ -1587,9 +1587,6 @@ declare module "typescript" {
getDocumentationComment(): SymbolDisplayPart[];
}
interface SourceFile {
version: string;
scriptSnapshot: IScriptSnapshot;
nameTable: Map<string>;
getNamedDeclarations(): Declaration[];
getLineAndCharacterOfPosition(pos: number): LineAndCharacter;
getLineStarts(): number[];

View File

@ -5165,17 +5165,6 @@ declare module "typescript" {
interface SourceFile {
>SourceFile : SourceFile
version: string;
>version : string
scriptSnapshot: IScriptSnapshot;
>scriptSnapshot : IScriptSnapshot
>IScriptSnapshot : IScriptSnapshot
nameTable: Map<string>;
>nameTable : Map<string>
>Map : Map<T>
getNamedDeclarations(): Declaration[];
>getNamedDeclarations : () => Declaration[]
>Declaration : Declaration