Skip to content

Perf: Add PSObjectBuilder #7481

Closed
Closed

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

  1. iSazonov commented on Aug 9, 2018

    @iSazonov
    Collaborator

    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.

  2. added
    Issue-Enhancement
    WG-Engine-Performance
    Issue-Discussion
    on Aug 9, 2018
  3. dragonwolf83 commented on Aug 9, 2018

    @dragonwolf83

    Add-Member performance would be good to test as well.

  4. iSazonov commented on Aug 10, 2018

    @iSazonov
    Collaborator

    Add-Member is another code path - adding new properties (members) vs bulk creating new similar PSObjects.

  5. powercode commented on Aug 10, 2018

    @powercode
    CollaboratorAuthor

    There is one use case where it might make sense, and that is the -NotePropertyMembers @{...}.

    That could potentially be made faster.

  6. iSazonov commented on Aug 10, 2018

    @iSazonov
    Collaborator

    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 like Add(String, Object) in PSMemberInfoIntegratingCollection. 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.

  7. powercode commented on Aug 10, 2018

    @powercode
    CollaboratorAuthor

    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.

  8. iSazonov commented on Aug 10, 2018

    @iSazonov
    Collaborator

    Staffan Gustafsson (@powercode) Thanks! I did not see your PR #7481 (Great!) - now I can review your ideas!

  9. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    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.

  10. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    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.

  11. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    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.

  12. added
    Resolution-No Activity
    on Nov 16, 2023
  13. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-DiscussionIssue-EnhancementResolution-No ActivityWG-Engine-Performance

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions