Skip to content

Commit ffc5541

Browse files
authored
Merge pull request #9852 from Microsoft/transforms-visitPerf
[Transforms] Address visitor performance.
2 parents d4ad7f3 + b087526 commit ffc5541

File tree

12 files changed

+1779
-1211
lines changed

12 files changed

+1779
-1211
lines changed

src/compiler/core.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,9 @@ namespace ts {
355355
export function addRange<T>(to: T[], from: T[]): void {
356356
if (to && from) {
357357
for (const v of from) {
358-
to.push(v);
358+
if (v !== undefined) {
359+
to.push(v);
360+
}
359361
}
360362
}
361363
}

src/compiler/factory.ts

Lines changed: 721 additions & 164 deletions
Large diffs are not rendered by default.

src/compiler/program.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1441,6 +1441,7 @@ namespace ts {
14411441
// checked is to not pass the file to getEmitResolver.
14421442
const emitResolver = getDiagnosticsProducingTypeChecker().getEmitResolver((options.outFile || options.out) ? undefined : sourceFile);
14431443

1444+
performance.emit("beforeEmit");
14441445
const emitStart = performance.mark();
14451446

14461447
const emitResult = emitFiles(
@@ -1449,6 +1450,7 @@ namespace ts {
14491450
sourceFile);
14501451

14511452
performance.measure("emitTime", emitStart);
1453+
performance.emit("afterEmit");
14521454

14531455
return emitResult;
14541456
}

src/compiler/transformers/destructuring.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,9 @@ namespace ts {
351351
value = ensureIdentifier(value, /*reuseIdentifierExpressions*/ true, location, emitTempVariableAssignment);
352352
return createConditional(
353353
createStrictEquality(value, createVoidZero()),
354+
createToken(SyntaxKind.QuestionToken),
354355
defaultValue,
356+
createToken(SyntaxKind.ColonToken),
355357
value
356358
);
357359
}

src/compiler/transformers/es6.ts

Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,7 +1196,7 @@ namespace ts {
11961196
*/
11971197
function transformAccessorsToStatement(receiver: LeftHandSideExpression, accessors: AllAccessorDeclarations): Statement {
11981198
const statement = createStatement(
1199-
transformAccessorsToExpression(receiver, accessors),
1199+
transformAccessorsToExpression(receiver, accessors, /*startsOnNewLine*/ false),
12001200
/*location*/ getSourceMapRange(accessors.firstAccessor)
12011201
);
12021202

@@ -1213,7 +1213,7 @@ namespace ts {
12131213
*
12141214
* @param receiver The receiver for the member.
12151215
*/
1216-
function transformAccessorsToExpression(receiver: LeftHandSideExpression, { firstAccessor, getAccessor, setAccessor }: AllAccessorDeclarations): Expression {
1216+
function transformAccessorsToExpression(receiver: LeftHandSideExpression, { firstAccessor, getAccessor, setAccessor }: AllAccessorDeclarations, startsOnNewLine: boolean): Expression {
12171217
// To align with source maps in the old emitter, the receiver and property name
12181218
// arguments are both mapped contiguously to the accessor name.
12191219
const target = getMutableClone(receiver);
@@ -1246,7 +1246,7 @@ namespace ts {
12461246
createPropertyAssignment("configurable", createLiteral(true))
12471247
);
12481248

1249-
return createCall(
1249+
const call = createCall(
12501250
createPropertyAccess(createIdentifier("Object"), "defineProperty"),
12511251
/*typeArguments*/ undefined,
12521252
[
@@ -1255,6 +1255,10 @@ namespace ts {
12551255
createObjectLiteral(properties, /*location*/ undefined, /*multiLine*/ true)
12561256
]
12571257
);
1258+
if (startsOnNewLine) {
1259+
call.startsOnNewLine = true;
1260+
}
1261+
return call;
12581262
}
12591263

12601264
/**
@@ -1895,26 +1899,27 @@ namespace ts {
18951899

18961900
// Write out the first non-computed properties, then emit the rest through indexing on the temp variable.
18971901
const expressions: Expression[] = [];
1898-
addNode(expressions,
1899-
createAssignment(
1900-
temp,
1901-
setNodeEmitFlags(
1902-
createObjectLiteral(
1903-
visitNodes(properties, visitor, isObjectLiteralElement, 0, numInitialProperties),
1904-
/*location*/ undefined,
1905-
node.multiLine
1906-
),
1907-
NodeEmitFlags.Indented
1908-
)
1909-
),
1910-
node.multiLine
1902+
const assignment = createAssignment(
1903+
temp,
1904+
setNodeEmitFlags(
1905+
createObjectLiteral(
1906+
visitNodes(properties, visitor, isObjectLiteralElement, 0, numInitialProperties),
1907+
/*location*/ undefined,
1908+
node.multiLine
1909+
),
1910+
NodeEmitFlags.Indented
1911+
)
19111912
);
1913+
if (node.multiLine) {
1914+
assignment.startsOnNewLine = true;
1915+
}
1916+
expressions.push(assignment);
19121917

19131918
addObjectLiteralMembers(expressions, node, temp, numInitialProperties);
19141919

19151920
// We need to clone the temporary identifier so that we can write it on a
19161921
// new line
1917-
addNode(expressions, getMutableClone(temp), node.multiLine);
1922+
expressions.push(node.multiLine ? startOnNewLine(getMutableClone(temp)) : temp);
19181923
return inlineExpressions(expressions);
19191924
}
19201925

@@ -2313,21 +2318,21 @@ namespace ts {
23132318
case SyntaxKind.SetAccessor:
23142319
const accessors = getAllAccessorDeclarations(node.properties, <AccessorDeclaration>property);
23152320
if (property === accessors.firstAccessor) {
2316-
addNode(expressions, transformAccessorsToExpression(receiver, accessors), node.multiLine);
2321+
expressions.push(transformAccessorsToExpression(receiver, accessors, node.multiLine));
23172322
}
23182323

23192324
break;
23202325

23212326
case SyntaxKind.PropertyAssignment:
2322-
addNode(expressions, transformPropertyAssignmentToExpression(node, <PropertyAssignment>property, receiver), node.multiLine);
2327+
expressions.push(transformPropertyAssignmentToExpression(node, <PropertyAssignment>property, receiver, node.multiLine));
23232328
break;
23242329

23252330
case SyntaxKind.ShorthandPropertyAssignment:
2326-
addNode(expressions, transformShorthandPropertyAssignmentToExpression(node, <ShorthandPropertyAssignment>property, receiver), node.multiLine);
2331+
expressions.push(transformShorthandPropertyAssignmentToExpression(node, <ShorthandPropertyAssignment>property, receiver, node.multiLine));
23272332
break;
23282333

23292334
case SyntaxKind.MethodDeclaration:
2330-
addNode(expressions, transformObjectLiteralMethodDeclarationToExpression(node, <MethodDeclaration>property, receiver), node.multiLine);
2335+
expressions.push(transformObjectLiteralMethodDeclarationToExpression(node, <MethodDeclaration>property, receiver, node.multiLine));
23312336
break;
23322337

23332338
default:
@@ -2344,15 +2349,19 @@ namespace ts {
23442349
* @param property The PropertyAssignment node.
23452350
* @param receiver The receiver for the assignment.
23462351
*/
2347-
function transformPropertyAssignmentToExpression(node: ObjectLiteralExpression, property: PropertyAssignment, receiver: Expression) {
2348-
return createAssignment(
2352+
function transformPropertyAssignmentToExpression(node: ObjectLiteralExpression, property: PropertyAssignment, receiver: Expression, startsOnNewLine: boolean) {
2353+
const expression = createAssignment(
23492354
createMemberAccessForPropertyName(
23502355
receiver,
23512356
visitNode(property.name, visitor, isPropertyName)
23522357
),
23532358
visitNode(property.initializer, visitor, isExpression),
23542359
/*location*/ property
23552360
);
2361+
if (startsOnNewLine) {
2362+
expression.startsOnNewLine = true;
2363+
}
2364+
return expression;
23562365
}
23572366

23582367
/**
@@ -2362,15 +2371,19 @@ namespace ts {
23622371
* @param property The ShorthandPropertyAssignment node.
23632372
* @param receiver The receiver for the assignment.
23642373
*/
2365-
function transformShorthandPropertyAssignmentToExpression(node: ObjectLiteralExpression, property: ShorthandPropertyAssignment, receiver: Expression) {
2366-
return createAssignment(
2374+
function transformShorthandPropertyAssignmentToExpression(node: ObjectLiteralExpression, property: ShorthandPropertyAssignment, receiver: Expression, startsOnNewLine: boolean) {
2375+
const expression = createAssignment(
23672376
createMemberAccessForPropertyName(
23682377
receiver,
23692378
visitNode(property.name, visitor, isPropertyName)
23702379
),
23712380
getSynthesizedClone(property.name),
23722381
/*location*/ property
23732382
);
2383+
if (startsOnNewLine) {
2384+
expression.startsOnNewLine = true;
2385+
}
2386+
return expression;
23742387
}
23752388

23762389
/**
@@ -2380,15 +2393,19 @@ namespace ts {
23802393
* @param method The MethodDeclaration node.
23812394
* @param receiver The receiver for the assignment.
23822395
*/
2383-
function transformObjectLiteralMethodDeclarationToExpression(node: ObjectLiteralExpression, method: MethodDeclaration, receiver: Expression) {
2384-
return createAssignment(
2396+
function transformObjectLiteralMethodDeclarationToExpression(node: ObjectLiteralExpression, method: MethodDeclaration, receiver: Expression, startsOnNewLine: boolean) {
2397+
const expression = createAssignment(
23852398
createMemberAccessForPropertyName(
23862399
receiver,
23872400
visitNode(method.name, visitor, isPropertyName)
23882401
),
23892402
transformFunctionLikeToExpression(method, /*location*/ method, /*name*/ undefined),
23902403
/*location*/ method
23912404
);
2405+
if (startsOnNewLine) {
2406+
expression.startsOnNewLine = true;
2407+
}
2408+
return expression;
23922409
}
23932410

23942411
/**

src/compiler/transformers/generators.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -580,8 +580,8 @@ namespace ts {
580580
transformAndEmitStatements(body.statements, statementOffset);
581581

582582
const buildResult = build();
583-
addNodes(statements, endLexicalEnvironment());
584-
addNode(statements, createReturn(buildResult));
583+
addRange(statements, endLexicalEnvironment());
584+
statements.push(createReturn(buildResult));
585585

586586
// Restore previous generator state
587587
inGeneratorFunctionBody = savedInGeneratorFunctionBody;
@@ -1019,7 +1019,7 @@ namespace ts {
10191019
);
10201020

10211021
const expressions = reduceLeft(properties, reduceProperty, <Expression[]>[], numInitialProperties);
1022-
addNode(expressions, getMutableClone(temp), multiLine);
1022+
expressions.push(multiLine ? startOnNewLine(getMutableClone(temp)) : temp);
10231023
return inlineExpressions(expressions);
10241024

10251025
function reduceProperty(expressions: Expression[], property: ObjectLiteralElement) {
@@ -1029,7 +1029,13 @@ namespace ts {
10291029
}
10301030

10311031
const expression = createExpressionForObjectLiteralElement(node, property, temp);
1032-
addNode(expressions, visitNode(expression, visitor, isExpression), multiLine);
1032+
const visited = visitNode(expression, visitor, isExpression);
1033+
if (visited) {
1034+
if (multiLine) {
1035+
visited.startsOnNewLine = true;
1036+
}
1037+
expressions.push(visited);
1038+
}
10331039
return expressions;
10341040
}
10351041
}

src/compiler/transformers/module/es6.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,11 @@ namespace ts {
6565
return node;
6666
}
6767
return newExportClause
68-
? createExportDeclaration(newExportClause, node.moduleSpecifier)
68+
? createExportDeclaration(
69+
/*decorators*/ undefined,
70+
/*modifiers*/ undefined,
71+
newExportClause,
72+
node.moduleSpecifier)
6973
: undefined;
7074
}
7175

@@ -92,7 +96,11 @@ namespace ts {
9296
return undefined;
9397
}
9498
else if (newImportClause !== node.importClause) {
95-
return createImportDeclaration(newImportClause, node.moduleSpecifier);
99+
return createImportDeclaration(
100+
/*decorators*/ undefined,
101+
/*modifiers*/ undefined,
102+
newImportClause,
103+
node.moduleSpecifier);
96104
}
97105
}
98106
return node;

src/compiler/transformers/module/module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -734,8 +734,10 @@ namespace ts {
734734
statements.push(
735735
setOriginalNode(
736736
createClassDeclaration(
737+
/*decorators*/ undefined,
737738
/*modifiers*/ undefined,
738739
name,
740+
/*typeParameters*/ undefined,
739741
node.heritageClauses,
740742
node.members,
741743
/*location*/ node
@@ -908,7 +910,7 @@ namespace ts {
908910
setNodeEmitFlags(node, NodeEmitFlags.NoSubstitution);
909911
let transformedUnaryExpression: BinaryExpression;
910912
if (node.kind === SyntaxKind.PostfixUnaryExpression) {
911-
transformedUnaryExpression = createBinaryWithOperatorToken(
913+
transformedUnaryExpression = createBinary(
912914
operand,
913915
createNode(operator === SyntaxKind.PlusPlusToken ? SyntaxKind.PlusEqualsToken : SyntaxKind.MinusEqualsToken),
914916
createLiteral(1),

0 commit comments

Comments
 (0)