Skip to content

Performance of ls with recursive globs scales extremely poorly with search depth #15218

Open

Description

@Petrusion

Describe the bug

I think there is a bug somewhere in ls which makes it scale much worse than glob. This applies to both time and memory complexity. Of course, it is to be expected that ls **/* will not be as fast as glob **/* but what I am talking about is somewhere in the ballpark of linear vs exponential complexity.

I noticed that I can never use ls to search through my .config directory because ls ~/.config/**/* would just take forever while taking all of my RAM. Interestingly, I could use glob ~/.config/**/* | each { ls --directory $in } | flatten (--directory means only return results for the single file/dir/symlink) and that would return the same output ls **/* should while running for just a second (as opposed to seemingly forever).

(Also, while ls **/* is evaluating, it doesn't respont to cancellation via ctrl-c and has to be killed in some other ways.)

When I originally started writing this issue I thought that ls scales poorly with the amount of files in a directory, but that turned out not to be the case when I was gathering data. Directory file count didn't have nowhere near this significant impact on performance, but it turned out search depth does!

To support my claims, below is a table illustrating the performance of glob vs ls. To collect the data for the table, following was ran from my home directory:

  • timeit { ls -a * } and timeit { glob * } for depth 1
  • timeit { ls -a */* } and timeit { glob */* } for depth 2
  • timeit { ls -a */*/* } and timeit { glob */*/* } for depth 3
  • ... and so on ...
╭───────┬───────────┬────────────╮
│ depth │   glob    │     ls     │
├───────┼───────────┼────────────┤
│     1 │ 0.78 ms   │ 0.44 ms    │
│     2 │ 4.45 ms   │ 1.51 ms    │
│     3 │ 8.37 ms   │ 11.58 ms   │
│     4 │ 34.19 ms  │ 29.08 ms   │
│     5 │ 142.84 ms │ 175.30 ms  │
│     6 │ 221.08 ms │ 696.22 ms  │
│     7 │ 398.10 ms │ 999.20 ms  │
│     8 │ 376.17 ms │ 1380.83 ms │
│     9 │ 0.41 sec  │ 3.44 sec   │
│    10 │ 0.61 sec  │ 5.72 sec   │
│    11 │ 0.64 sec  │ 21.44 sec  │
│    12 │ 0.73 sec  │ 221.85 sec │
╰───────┴───────────┴────────────╯

(at depth 12, ls needed over 16GB of RAM to finish)

How to reproduce

  1. cd into a directory with lots of files and with deep subdirectories (like the home directory, or ~/.config)
  2. see that glob **/* finishes fast (28ms for my ~/.config)
  3. see that ls **/* will not finish even after several minutes, will not be cancelled by ctrl-c, and will use tens of GB of memory if allowed to continue (ls ~/.config/**/* used 11GB before I killed the process, even my entire ~/.config contains only 900MB of data)
  4. see that glob **/* | each { ls --directory $in } | flatten finishes reasonably quickly (908ms for my ~/.config), so the slowdown of ls cannot possibly be the time it takes to read the additional metadata because the same metadata is read using this command

Expected behavior

I expect ls -a **/* to run reasonably fast compared to glob -a **/*. Some slowdown is expected compared to glob, but not an exponential decrease in speed.

In other words, I expect these two commands to exhibit the same asymptotic time and memory complexity with respect to the depth of the search space.

Configuration

Tested that the issue is present with both "stable" (nu 0.100) and "unstable" (nu 0.102) nix packages:

| key                | value                                                             |
| ------------------ | ----------------------------------------------------------------- |
| version            | 0.100.0                                                           |
| major              | 0                                                                 |
| minor              | 100                                                               |
| patch              | 0                                                                 |
| branch             |                                                                   |
| commit_hash        |                                                                   |
| build_os           | linux-x86_64                                                      |
| build_target       | x86_64-unknown-linux-gnu                                          |
| rust_version       | rustc 1.82.0 (f6e511eec 2024-10-15) (built from a source tarball) |
| cargo_version      | cargo 1.82.0 (8f40fc59f 2024-08-21)                               |
| build_time         | 1980-01-01 00:00:00 +00:00                                        |
| build_rust_channel | release                                                           |
| allocator          | mimalloc                                                          |
| features           | default, sqlite, trash                                            |
| installed_plugins  |                                                                   |

| key                | value                                                             |
| ------------------ | ----------------------------------------------------------------- |
| version            | 0.102.0                                                           |
| major              | 0                                                                 |
| minor              | 102                                                               |
| patch              | 0                                                                 |
| branch             |                                                                   |
| commit_hash        |                                                                   |
| build_os           | linux-x86_64                                                      |
| build_target       | x86_64-unknown-linux-gnu                                          |
| rust_version       | rustc 1.84.1 (e71f9a9a9 2025-01-27) (built from a source tarball) |
| cargo_version      | cargo 1.84.0 (66221abde 2024-11-19)                               |
| build_time         | 1980-01-01 00:00:00 +00:00                                        |
| build_rust_channel | release                                                           |
| allocator          | mimalloc                                                          |
| features           | default, sqlite, trash                                            |
| installed_plugins  |                                                                   |

Additionally, the issue isn't happening inside of my Windows VM.

Activity

  1. added
    status:needs-triage
    on Feb 28, 2025
  2. added
    performance
    A:glob
    on Feb 28, 2025
  3. 132ikl commented on Feb 28, 2025

    @132ikl
    Member

    Thanks for the benchmarks. We're interested in replacing our current glob engine (nu-glob), and the glob engine used in the glob command (wax) with one based on devyn's glob_experiment which should hopefully be much faster (although currently it is not).

    (Also, while ls */ is evaluating, it doesn't respont to cancellation via ctrl-c and has to be killed in some other ways.)

    This was actually just fixed today by #15140 😄

    For future reference, I put together a little procedure to create a similar output to your issue for future reference:

    mut benchmarks = 1..12 | wrap depth
    $benchmarks = $benchmarks | insert ls {|row| timeit { ls (make-pattern $row.depth | into glob) } }
    $benchmarks = $benchmarks | insert glob {|row| timeit { glob (make-pattern $row.depth) }
  4. removed
    status:needs-triage
    on Feb 28, 2025
  5. fdncred commented on Feb 28, 2025

    @fdncred
    Contributor

    You might want to take a look at a prototype command I built as a plugin called nu_plugin_jwalk. It's pretty fast and streams output as well. You can also control a variety of parameters. However, it walks a path and doesn't glob, meaning, if you want to filter you have to do that in a pipeline like jwalk -x 5 -t 2 . | each {|r| if ($r ends-with .toml) { $r }}. I'd be interested how it compares in your benchmark.

    ❯ jwalk --help
    View jwalk results of walking the path.
    
    Usage:
      > jwalk {flags} <path>
    
    Flags:
      -h, --help: Display the help message for this command
      -v, --verbose: run in verbose mode with multi-column output
      -s, --sort: sort by file name
      -c, --custom: custom hard-coded walker with process_read_dir
      -k, --skip-hidden: skip hidden files
      -f, --follow-links: follow symbolic links
      -m, --min-depth <int>: minimum depth to search
      -x, --max-depth <int>: maximum depth to search
      -t, --threads <int>: number of rayon threads to use
      -d, --debug: show internal settings and performance metrics
    
    Parameters:
      path <string>: path to jwalk
    
    Examples:
      Walk the process working directory with 2 threads and max depth of 1
      > jwalk --max-depth 1 --threads 2 (pwd)
    
      Walk the process working directory with 2 threads and max depth of 1 using verbose
      > jwalk --verbose --max-depth 1 --threads 2 (pwd)
  6. kronberger-droid commented on Nov 14, 2026

    @kronberger-droid
    Contributor

    On a tree without symlinks ls **/* and glob **/* scale the same on 0.117.
    The blowup shows up with directory symlinks: ls follows them, through loops up to the ELOOP limit, while glob doesn't.
    With a single ln -s .. s/a/b/up, glob **/* gives 5 entries and ls -a **/* gives 84, going about 40 levels deep.
    That's the same root as #8736.

    AI:X EXECUTE

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

    A:globperformance

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions