Skip to content

Add a switch to Select-String that makes it return just strings for convenience and performance #7713

Closed

Description

Related: #7712

Sometimes, all you're interested in is the matching input lines as strings rather than full-blown [Microsoft.PowerShell.Commands.MatchInfo] instances.

Not having to extract the .Line property in subsequent processing can also significantly improve performance.

Update: Performance was originally discussed only with respect to how expensive wrapping the matched strings in MatchInfo instances is to begin with: nothing to worry about, apparently - see comments for a discussion.

A new switch named, say, -Bare switch could instruct Select-String to output (undecorated) strings only.

$res = 'line1', 'lineother', 'line3' | Select-String '\d' -Bare; $res; '---'; $res[0].GetType().Name
line1
line3
---
String

Note: I'm suggesting the somewhat abstract name -Bare, because the abstract logic of this proposal - namely to output "bare" objects that are undecorated (have no ETS properties added to them) / are not wrapped in instances of a helper type - applies to other cmdlets as well, such as in #7537, and its conceivable that other cmdlets may benefit from -Bare too, such as ConvertTo-Json in order to solve #5797; other cmdlets could benefit from such a switch too, such as Compare-Object.
Update: The case for -Bare as a general pattern has since been made in #7855.

Environment data

Written as of:

PowerShell Core 6.1.0-preview.4

Activity

  1. iSazonov commented on Sep 6, 2018

    @iSazonov
    Collaborator

    instruct Select-Object

    Typo?

    We could consider -AsString parameter - looks more understandably.

  2. added
    Issue-Discussion
    on Sep 6, 2018
  3. mklement0 commented on Sep 6, 2018

    @mklement0
    ContributorAuthor

    Ilya (@iSazonov): Yes, a typo, thanks - fixed.

    How about we use both parameter names?

    I agree that -AsString is more descriptive in this case, but what appeals me to about -Bare is that it can become part of a general pattern, across different cmdlets, where it requests output of undecorated / unwrapped objects.

  4. iSazonov commented on Sep 6, 2018

    @iSazonov
    Collaborator

    -Bare is that it can become part of a general pattern

    In the case it is better to enumerate in the Issue all cases where the parameter should be .

  5. mklement0 commented on Sep 6, 2018

    @mklement0
    ContributorAuthor

    Ilya (@iSazonov):

    I've added another example to the OP, in addition to the previous Get-Content (#7537 ) one.

    If you can think of additional ones, do tell us.

    Other than that, I think the updated OP now sufficiently establishes the case for using the name -Bare.

  6. BrucePay commented on Sep 6, 2018

    @BrucePay
    Collaborator

    Michael Klement (@mklement0) We already have -Raw as the pattern for undecorated output (though we only have one instance of it) so there is no need to add a random new parameter pattern here. But I also don't think -Raw is appropriate as MatchInfo is concrete type, not a decorated object that can be cored (i.e. not a PSObject with note properties.) @iSazonovI I like -AsString since it's more informative and we do things like -AsHashtable elsewhere. And adding multiples aliases for no reason is undesirable - it's just more stuff to learn with no benefit.

    Regarding performance, I doubt creating MatchInfo objects has much to do with Select-String performance. Again, it's a simple concrete type, not NoteProperties on a PSObject so the Get-Content analogy is not appropriate. Compared to the other operations in the cmdlet, I suspect that MatchObject construction would barely register. (Note: This PR will help with overall performance.) In the issue linked above, it seems that the specific issue has to do with rendering the MatchInfo objects rather than creating them.

  7. mklement0 commented on Sep 6, 2018

    @mklement0
    ContributorAuthor

    Bruce Payette (@BrucePay):

    As for the parameter name:

    If you're referring to Get-Content -Raw: it does not provide undecorated output, it just reads the file as a whole - the resulting single string still has ETS properties.

    The only other instance of -Raw I could find is on Format-Hex, which is currently undocumented, and it's unclear to me what it does.

    I therefore consider -Raw "skunked" and -Bare a viable alternative, not least because raw, as stated, has connotations of uninterpreted / undecoded, which do not apply.


    As fo the semantics of the proposed -Bare:

    The OP now states:

    the abstract logic of this proposal - namely to output "bare" objects that are undecorated (have no ETS properties added to them) / are not wrapped in instances of another type

    That is, -Bare would be an abstraction that would apply to both this case (objects of interest wrapped in another type) and the Get-Content case (objects decorated with ETS properties).

    The informal gist of this abstraction is:

    "Give me just the objects I'm interested, nothing else - do not attach properties I don't care about and don't wrap the object in another type providing metadata."


    As for performance:

    The primary reason for creating this issue was to forgo the wrappers around the matching lines, because just wanting the lines themselves is a common use case.

    Any performance gain - if any - would just be beneficial side effect.

    It sounds like you're saying that performance gain will primarily come from bypassing ETS properties rather than straight .NET-type wrappers.


    The issue you meant to link to is #7673 (your link URL has an extraneous char. at the end).

  8. BrucePay commented on Sep 6, 2018

    @BrucePay
    Collaborator

    Michael Klement (@mklement0) Thanks for pointing out the link issue, it should be fixed now.

    It sounds like you're saying that performance gain will primarily come from bypassing ETS properties rather than straight .NET-type wrappers.

    There are no wrappers for MatchInfo, that is, no PSObject extended properties to bypass. MatchInfo is the result type of Select-String just like RegularExpressions.Match is the result type of [regex]::Match(). The match object does contain the string, but it also contains all of the context information.

    In terms of actual string matching performance, Select-String and (git's) grep.exe are not that far apart:

    PSCore (2:213) >  time { grep.exe try C:\netpop\WarAndPeace.txt} | % totalmilliseconds
    61.3222
    PSCore (2:214) >  time { sls try C:\netpop\WarAndPeace.txt} | % totalmilliseconds
    82.9409

    Hopefully Staffan Gustafsson (@powercode) 's changes will make them even closer. Where the performance diverges significantly is when you render the output to string:

    PSCore (2:215) >  time { grep.exe try C:\netpop\WarAndPeace.txt | out-string} | % totalmilliseconds
    66.2149
    PSCore (2:216) >  time { sls try C:\netpop\WarAndPeace.txt | out-string} | % totalmilliseconds
    330.4404

    Now PowerShell is much slower. So speeding up rendering is the place to look for performance issues. In fact, only emitting the string does substantially "improve" performance, at the cost of losing all context information:

    PSCore (2:230) >  time { (sls try C:\netpop\WarAndPeace.txt).line | out-string} | % totalmilliseconds
    96.3773

    Also note that rendering performance is only an issue if you have a lot of matches.

    The primary reason for creating this issue was to forgo the wrappers around the matching lines, because just wanting the lines themselves is a common use case.

    Which is certainly easy enough to accomplish now as shown above. It's not a use case I find especially compelling in PowerShell - if I simply want to filter strings I'll use the -match operator. I use Select-String when I want to know in which file and on what line the pattern is matched.

  9. mklement0 commented on Sep 7, 2018

    @mklement0
    ContributorAuthor

    Bruce Payette (@BrucePay):

    There are no wrappers for MatchInfo, that is, no PSObject extended properties to bypass

    What I meant by wrapper in this context: the core data of interest - the matching lines - are wrapped in a helper type (MatchInfo) whose purpose is to provide context.

    Often there is no need for that wrapper, hence this proposal.


    Which is certainly easy enough to accomplish now as shown above. It's not a use case I find especially compelling in PowerShell - if I simply want to filter strings I'll use the -match operator. I use Select-String when I want to know in which file and on what line the pattern is matched.

    A primary benefit of the pipeline is memory-throttling.

    You forfeit that benefit if you use the -match operator and - to a lesser degree, depending on the number of matches - if you use (...).Line

    Especially with large files, Select-String is the tool of choice and -match is then not a suitable alternative (as it would require loading the entire file into memory up-front).

    In general, it's not a good idea to frame operators vs. cmdlets as something you can choose freely between.

  10. powercode commented on Sep 7, 2018

    @powercode
    Collaborator

    I have done some profiling on Select-String, and the massive hit is creating strings for all lines of every file. This also limits the gains of parallelizing Select-String, since it dies the GC death. (spending half it's time on GC).

    Bruce Payette (@BrucePay) is correct that the allocation of MatchInfos is a microscopic part of the runtime.

    I've opened an issue on .net core, regarding span based alternatives for regex, and work is being done there.

    Once we have a way of quickly scanning lines without allocating strings, and have a RegEx class that doesn't allocate a lot of strings internally, we can take a second pass on the performance of Select-String.

    I have a parallel impementation of Select-String, but it doesn't perform much better because of the GC issues.

  11. iSazonov commented on Sep 7, 2018

    @iSazonov
    Collaborator

    I've opened an issue on .net core, regarding span based alternatives for regex, and work is being done there.

    Please add reference on the issue.

  12. iSazonov commented on Sep 7, 2018

    @iSazonov
    Collaborator

    I think we can exclude "performance" from considiration in the Issue and look only on "convenience and performance".

    So suggestion is to add new switch parameter to output string results not MatchInfo objects. Name can be -Raw or -AsString. It is a question for PowerShell Committee.

  13. added
    Review - Committee
    on Sep 7, 2018
  14. changed the title [-]Add a switch to Select-String that makes it return just strings, for both convenience and performance[/-] [+]Add a switch to Select-String that makes it return just strings for convenience[/+] on Sep 11, 2018
  15. 19 remaining items

  16. SteveL-MSFT commented on Nov 7, 2018

    @SteveL-MSFT
    Member

    @PowerShell/powershell-committee reviewed this again. We are fine with adding a -Raw switch in a new parameter set to emit strings.

  17. added
    Committee-Reviewed
    Up-for-Grabs
    and removed
    Review - Committee
    on Nov 7, 2018
  18. added
    First-Time-Issue
    on Jan 1, 2019
  19. added
    Hacktoberfest
    on Feb 19, 2019
  20. changed the title [-]Add a switch to Select-String that makes it return just strings for convenience[/-] [+]Add a switch to Select-String that makes it return just strings for convenience and performance[/+] on Feb 26, 2019
  21. Jawz84 commented on Jun 14, 2019

    @Jawz84
    Contributor

    I'd like to pick this one up.

  22. vexx32 commented on Jun 14, 2019

    @vexx32
    Collaborator

    Have at it! 💖

  23. Jawz84 commented on Jun 14, 2019

    @Jawz84
    Contributor

    Hey Joel Rain Sallow (/u/ta11ow) (@vexx32) tnx! I've put in a PR as you can see.

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

    Committee-ReviewedFirst-Time-IssueHacktoberfestIssue-DiscussionResolution-FixedUp-for-GrabsWG-Cmdlets

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions