Skip to content

Commit

Permalink
[fuzzydir] optimize path traversal on Windows (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
sibwaf committed Mar 23, 2023
1 parent 3285c0b commit dca7f61
Showing 1 changed file with 51 additions and 2 deletions.
53 changes: 51 additions & 2 deletions fuzzydir.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,55 @@ function normalize(path)
return path
end

function call_command(command)
local process = mp.command_native({
name = "subprocess",
playback_only = false,
capture_stdout = true,
args = command,
})

if process.status ~= 0 then
return nil
end

local result = {}
for line in string.gmatch(process.stdout, "([^\r\n]+)") do
table.insert(result, line)
end
return result
end

-- Platform-dependent optimization

local powershell_version = call_command({
"powershell.exe",
"-NoProfile",
"-Command",
"$Host.Version.Major",
})
if powershell_version ~= nil then
powershell_version = tonumber(powershell_version[1])
end
if powershell_version == nil then
powershell_version = -1
end

function fast_readdir(path)
if powershell_version >= 3 then
return call_command({
"powershell.exe",
"-NoProfile",
"-Command",
"& { Get-ChildItem -LiteralPath FileSystem::\"" .. path .. "\" -Directory | foreach { $_.Name } }",
})
end

return utils.readdir(path, "dirs")
end

-- Platform-dependent optimization end

function traverse(search_path, current_path, level, cache)
if level > max_search_depth then
return {}
Expand All @@ -100,7 +149,7 @@ function traverse(search_path, current_path, level, cache)

local result = {}

local discovered_paths = utils.readdir(full_path, "dirs")
local discovered_paths = fast_readdir(full_path)
if discovered_paths == nil then
-- noop
elseif discovery_threshold > 0 and #discovered_paths > discovery_threshold then
Expand All @@ -115,7 +164,7 @@ function traverse(search_path, current_path, level, cache)
end

cache[full_path] = result


return result
end

Expand Down

0 comments on commit dca7f61

Please sign in to comment.