From b63e08623e45658cd2c8c8cce04b9da804e825ca Mon Sep 17 00:00:00 2001 From: Ian Butterworth Date: Fri, 1 Mar 2024 18:04:35 -0500 Subject: [PATCH] use `_readdirx` for `walkdir` (#53545) On a M2 Mac there is some benefit, but assumed to be much greater on slower filesystems. ``` # master julia> @btime collect(walkdir(expanduser("~/Downloads"))); 380.086 ms (310696 allocations: 25.29 MiB) # This PR julia> @btime collect(walkdir(expanduser("~/Downloads"))); 289.747 ms (103300 allocations: 7.50 MiB) ``` The implementations appear to produce the same result ``` julia> collect(walkdir(expanduser("~/Downloads"))) == collect(walkdirx(expanduser("~/Downloads"))) true ``` (cherry picked from commit 2b9595601e6e81b0c81376d7942497af22e222dd) --- base/file.jl | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/base/file.jl b/base/file.jl index 7f5caaaf96c6b..cb69cc5e0ee44 100644 --- a/base/file.jl +++ b/base/file.jl @@ -1073,18 +1073,16 @@ function walkdir(root; topdown=true, follow_symlinks=false, onerror=throw) end return end - content = tryf(readdir, root) - content === nothing && return - dirs = Vector{eltype(content)}() - files = Vector{eltype(content)}() - for name in content - path = joinpath(root, name) - + entries = tryf(_readdirx, root) + entries === nothing && return + dirs = Vector{String}() + files = Vector{String}() + for entry in entries # If we're not following symlinks, then treat all symlinks as files - if (!follow_symlinks && something(tryf(islink, path), true)) || !something(tryf(isdir, path), false) - push!(files, name) + if (!follow_symlinks && something(tryf(islink, entry), true)) || !something(tryf(isdir, entry), false) + push!(files, entry.name) else - push!(dirs, name) + push!(dirs, entry.name) end end