Repository navigation
Description
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 * }andtimeit { glob * }for depth 1timeit { ls -a */* }andtimeit { glob */* }for depth 2timeit { ls -a */*/* }andtimeit { 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
cdinto a directory with lots of files and with deep subdirectories (like the home directory, or~/.config)- see that
glob **/*finishes fast (28ms for my~/.config) - 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~/.configcontains only 900MB of data) - see that
glob **/* | each { ls --directory $in } | flattenfinishes reasonably quickly (908ms for my~/.config), so the slowdown oflscannot 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
132ikl commentedon Feb 28, 2025
Thanks for the benchmarks. We're interested in replacing our current glob engine (
nu-glob), and the glob engine used in theglobcommand (wax) with one based on devyn's glob_experiment which should hopefully be much faster (although currently it is not).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:
fdncred commentedon Feb 28, 2025
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.kronberger-droid commentedon Nov 14, 2026
On a tree without symlinks
ls **/*andglob **/*scale the same on 0.117.The blowup shows up with directory symlinks:
lsfollows them, through loops up to theELOOPlimit, whileglobdoesn't.With a single
ln -s .. s/a/b/up,glob **/*gives 5 entries andls -a **/*gives 84, going about 40 levels deep.That's the same root as #8736.
fix(glob): prevent directory symlink cycles during traversal (#19622)