-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Improve Array BinaryAdd Method for object[] #23901
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
Use Array.Resize when doing += on an array type. This is more efficient than the default BinaryAdd done on other enumerable types. As adding to an array is a common operation done by people new to PowerShell this should reduce some of the issues which come with that type of code.
In scripts/functions yes, but if this really reduces time spent on += by 90% I'll likely never bother using lists in interactive sessions anymore. Wow 🙌 |
|
The Engine WG discussed this and agree with the change, and it is ready to be reviewed. |
iSazonov
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: interesting, have we such issues with Collection, List (and others IEnumarable-s?
Somewhat yes, at least the known size types (not |
I would lean toward "maybe, but out of scope for this PR" just to get the most common use case settled quickly. |
To be fair, interactively it's really hard to beat statement capture (e.g. |
True. I'm usually a few foreach-loops deep or splitting into multiple arrays when I need Either way, looking forward to this. |
daxian-dbw
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
|
This is a great improvement to PowerShell 🎉. Btw. a similar issue exists for using the increase assignment operator (
|
PR Summary
Use Array.Resize when doing += on an array type. This is more efficient than the default BinaryAdd done on other enumerable types. As adding to an array is a common operation done by people new to PowerShell this should reduce some of the issues which come with that type of code.
An example of dramatic difference this can make is reflected by this code
When run with the new code (
TEST=true) you can see it is 8 seconds faster and requires less memory to allocate. As the arrays get larger the differences are even more profound.This doesn't negate the existing performance impacts of adding to an array, it just removes extra work that wasn't needed in the first place (which was pretty inefficient) making it slower than it has to. People should still use an alternative like capturing the output from the pipeline or use
List<T>.PR Context
Partially Fixes: #23899
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.- [ ] Issue filed:
(which runs in a different PS Host).