Repository navigation
Description
Not really a bug but:
PowerShell/src/System.Management.Automation/engine/parser/Compiler.cs
Lines 952 to 955 in 904e551
| private IEnumerable<Expression> CompileInvocationArguments(IEnumerable<ExpressionAst> arguments) | |
| { | |
| return arguments == null ? Array.Empty<Expression>() : arguments.Select(CompileExpressionOperand); | |
| } |
And the method in the select:
PowerShell/src/System.Management.Automation/engine/parser/Compiler.cs
Lines 941 to 950 in 904e551
| internal Expression CompileExpressionOperand(ExpressionAst exprAst) | |
| { | |
| var result = Compile(exprAst); | |
| if (result.Type == typeof(void)) | |
| { | |
| result = Expression.Block(result, ExpressionCache.NullConstant); | |
| } | |
| return result; | |
| } |
That method is used to compile method arguments, the result is then enumerated twice (once in the first highlighted line, one is the last):
PowerShell/src/System.Management.Automation/engine/parser/Compiler.cs
Lines 6338 to 6344 in 904e551
| var callInfo = new CallInfo(args.Count()); | |
| var classScope = _memberFunctionType != null ? _memberFunctionType.Type : null; | |
| var binder = name.Equals("new", StringComparison.OrdinalIgnoreCase) && @static | |
| ? (CallSiteBinder)PSCreateInstanceBinder.Get(callInfo, constraints, publicTypeOnly: true) | |
| : PSInvokeMemberBinder.Get(name, callInfo, @static, propertySet, constraints, classScope); | |
| var dynamicExprFromBinder = DynamicExpression.Dynamic(binder, typeof(object), args.Prepend(target)); |
This causes all method arguments to be compiled twice due to multiple enumeration. I'm not aware of any bugs due to this behavior, but it probably negatively impacts compile time for complicated member invocations. Also the method supplied to Select is not done in such a way that the delegate can be cached per instance by roslyn.
Activity
iSazonov commentedon Aug 20, 2020
/cc Dongbo Wang (@daxian-dbw)
iSazonov commentedon Aug 20, 2020
Patrick Meinecke (@SeeminglyScience) Could you please create a simple repro so that we can measure performance impact since you mentioned this?
SeeminglyScience commentedon Aug 20, 2020
In theory something like this should be significantly faster after a fix:
Appx.format.ps1xml as XElement (Click to expand)
If I'm reading it right the inner most nodes will be compiled quite a few times.
daxian-dbw commentedon Aug 20, 2020
Thanks Patrick Meinecke (@SeeminglyScience)! I submitted the PR #13491 to fix this, please take a look if you have time.