Skip to content
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

Avoid unnecessary tail increment in collection expressions codegen #75559

Merged
merged 1 commit into from
Oct 21, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -738,7 +738,7 @@ bool tryGetToArrayMethod(TypeSymbol spreadTypeOriginalDefinition, WellKnownType
localsBuilder,
numberIncludingLastSpread,
sideEffects,
addElement: (ArrayBuilder<BoundExpression> expressions, BoundExpression arrayTemp, BoundExpression rewrittenValue) =>
addElement: (ArrayBuilder<BoundExpression> expressions, BoundExpression arrayTemp, BoundExpression rewrittenValue, bool isLastElement) =>
{
Debug.Assert(arrayTemp.Type is ArrayTypeSymbol);
Debug.Assert(indexTemp.Type is { SpecialType: SpecialType.System_Int32 });
Expand All @@ -754,14 +754,17 @@ bool tryGetToArrayMethod(TypeSymbol spreadTypeOriginalDefinition, WellKnownType
rewrittenValue,
isRef: false,
elementType));
// index = index + 1;
expressions.Add(
new BoundAssignmentOperator(
expressionSyntax,
indexTemp,
_factory.Binary(BinaryOperatorKind.Addition, indexTemp.Type, indexTemp, _factory.Literal(1)),
isRef: false,
indexTemp.Type));
if (!isLastElement)
{
// index = index + 1;
expressions.Add(
new BoundAssignmentOperator(
expressionSyntax,
indexTemp,
_factory.Binary(BinaryOperatorKind.Addition, indexTemp.Type, indexTemp, _factory.Literal(1)),
isRef: false,
indexTemp.Type));
}
},
tryOptimizeSpreadElement: (ArrayBuilder<BoundExpression> sideEffects, BoundExpression arrayTemp, BoundCollectionExpressionSpreadElement spreadElement, BoundExpression rewrittenSpreadOperand) =>
{
Expand Down Expand Up @@ -1072,7 +1075,7 @@ private BoundExpression CreateAndPopulateList(BoundCollectionExpression node, Ty
localsBuilder,
numberIncludingLastSpread,
sideEffects,
addElement: (ArrayBuilder<BoundExpression> expressions, BoundExpression spanTemp, BoundExpression rewrittenValue) =>
addElement: (ArrayBuilder<BoundExpression> expressions, BoundExpression spanTemp, BoundExpression rewrittenValue, bool isLastElement) =>
{
Debug.Assert(spanTemp.Type is NamedTypeSymbol);
Debug.Assert(indexTemp.Type is { SpecialType: SpecialType.System_Int32 });
Expand All @@ -1088,14 +1091,17 @@ private BoundExpression CreateAndPopulateList(BoundCollectionExpression node, Ty
rewrittenValue,
isRef: false,
elementType));
// index = index + 1;
expressions.Add(
new BoundAssignmentOperator(
expressionSyntax,
indexTemp,
_factory.Binary(BinaryOperatorKind.Addition, indexTemp.Type, indexTemp, _factory.Literal(1)),
isRef: false,
indexTemp.Type));
if (!isLastElement)
{
// index = index + 1;
expressions.Add(
new BoundAssignmentOperator(
expressionSyntax,
indexTemp,
_factory.Binary(BinaryOperatorKind.Addition, indexTemp.Type, indexTemp, _factory.Literal(1)),
isRef: false,
indexTemp.Type));
}
},
tryOptimizeSpreadElement: (ArrayBuilder<BoundExpression> sideEffects, BoundExpression spanTemp, BoundCollectionExpressionSpreadElement spreadElement, BoundExpression rewrittenSpreadOperand) =>
{
Expand All @@ -1116,7 +1122,7 @@ private BoundExpression CreateAndPopulateList(BoundCollectionExpression node, Ty
localsBuilder,
numberIncludingLastSpread,
sideEffects,
addElement: (ArrayBuilder<BoundExpression> expressions, BoundExpression listTemp, BoundExpression rewrittenValue) =>
addElement: (ArrayBuilder<BoundExpression> expressions, BoundExpression listTemp, BoundExpression rewrittenValue, bool isLastElement) =>
{
// list.Add(element);
expressions.Add(
Expand Down Expand Up @@ -1180,7 +1186,7 @@ private void AddCollectionExpressionElements(
ArrayBuilder<BoundLocal> rewrittenExpressions,
int numberIncludingLastSpread,
ArrayBuilder<BoundExpression> sideEffects,
Action<ArrayBuilder<BoundExpression>, BoundExpression, BoundExpression> addElement,
Action<ArrayBuilder<BoundExpression>, BoundExpression, BoundExpression, bool> addElement,
Func<ArrayBuilder<BoundExpression>, BoundExpression, BoundCollectionExpressionSpreadElement, BoundExpression, bool> tryOptimizeSpreadElement)
{
for (int i = 0; i < elements.Length; i++)
Expand All @@ -1202,7 +1208,7 @@ private void AddCollectionExpressionElements(
{
var rewrittenValue = VisitExpression(((BoundExpressionStatement)iteratorBody).Expression);
var builder = ArrayBuilder<BoundExpression>.GetInstance();
addElement(builder, rewrittenReceiver, rewrittenValue);
addElement(builder, rewrittenReceiver, rewrittenValue, false);
var statements = builder.SelectAsArray(expr => (BoundStatement)new BoundExpressionStatement(expr.Syntax, expr));
builder.Free();
Debug.Assert(statements.Length > 0);
Expand All @@ -1214,7 +1220,8 @@ private void AddCollectionExpressionElements(
}
else
{
addElement(sideEffects, rewrittenReceiver, rewrittenExpression);
var isLastElement = i == (elements.Length - 1);
addElement(sideEffects, rewrittenReceiver, rewrittenExpression, isLastElement);
}
}
}
Expand Down
Loading