-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Fast pipeline binding for ForEach-Object #10153
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
Conversation
|
When using & { process { $_ }} instead of Foreach-Object/Where-Object, there is a huge performance gain, apparently caused by using a simple function instead of an advanced function. Parameter binding is no issue for these cmdlets in the vast majority of use cases as users typically only use $_ in their payload scriptblocks, and thus internally switching to a simple function w/o cmdletbinding might improve the speed without having to reinvent the entire cmdlet. |
|
I have an open issue #9941 for Where-Object. @daxian-dbw is taking care of it. Maybe those two can be worked together? Seems similar to me? |
|
I guess there is 2 places for perf consideration:
|
|
@lzybkr @daxian-dbw could the suggestion from @TobiasPSP be considered for 7.1? In his blog he shows many alternative approaches to speed up pwsh that should be unnecessary due to optimizations in the core. |
|
This pull request has been automatically marked as Review Needed because it has been there has not been any activity for 7 days. |
|
This pull request has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 15 days. It will be closed if no further activity occurs within 10 days of this comment. |
|
Why did this PR get marked as 'Waiting on Author' when there wasn't any maintainer feedback that I can see? EDIT: Oops, nevermind. Didn't notice daxian-dbw was part of the PowerShell team. |
PR Summary
Proof of concept change to speed up ForEach-Object. I don't plan to do further work, but someone else can hopefully use the idea.
This undoubtedly misses something important, but it does make ForEach-Object faster. (Note I include the loop for comparison, the cmdlet is still an order of magnitude slower.)
1..100kb | % { }foreach ($i in 1..100kb) { }dir -re PowerShell | % NamePR Context
People often use trivial pipelines with
ForEach-Objectto micro-benchmark PowerShell and conclude that it is slow, or they should use theforeachstatement instead of the cmdlet.I've long had the idea that we can have dynamic sites like we have for property access or method invocation, for but instead for parameter binding.
This PR is the embodiment of that idea for the single use case of binding
InputObjectfor theForEach-Objectcommand.PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:or[ WIP ]to the beginning of the title (theWIPbot will keep its status check atPendingwhile the prefix is present) and remove the prefix when the PR is ready.