Skip to content

Commit

Permalink
Merge pull request #2177 from Microsoft/intern
Browse files Browse the repository at this point in the history
Don't intern all strings and numbers.  Just the ones used as declaration names
  • Loading branch information
CyrusNajmabadi committed Mar 2, 2015
2 parents bd447f7 + e452cff commit 0de61cb
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 86 deletions.
82 changes: 52 additions & 30 deletions src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 0 additions & 3 deletions tests/baselines/reference/APISample_compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down
11 changes: 0 additions & 11 deletions tests/baselines/reference/APISample_compile.types
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions tests/baselines/reference/APISample_linter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down
11 changes: 0 additions & 11 deletions tests/baselines/reference/APISample_linter.types
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions tests/baselines/reference/APISample_transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down
11 changes: 0 additions & 11 deletions tests/baselines/reference/APISample_transform.types
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions tests/baselines/reference/APISample_watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
Expand Down
11 changes: 0 additions & 11 deletions tests/baselines/reference/APISample_watcher.types
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0de61cb

Please sign in to comment.