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
Performance problem: -Name slows down Get-ChildItem -Recurse #9119
Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-No ActivityIssue has had no activity for 6 months or moreUp-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Cmdletsgeneral cmdlet issuesWG-Engine-Performancecore PowerShell engine, interpreter, and runtime performance
-Name is a convenient switch to only output item names (without -Recurse) or relative paths (with -Recurse).
Given that only string instances rather than System.IO.File/DirectoryInfo instances are output, one would expect -Name to speed things up (or at least not to slow things down); however, with -Recurse present, -Nameslows down the command noticeably.
Steps to reproduce (on Windows)
# Warm up the cache$null=Get-ChildItem-Recurse 'C:\Program Files'# Without -Name
(Measure-Command { Get-ChildItem-Recurse 'C:\Program Files' }).TotalSeconds
# With -Name
(Measure-Command { Get-ChildItem-Name -Recurse 'C:\Program Files' }).TotalSeconds
Expected behavior
The command with -Name should be faster (or at least not slower).
Actual behavior
Sample timings:
1.544098 # without -Name
4.5759553 # with -Name
That is, adding -Name made the command about 3 times slower.
Ilya (@iSazonov) that method should be called in all cases, and not just when -Name is used. But if you're suggesting the difference between WinPS and PSCore is due to that, then I would agree. Looking at the code, it special cases -Recurse with -Name and handles it itself. So instead of relying on a .NET API, it goes though much more code to gather objects at each level and recurse. In fact, this makes much slower than doing a Select-Object for equivalent output:
Seems like we should expose an overload to GetChildNames() to accept recursion. Then the FileSystemProvider can implement this and simply use EnumerateFileSystemEntries() to quickly return just the names. In SessionStateContainer.cs, it would try the overload first, but not all providers implement it and fallback to current behavior. Since this is not a mainline issue, not going to implement this for now.
that method should be called in all cases, and not just when -Name is used.
Steve Lee (@SteveL-MSFT) For dir -Recurse it is called 3 times for root directory, for dir -Recurse -Name - 3 times for every subdirectory for every level and 1 time for every file.
Ilya (@iSazonov) yes, because when -Recurse and -Name is used together, the recursion is done outside of the FileSystemProvider so each call into the provider results in a call to GetCorrectCasedPath(). However, there's actually a lot of other work happening because of the manual recursion than just that API call. The FileSystemProvider can leverage the EnumerateFileSystemEntries() to just get the names, but the current interface doesn't expose such a call into the provider.
I found -Name makes things slower; Jordan Borean (@jborean93) looked into it and suggested this possible cause:
The strings are decorated with .PSPath, .PSParentPath, .PS... properties.
Normally a FileInfo is returned from the FileSystem Provider, the Provider Base adds these decorators.
With -Name, a FileInfo is made but only the name string is returned, the Provider Base then resolves the string to a path and another FileInfo is made to add these extended properties.
This issue has not had any activity in 6 months, if there is no further activity in 7 days, the issue will be closed automatically.
Activity in this case refers only to comments on the issue. If the issue is closed and you are the author, you can re-open the issue using the button below. Please add more information to be considered during retriage. If you are not the author but the issue is impacting you after it has been closed, please submit a new issue with updated details and a link to this issue and the original.
Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-No ActivityIssue has had no activity for 6 months or moreUp-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Cmdletsgeneral cmdlet issuesWG-Engine-Performancecore PowerShell engine, interpreter, and runtime performance
Activity
iSazonov commentedon Mar 12, 2019
I expect #8995 will address this.
ericdching commentedon Jan 2, 2020
It looks like this issue is more pronounced in Powershell 7.0.
Running the following command from a nodeJS project:
Returns 12886 files.
PowerShell 6.2.3
Without -Name
With -Name
PowerShell 7.0.0-rc.1
Without -Name
With -Name
I see similar results for both macOS and Windows.
iSazonov commentedon Jan 3, 2020
I guess this perf regressions comes from #9250

If previously I thought Kaspersky AV slow down file operations, now I tested with Microsoft AV.
/cc Steve Lee (@SteveL-MSFT) Dongbo Wang (@daxian-dbw)
SteveL-MSFT commentedon Jan 7, 2020
Ilya (@iSazonov) that method should be called in all cases, and not just when
-Nameis used. But if you're suggesting the difference between WinPS and PSCore is due to that, then I would agree. Looking at the code, it special cases-Recursewith-Nameand handles it itself. So instead of relying on a .NET API, it goes though much more code to gather objects at each level and recurse. In fact, this makes much slower than doing aSelect-Objectfor equivalent output:Seems like
-Namecould do that equivalent and would be faster. Let me play with this.SteveL-MSFT commentedon Jan 7, 2020
Seems like we should expose an overload to
GetChildNames()to accept recursion. Then the FileSystemProvider can implement this and simply useEnumerateFileSystemEntries()to quickly return just the names. InSessionStateContainer.cs, it would try the overload first, but not all providers implement it and fallback to current behavior. Since this is not a mainline issue, not going to implement this for now.iSazonov commentedon Jan 8, 2020
Steve Lee (@SteveL-MSFT) For
dir -Recurseit is called 3 times for root directory, fordir -Recurse -Name- 3 times for every subdirectory for every level and 1 time for every file.SteveL-MSFT commentedon Jan 8, 2020
Ilya (@iSazonov) yes, because when
-Recurseand-Nameis used together, the recursion is done outside of theFileSystemProviderso each call into the provider results in a call toGetCorrectCasedPath(). However, there's actually a lot of other work happening because of the manual recursion than just that API call. TheFileSystemProvidercan leverage theEnumerateFileSystemEntries()to just get the names, but the current interface doesn't expose such a call into the provider.HumanEquivalentUnit commentedon Jun 25, 2023
I found
-Namemakes things slower; Jordan Borean (@jborean93) looked into it and suggested this possible cause:The strings are decorated with .PSPath, .PSParentPath, .PS... properties.
Normally a FileInfo is returned from the FileSystem Provider, the Provider Base adds these decorators.
With
-Name, a FileInfo is made but only the name string is returned, the Provider Base then resolves the string to a path and another FileInfo is made to add these extended properties.-Nameends up doing more work.This code may be involved:
microsoft-github-policy-service commentedon Dec 23, 2023
This issue has not had any activity in 6 months, if there is no further activity in 7 days, the issue will be closed automatically.
Activity in this case refers only to comments on the issue. If the issue is closed and you are the author, you can re-open the issue using the button below. Please add more information to be considered during retriage. If you are not the author but the issue is impacting you after it has been closed, please submit a new issue with updated details and a link to this issue and the original.