Repository navigation
Description
As a part of some perf work I've been doing lately, I looked at the creations of PSObjects.
The creation of them could be significantly faster if we can provide a context for the creation. The code as it exists is very defensive, doing work multiple times that in many cases is only needed once etc.
I propose we add two new types, ref struct PSObjectBuilder, and class PSObjectBuilderUnsafe.
PSObjectBuilder can be more aggressive since we can guarantee that the object it is building is only ever accessible from the same call stack.
PSObjectBuilderUnsafe has to be a bit more defensive since it could potentially be leaked to another runspace/thread.
The PSObjectBuilder guarantees that the object under construction is never exposed to the outside world until it is fully constructed.
It also keep track of what validations has been done, and can use this to skip expensive checks that otherwise has to be performed.
I have prototyped this at https://github.com/powercode/powershell/tree/PSObjectBuilder where I also used it in the JSON cmdlets and the providers.
The results are quite promising.
ConvertFrom-Json is 7x faster.
gci -re -fo c:\windows -ea:0 is 2-3x faster.
But I'm not quite sure about how to move forward here.
The branch I have contains changes that should go into separate PRs and there are things I would like to get design feedback on.
Should I create a PR anyway or what's the best way to go about this?
Activity
iSazonov commentedon Aug 9, 2018
Staffan Gustafsson (@powercode) Feel free to push the PR - it is easiest way to discuss new ideas and code. Many thanks for your investigations!
Currently I don't understand where we could use
PSObjectBuilder- do we currently create PSObject objects for internal use only in the engine?Also why do we need new struct and class - can we implement new constructors for PSObject ? or you say about "PSObject fabric"?
Please add Select-Object in your performance test. I suppose this will add clarity where we still have performance problems.
dragonwolf83 commentedon Aug 9, 2018
Add-Member performance would be good to test as well.
iSazonov commentedon Aug 10, 2018
Add-Member is another code path - adding new properties (members) vs bulk creating new similar PSObjects.
powercode commentedon Aug 10, 2018
There is one use case where it might make sense, and that is the
-NotePropertyMembers @{...}.That could potentially be made faster.
iSazonov commentedon Aug 10, 2018
With latest build I see that Import-Csv allocates many PSNoteProperty objects then they cloned here - looks wasteful. I don't know why the cloning is needed for. If we can not exclude this in general we could easily exclude this for PSNoteProperty by enhancing
Add()method likeAdd(String, Object)inPSMemberInfoIntegratingCollection. Perhaps this will be useful for Select-Object where we can not expect objects of the same type. If your two new types don't cover the scenario we could consider the new Add() overload.powercode commentedon Aug 10, 2018
That is one of the issues that is addressed by PSObjectBuilder.
They think they are defensive copies, made as we cannot know who else has references to that property. One of the benefits of PSObjectBuilder is that it can guarantee that none of the properties has been seen by anyone else, and hence skip the expensive copies.
iSazonov commentedon Aug 10, 2018
Staffan Gustafsson (@powercode) Thanks! I did not see your PR #7481 (Great!) - now I can review your ideas!
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 23, 2023
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.