You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add a switch to Select-String that makes it return just strings for convenience and performance #7713
Committee-ReviewedPS-Committee has reviewed this and made a decisionFirst-Time-IssueEasy issues first time contributors can work on to learn about this projectHacktoberfestPotential candidate to participate in HacktoberfestIssue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-FixedThe issue is fixed.Up-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Cmdletsgeneral cmdlet issues
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.
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.
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.
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 creatingMatchInfo 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.
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).
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:
Hopefully Staffan Gustafsson (@powercode) 's changes will make them even closer. Where the performance diverges significantly is when you render the output to string:
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:
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.
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.
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.
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.
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
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
Committee-ReviewedPS-Committee has reviewed this and made a decisionFirst-Time-IssueEasy issues first time contributors can work on to learn about this projectHacktoberfestPotential candidate to participate in HacktoberfestIssue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifResolution-FixedThe issue is fixed.Up-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Cmdletsgeneral cmdlet issues
Activity
iSazonov commentedon Sep 6, 2018
Typo?
We could consider
-AsStringparameter - looks more understandably.mklement0 commentedon Sep 6, 2018
Ilya (@iSazonov): Yes, a typo, thanks - fixed.
How about we use both parameter names?
I agree that
-AsStringis more descriptive in this case, but what appeals me to about-Bareis that it can become part of a general pattern, across different cmdlets, where it requests output of undecorated / unwrapped objects.iSazonov commentedon Sep 6, 2018
In the case it is better to enumerate in the Issue all cases where the parameter should be .
mklement0 commentedon Sep 6, 2018
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.BrucePay commentedon Sep 6, 2018
Michael Klement (@mklement0) We already have
-Rawas 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-Rawis appropriate asMatchInfois concrete type, not a decorated object that can be cored (i.e. not aPSObjectwith note properties.) @iSazonovI I like-AsStringsince it's more informative and we do things like-AsHashtableelsewhere. And adding multiples aliases for no reason is undesirable - it's just more stuff to learn with no benefit.Regarding performance, I doubt creating
MatchInfoobjects has much to do withSelect-Stringperformance. Again, it's a simple concrete type, notNotePropertieson aPSObjectso theGet-Contentanalogy is not appropriate. Compared to the other operations in the cmdlet, I suspect thatMatchObjectconstruction 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 theMatchInfoobjects rather than creating them.mklement0 commentedon Sep 6, 2018
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
-RawI could find is onFormat-Hex, which is currently undocumented, and it's unclear to me what it does.I therefore consider
-Raw"skunked" and-Barea 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:
That is,
-Barewould be an abstraction that would apply to both this case (objects of interest wrapped in another type) and theGet-Contentcase (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).
BrucePay commentedon Sep 6, 2018
Michael Klement (@mklement0) Thanks for pointing out the link issue, it should be fixed now.
There are no wrappers for
MatchInfo, that is, noPSObjectextended properties to bypass.MatchInfois the result type ofSelect-Stringjust likeRegularExpressions.Matchis 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-Stringand (git's)grep.exeare not that far apart:Hopefully Staffan Gustafsson (@powercode) 's changes will make them even closer. Where the performance diverges significantly is when you render the output to string:
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:
Also note that rendering performance is only an issue if you have a lot of matches.
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
-matchoperator. I useSelect-Stringwhen I want to know in which file and on what line the pattern is matched.mklement0 commentedon Sep 7, 2018
Bruce Payette (@BrucePay):
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.
A primary benefit of the pipeline is memory-throttling.
You forfeit that benefit if you use the
-matchoperator and - to a lesser degree, depending on the number of matches - if you use(...).LineEspecially with large files,
Select-Stringis the tool of choice and-matchis 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.
powercode commentedon Sep 7, 2018
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.
iSazonov commentedon Sep 7, 2018
Please add reference on the issue.
iSazonov commentedon Sep 7, 2018
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
-Rawor-AsString. It is a question for PowerShell Committee.[-]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[/+]19 remaining items
SteveL-MSFT commentedon Nov 7, 2018
@PowerShell/powershell-committee reviewed this again. We are fine with adding a
-Rawswitch in a new parameter set to emit strings.[-]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[/+]Jawz84 commentedon Jun 14, 2019
I'd like to pick this one up.
vexx32 commentedon Jun 14, 2019
Have at it! 💖
Jawz84 commentedon Jun 14, 2019
Hey Joel Rain Sallow (/u/ta11ow) (@vexx32) tnx! I've put in a PR as you can see.