Skip to content

Performance problem: -Name slows down Get-ChildItem -Recurse #9119

Closed

Description

-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, -Name slows 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.

Environment data

PowerShell Core 6.2.0-preview.4

Activity

  1. added
    Issue-Question
    on Mar 11, 2019
  2. iSazonov commented on Mar 12, 2019

    @iSazonov
    Collaborator

    I expect #8995 will address this.

  3. added
    WG-Engine-Performance
    on Mar 12, 2019
  4. added
    WG-Engine-Performance
    and removed
    WG-Engine-Performance
    on Mar 23, 2019
  5. ericdching commented on Jan 2, 2020

    @ericdching

    It looks like this issue is more pronounced in Powershell 7.0.

    Running the following command from a nodeJS project:

    Get-ChildItem -Recurse -Name
    

    Returns 12886 files.

    PowerShell 6.2.3

    Without -Name

    IsRunning Elapsed          ElapsedMilliseconds ElapsedTicks
    --------- -------          ------------------- ------------
        False 00:00:00.7503959                 750    750395941
    

    With -Name

    IsRunning Elapsed          ElapsedMilliseconds ElapsedTicks
    --------- -------          ------------------- ------------
        False 00:00:01.3749722                1374   1374972263
    

    PowerShell 7.0.0-rc.1

    Without -Name

    IsRunning Elapsed          ElapsedMilliseconds ElapsedTicks
    --------- -------          ------------------- ------------
        False 00:00:00.6832990                 683    683299055
    

    With -Name

    IsRunning Elapsed          ElapsedMilliseconds ElapsedTicks
    --------- -------          ------------------- ------------
        False 00:00:09.0078861                9007   9007886141
    

    I see similar results for both macOS and Windows.

  6. iSazonov commented on Jan 3, 2020

    @iSazonov
    Collaborator

    I guess this perf regressions comes from #9250
    If previously I thought Kaspersky AV slow down file operations, now I tested with Microsoft AV.
    image

    /cc Steve Lee (@SteveL-MSFT) Dongbo Wang (@daxian-dbw)

  7. SteveL-MSFT commented on Jan 7, 2020

    @SteveL-MSFT
    Member

    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:

    PS> (Measure-Command { dir . -Recurse }).TotalMilliseconds
    277.7461
    [280.54ms] /Users/steve/repos/PowerShell [origin/master]
    PS> (Measure-Command { dir . -Recurse -Name }).TotalMilliseconds
    1397.9301
    [1.4s] /Users/steve/repos/PowerShell [origin/master]
    PS> (Measure-Command { dir . -Recurse | select fullname }).TotalMilliseconds
    449.8212

    Seems like -Name could do that equivalent and would be faster. Let me play with this.

  8. SteveL-MSFT commented on Jan 7, 2020

    @SteveL-MSFT
    Member

    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.

  9. added
    Up-for-Grabs
    on Jan 7, 2020
  10. iSazonov commented on Jan 8, 2020

    @iSazonov
    Collaborator

    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.

  11. SteveL-MSFT commented on Jan 8, 2020

    @SteveL-MSFT
    Member

    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.

  12. HumanEquivalentUnit commented on Jun 25, 2023

    @HumanEquivalentUnit
    Contributor

    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.

    • -Name ends up doing more work.

    This code may be involved:

  13. added
    Resolution-No Activity
    on Dec 23, 2023
  14. microsoft-github-policy-service commented on Dec 23, 2023

    @microsoft-github-policy-service
    Contributor

    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.

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

Metadata

Metadata

Labels

Issue-QuestionResolution-No ActivityUp-for-GrabsWG-CmdletsWG-Engine-Performance

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions