Skip to content

Avoid multiple enumerations of an IEnumerable<Expression> instance in Compiler.cs - #13491

Merged
Rob Holt (rjmholt) merged 3 commits into
PowerShell:masterfrom
daxian-dbw:perf2
Aug 21, 2020
Merged

Rob Holt (rjmholt) merged 3 commits into
PowerShell:masterfrom
daxian-dbw:perf2

Conversation

@daxian-dbw

Copy link
Copy Markdown
Member

PR Summary

Fix #13474
Avoid multiple enumerations of an IEnumerable<Expression> instance at a few places in Compiler.cs.
More than one enumeration cause the operations on individual elements of the source collection to be done repeatedly.

PR Checklist

@rjmholt Rob Holt (rjmholt) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like a decent fix for now. It would be nice if we could remove using System.Linq; from the top of Compiler.cs and clean up all the LINQ in there

private IEnumerable<Expression> CompileInvocationArguments(IEnumerable<ExpressionAst> arguments)
{
return arguments == null ? Array.Empty<Expression>() : arguments.Select(CompileExpressionOperand);
return arguments == null ? Array.Empty<Expression>() : arguments.Select(CompileExpressionOperand).ToArray();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like everywhere this method is called passes a ReadOnlyCollection<ExpressionAst>. Could type arguments as IReadOnlyList and do this:

if (arguments is null)
{
    return Array.Empty<Expression>();
}

var result = new Expression[arguments.Count];
for (int i = result.Length - 1; i >= 0; i--)
{
    result[i] = CompileExpressionOperand(arguments[i]);
}

return result;

If you would rather not change the method parameter type then LGTM!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code updated.

@vexx32

Copy link
Copy Markdown
Collaborator

Rob Holt (@rjmholt) is that an invitation? 😏

@rjmholt

Copy link
Copy Markdown
Collaborator

PoshChan-Bot (@PoshChan) please remind me in 20 hours

@iSazonov Ilya (iSazonov) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, it looks as a trick because these methods (InvokeMember/InvokeBaseCtorMethod/InvokeDynamicMember) still use IEnumerable type.

@rjmholt
Rob Holt (rjmholt) merged commit 44bb586 into PowerShell:master Aug 21, 2020
@iSazonov Ilya (iSazonov) added the CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log label Aug 21, 2020
@iSazonov Ilya (iSazonov) added this to the 7.1.0-preview.7 milestone Aug 21, 2020
@PoshChan

Copy link
Copy Markdown
Collaborator

Rob Holt (@rjmholt), this is the reminder you requested 20 hours ago

@ghost

Copy link
Copy Markdown

🎉v7.1.0-preview.7 has been released which incorporates this pull request.:tada:

Handy links:

Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj-contribs/PowerShell that referenced this pull request Aug 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CL-General Indicates that a PR should be marked as a general cmdlet change in the Change Log

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LINQ in the compiler

6 participants