Skip to content

Commit 1b7a67e

Browse files
committed
Merge pull request microsoft#8873 from Microsoft/transforms-visitEachChildPerf
[Transforms] Optimize frequent paths in `visitEachChild`.
2 parents 7783cb9 + 5fbb326 commit 1b7a67e

13 files changed

Lines changed: 902 additions & 178 deletions

File tree

src/compiler/comments.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ namespace ts {
296296
},
297297
emitTrailingComments(range: TextRange, comments: CommentRange[]): void {
298298
const commentStart = performance.mark();
299-
emitLeadingComments(range, comments);
299+
emitTrailingComments(range, comments);
300300
performance.measure("commentTime", commentStart);
301301
},
302302
emitLeadingDetachedComments(range: TextRange, contextNode?: Node, ignoreNodeCallback?: (contextNode: Node) => boolean): void {
@@ -324,8 +324,8 @@ namespace ts {
324324

325325
function setSourceFile(sourceFile: SourceFile) {
326326
currentSourceFile = sourceFile;
327-
currentText = sourceFile.text;
328-
currentLineMap = getLineStarts(sourceFile);
327+
currentText = currentSourceFile.text;
328+
currentLineMap = getLineStarts(currentSourceFile);
329329
detachedCommentsInfo = undefined;
330330
consumedCommentRanges = {};
331331
leadingCommentRangePositions = {};

src/compiler/factory.ts

Lines changed: 326 additions & 77 deletions
Large diffs are not rendered by default.

src/compiler/sourcemap.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -508,10 +508,6 @@ namespace ts {
508508
return skipTrivia(currentSourceText, rangeHasDecorators ? (range as Node).decorators.end : range.pos);
509509
}
510510

511-
function getStartPos(range: TextRange) {
512-
return skipTrivia(currentSourceText, range.pos);
513-
}
514-
515511
/**
516512
* Emits a mapping for the start of a range.
517513
*
@@ -701,7 +697,7 @@ namespace ts {
701697
*/
702698
function setSourceFile(sourceFile: SourceFile) {
703699
currentSourceFile = sourceFile;
704-
currentSourceText = sourceFile.text;
700+
currentSourceText = currentSourceFile.text;
705701

706702
// Add the file to tsFilePaths
707703
// If sourceroot option: Use the relative path corresponding to the common directory path
@@ -720,10 +716,10 @@ namespace ts {
720716
sourceMapData.sourceMapSources.push(source);
721717

722718
// The one that can be used from program to get the actual source file
723-
sourceMapData.inputSourceFileNames.push(sourceFile.fileName);
719+
sourceMapData.inputSourceFileNames.push(currentSourceFile.fileName);
724720

725721
if (compilerOptions.inlineSources) {
726-
sourceMapData.sourceMapSourcesContent.push(sourceFile.text);
722+
sourceMapData.sourceMapSourcesContent.push(currentSourceFile.text);
727723
}
728724
}
729725
}

src/compiler/transformer.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,9 @@ namespace ts {
156156
* @param transforms An array of Transformers.
157157
*/
158158
export function transformFiles(resolver: EmitResolver, host: EmitHost, sourceFiles: SourceFile[], transformers: Transformer[]) {
159-
const transformId = nextTransformId++;
159+
const transformId = nextTransformId;
160+
nextTransformId++;
161+
160162
const tokenSourceMapRanges: Map<TextRange> = { };
161163
const lexicalEnvironmentVariableDeclarationsStack: VariableDeclaration[][] = [];
162164
const lexicalEnvironmentFunctionDeclarationsStack: FunctionDeclaration[][] = [];

src/compiler/transformers/destructuring.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ namespace ts {
9898
return declarations;
9999

100100
function emitAssignment(name: Identifier, value: Expression, location: TextRange) {
101-
const declaration = createVariableDeclaration(name, value, location);
101+
const declaration = createVariableDeclaration(name, /*type*/ undefined, value, location);
102102

103103
// NOTE: this completely disables source maps, but aligns with the behavior of
104104
// `emitAssignment` in the old emitter.
@@ -134,7 +134,7 @@ namespace ts {
134134
return declarations;
135135

136136
function emitAssignment(name: Identifier, value: Expression, location: TextRange, original: Node) {
137-
const declaration = createVariableDeclaration(name, value, location);
137+
const declaration = createVariableDeclaration(name, /*type*/ undefined, value, location);
138138
declaration.original = original;
139139

140140
// NOTE: this completely disables source maps, but aligns with the behavior of

src/compiler/transformers/es6.ts

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -554,6 +554,7 @@ namespace ts {
554554
createVariableDeclarationList([
555555
createVariableDeclaration(
556556
getDeclarationName(node, /*allowComments*/ true),
557+
/*type*/ undefined,
557558
transformClassLikeDeclarationToExpression(node)
558559
)
559560
]),
@@ -621,7 +622,9 @@ namespace ts {
621622
const classFunction = createFunctionExpression(
622623
/*asteriskToken*/ undefined,
623624
/*name*/ undefined,
625+
/*typeParameters*/ undefined,
624626
extendsClauseElement ? [createParameter("_super")] : [],
627+
/*type*/ undefined,
625628
transformClassBody(node, extendsClauseElement)
626629
);
627630

@@ -645,6 +648,7 @@ namespace ts {
645648
return createParen(
646649
createCall(
647650
outer,
651+
/*typeArguments*/ undefined,
648652
extendsClauseElement
649653
? [visitNode(extendsClauseElement.expression, visitor, isExpression)]
650654
: []
@@ -717,10 +721,13 @@ namespace ts {
717721
const hasSynthesizedSuper = hasSynthesizedDefaultSuperCall(constructor, extendsClauseElement !== undefined);
718722
statements.push(
719723
createFunctionDeclaration(
724+
/*decorators*/ undefined,
720725
/*modifiers*/ undefined,
721726
/*asteriskToken*/ undefined,
722727
getDeclarationName(node),
728+
/*typeParameters*/ undefined,
723729
transformConstructorParameters(constructor, hasSynthesizedSuper),
730+
/*type*/ undefined,
724731
transformConstructorBody(constructor, node, extendsClauseElement, hasSynthesizedSuper),
725732
/*location*/ constructor || node
726733
)
@@ -967,10 +974,9 @@ namespace ts {
967974
NodeEmitFlags.SingleLine | NodeEmitFlags.NoTrailingSourceMap | NodeEmitFlags.NoTokenSourceMaps
968975
),
969976
/*elseStatement*/ undefined,
970-
/*location*/ parameter,
971-
{ startOnNewLine: true }
977+
/*location*/ parameter
972978
);
973-
979+
statement.startsOnNewLine = true;
974980
setNodeEmitFlags(statement, NodeEmitFlags.NoTokenSourceMaps | NodeEmitFlags.NoTrailingSourceMap);
975981
statements.push(statement);
976982
}
@@ -1018,6 +1024,7 @@ namespace ts {
10181024
createVariableDeclarationList([
10191025
createVariableDeclaration(
10201026
declarationName,
1027+
/*type*/ undefined,
10211028
createArrayLiteral([])
10221029
)
10231030
]),
@@ -1030,7 +1037,7 @@ namespace ts {
10301037
// }
10311038
const forStatement = createFor(
10321039
createVariableDeclarationList([
1033-
createVariableDeclaration(temp, createLiteral(restIndex))
1040+
createVariableDeclaration(temp, /*type*/ undefined, createLiteral(restIndex))
10341041
], /*location*/ parameter),
10351042
createLessThan(
10361043
temp,
@@ -1073,6 +1080,7 @@ namespace ts {
10731080
createVariableDeclarationList([
10741081
createVariableDeclaration(
10751082
"_this",
1083+
/*type*/ undefined,
10761084
createThis()
10771085
)
10781086
])
@@ -1228,6 +1236,7 @@ namespace ts {
12281236

12291237
return createCall(
12301238
createPropertyAccess(createIdentifier("Object"), "defineProperty"),
1239+
/*typeArguments*/ undefined,
12311240
[
12321241
target,
12331242
propertyName,
@@ -1271,15 +1280,19 @@ namespace ts {
12711280
* @param node a FunctionDeclaration node.
12721281
*/
12731282
function visitFunctionDeclaration(node: FunctionDeclaration): FunctionDeclaration {
1274-
return createFunctionDeclaration(
1275-
/*modifiers*/ undefined,
1276-
node.asteriskToken,
1277-
node.name,
1278-
visitNodes(node.parameters, visitor, isParameter),
1279-
transformFunctionBody(node),
1280-
/*location*/ node,
1281-
/*original*/ node
1282-
);
1283+
return setOriginalNode(
1284+
createFunctionDeclaration(
1285+
/*decorators*/ undefined,
1286+
/*modifiers*/ undefined,
1287+
node.asteriskToken,
1288+
node.name,
1289+
/*typeParameters*/ undefined,
1290+
visitNodes(node.parameters, visitor, isParameter),
1291+
/*type*/ undefined,
1292+
transformFunctionBody(node),
1293+
/*location*/ node
1294+
),
1295+
/*original*/ node);
12831296
}
12841297

12851298
/**
@@ -1295,12 +1308,16 @@ namespace ts {
12951308
containingNonArrowFunction = node;
12961309
}
12971310

1298-
const expression = createFunctionExpression(
1299-
node.asteriskToken,
1300-
name,
1301-
visitNodes(node.parameters, visitor, isParameter),
1302-
saveStateAndInvoke(node, transformFunctionBody),
1303-
location,
1311+
const expression = setOriginalNode(
1312+
createFunctionExpression(
1313+
node.asteriskToken,
1314+
name,
1315+
/*typeParameters*/ undefined,
1316+
visitNodes(node.parameters, visitor, isParameter),
1317+
/*type*/ undefined,
1318+
saveStateAndInvoke(node, transformFunctionBody),
1319+
location
1320+
),
13041321
/*original*/ node
13051322
);
13061323

@@ -1755,6 +1772,7 @@ namespace ts {
17551772
createVariableDeclarationList([
17561773
createVariableDeclaration(
17571774
firstOriginalDeclaration ? firstOriginalDeclaration.name : createTempVariable(/*recordTempVariable*/ undefined),
1775+
/*type*/ undefined,
17581776
createElementAccess(rhsReference, counter)
17591777
)
17601778
], /*location*/ moveRangePos(initializer, -1)),
@@ -1818,8 +1836,8 @@ namespace ts {
18181836

18191837
const forStatement = createFor(
18201838
createVariableDeclarationList([
1821-
createVariableDeclaration(counter, createLiteral(0), /*location*/ moveRangePos(node.expression, -1)),
1822-
createVariableDeclaration(rhsReference, expression, /*location*/ node.expression)
1839+
createVariableDeclaration(counter, /*type*/ undefined, createLiteral(0), /*location*/ moveRangePos(node.expression, -1)),
1840+
createVariableDeclaration(rhsReference, /*type*/ undefined, expression, /*location*/ node.expression)
18231841
], /*location*/ node.expression),
18241842
createLessThan(
18251843
counter,
@@ -1996,11 +2014,14 @@ namespace ts {
19962014
[
19972015
createVariableDeclaration(
19982016
functionName,
2017+
/*type*/ undefined,
19992018
setNodeEmitFlags(
20002019
createFunctionExpression(
20012020
/*asteriskToken*/ undefined,
20022021
/*name*/ undefined,
2022+
/*typeParameters*/ undefined,
20032023
loopParameters,
2024+
/*type*/ undefined,
20042025
<Block>loopBody
20052026
),
20062027
currentState.containsLexicalThis
@@ -2027,6 +2048,7 @@ namespace ts {
20272048
(extraVariableDeclarations || (extraVariableDeclarations = [])).push(
20282049
createVariableDeclaration(
20292050
currentState.argumentsName,
2051+
/*type*/ undefined,
20302052
createIdentifier("arguments")
20312053
)
20322054
);
@@ -2047,8 +2069,9 @@ namespace ts {
20472069
(extraVariableDeclarations || (extraVariableDeclarations = [])).push(
20482070
createVariableDeclaration(
20492071
currentState.thisName,
2072+
/*type*/ undefined,
20502073
createIdentifier("this")
2051-
)
2074+
)
20522075
);
20532076
}
20542077
}
@@ -2140,7 +2163,7 @@ namespace ts {
21402163
!state.labeledNonLocalBreaks &&
21412164
!state.labeledNonLocalContinues;
21422165

2143-
const call = createCall(loopFunctionExpressionName, map(parameters, p => <Identifier>p.name));
2166+
const call = createCall(loopFunctionExpressionName, /*typeArguments*/ undefined, map(parameters, p => <Identifier>p.name));
21442167
if (isSimpleLoop) {
21452168
statements.push(createStatement(call));
21462169
copyOutParameters(state.loopOutParameters, CopyDirection.ToOriginal, statements);
@@ -2150,7 +2173,7 @@ namespace ts {
21502173
const stateVariable = createVariableStatement(
21512174
/*modifiers*/ undefined,
21522175
createVariableDeclarationList(
2153-
[createVariableDeclaration(loopResultName, call)]
2176+
[createVariableDeclaration(loopResultName, /*type*/ undefined, call)]
21542177
)
21552178
);
21562179
statements.push(stateVariable);
@@ -2474,6 +2497,7 @@ namespace ts {
24742497
thisArg,
24752498
transformAndSpreadElements(createNodeArray([createVoidZero(), ...node.arguments]), /*needsUniqueCopy*/ false, /*multiLine*/ false, /*hasTrailingComma*/ false)
24762499
),
2500+
/*typeArguments*/ undefined,
24772501
[]
24782502
);
24792503
}
@@ -2586,7 +2610,7 @@ namespace ts {
25862610
inlineExpressions([
25872611
createAssignment(temp, createArrayLiteral(cookedStrings)),
25882612
createAssignment(createPropertyAccess(temp, "raw"), createArrayLiteral(rawStrings)),
2589-
createCall(tag, templateArguments)
2613+
createCall(tag, /*typeArguments*/ undefined, templateArguments)
25902614
])
25912615
);
25922616
}
@@ -2894,20 +2918,6 @@ namespace ts {
28942918
return getDeclarationName(node, allowComments, allowSourceMaps, NodeEmitFlags.LocalName);
28952919
}
28962920

2897-
/**
2898-
* Gets the export name for a declaration for use in expressions.
2899-
*
2900-
* An export name will *always* be prefixed with an module or namespace export modifier
2901-
* like "exports." if one is required.
2902-
*
2903-
* @param node The declaration.
2904-
* @param allowComments A value indicating whether comments may be emitted for the name.
2905-
* @param allowSourceMaps A value indicating whether source maps may be emitted for the name.
2906-
*/
2907-
function getExportName(node: ClassDeclaration | ClassExpression | FunctionDeclaration, allowComments?: boolean, allowSourceMaps?: boolean) {
2908-
return getDeclarationName(node, allowComments, allowSourceMaps, NodeEmitFlags.ExportName);
2909-
}
2910-
29112921
/**
29122922
* Gets the name of a declaration, without source map or comments.
29132923
*

src/compiler/transformers/module/es6.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ namespace ts {
5050
return undefined; // do not emit export equals for ES6
5151
}
5252
const original = getOriginalNode(node);
53-
return nodeIsSynthesized(original) || resolver.isValueAliasDeclaration(original) ? node: undefined;
53+
return nodeIsSynthesized(original) || resolver.isValueAliasDeclaration(original) ? node : undefined;
5454
}
5555

5656
function visitExportDeclaration(node: ExportDeclaration): ExportDeclaration {
@@ -64,7 +64,7 @@ namespace ts {
6464
if (node.exportClause === newExportClause) {
6565
return node;
6666
}
67-
return newExportClause
67+
return newExportClause
6868
? createExportDeclaration(newExportClause, node.moduleSpecifier)
6969
: undefined;
7070
}
@@ -106,12 +106,12 @@ namespace ts {
106106
const newNamedBindings = visitNode(node.namedBindings, visitor, isNamedImportBindings, /*optional*/ true);
107107
return newDefaultImport !== node.name || newNamedBindings !== node.namedBindings
108108
? createImportClause(newDefaultImport, newNamedBindings)
109-
: node;
109+
: node;
110110
}
111111

112112
function visitNamedBindings(node: NamedImportBindings): VisitResult<NamedImportBindings> {
113113
if (node.kind === SyntaxKind.NamespaceImport) {
114-
return resolver.isReferencedAliasDeclaration(node) ? node: undefined;
114+
return resolver.isReferencedAliasDeclaration(node) ? node : undefined;
115115
}
116116
else {
117117
const newNamedImportElements = visitNodes((<NamedImports>node).elements, visitor, isImportSpecifier);

0 commit comments

Comments
 (0)