From 44bb5861cb2e8ee60ca4d251c8dbfb25c500f029 Mon Sep 17 00:00:00 2001 From: Dongbo Wang Date: Fri, 21 Aug 2020 10:28:16 -0700 Subject: [PATCH] Avoid multiple enumerations of an `IEnumerable` instance in `Compiler.cs` (#13491) --- .../engine/parser/Compiler.cs | 32 ++++++++++++++++--- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/System.Management.Automation/engine/parser/Compiler.cs b/src/System.Management.Automation/engine/parser/Compiler.cs index 3a25e8c5d9..642bd1e1dc 100644 --- a/src/System.Management.Automation/engine/parser/Compiler.cs +++ b/src/System.Management.Automation/engine/parser/Compiler.cs @@ -949,9 +949,20 @@ namespace System.Management.Automation.Language return result; } - private IEnumerable CompileInvocationArguments(IEnumerable arguments) + private IEnumerable CompileInvocationArguments(IReadOnlyList arguments) { - return arguments == null ? Array.Empty() : arguments.Select(CompileExpressionOperand); + if (arguments is null || arguments.Count == 0) + { + return Array.Empty(); + } + + var result = new Expression[arguments.Count]; + for (int i = 0; i < result.Length; i++) + { + result[i] = CompileExpressionOperand(arguments[i]); + } + + return result; } internal Expression ReduceAssignment(ISupportsAssignment left, TokenKind tokenKind, Expression right) @@ -6669,9 +6680,20 @@ namespace System.Management.Automation.Language return _argExprTemps; } - return InvokeMemberExpressionAst.Arguments == null - ? Array.Empty() - : InvokeMemberExpressionAst.Arguments.Select(compiler.Compile).ToArray(); + ReadOnlyCollection arguments = InvokeMemberExpressionAst.Arguments; + + if (arguments is null || arguments.Count == 0) + { + return Array.Empty(); + } + + var result = new Expression[arguments.Count]; + for (int i = 0; i < result.Length; i++) + { + result[i] = compiler.Compile(arguments[i]); + } + + return result; } public Expression GetValue(Compiler compiler, List exprs, List temps)