Skip to content

Commit f87d41c

Browse files
authored
perf(dap): cache rustc results (#881)
1 parent 9367b87 commit f87d41c

File tree

1 file changed

+23
-10
lines changed

1 file changed

+23
-10
lines changed

lua/rustaceanvim/dap.lua

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,30 +67,43 @@ local function get_cargo_args_from_runnables_args(runnable_args)
6767
return cargo_args
6868
end
6969

70-
---@param callback fun(commit_hash:string)
70+
---@type string
71+
local rustc_commit_hash
72+
73+
---@param callback fun(rustc_commit_hash:string)
7174
local function get_rustc_commit_hash(callback)
75+
if rustc_commit_hash then
76+
return callback(rustc_commit_hash)
77+
end
7278
vim.system({ 'rustc', '--version', '--verbose' }, nil, function(sc)
7379
---@cast sc vim.SystemCompleted
7480
local result = sc.stdout
7581
if sc.code ~= 0 or result == nil then
7682
return
7783
end
78-
local commit_hash = result:match('commit%-hash:%s+([^\n]+)')
79-
if not commit_hash then
80-
return
84+
rustc_commit_hash = result:match('commit%-hash:%s+([^\n]+)')
85+
if rustc_commit_hash then
86+
callback(rustc_commit_hash)
8187
end
82-
callback(commit_hash)
8388
end)
8489
end
8590

91+
---@type string
92+
local rustc_sysroot
93+
94+
---@param callback fun(rustc_sysroot:string)
8695
local function get_rustc_sysroot(callback)
96+
if rustc_sysroot then
97+
return callback(rustc_sysroot)
98+
end
8799
vim.system({ 'rustc', '--print', 'sysroot' }, nil, function(sc)
88100
---@cast sc vim.SystemCompleted
89101
local result = sc.stdout
90102
if sc.code ~= 0 or result == nil then
91103
return
92104
end
93-
callback((result:gsub('\n$', '')))
105+
rustc_sysroot = result:gsub('\n$', '')
106+
callback(rustc_sysroot)
94107
end)
95108
end
96109

@@ -132,10 +145,10 @@ local function generate_source_map(workspace_root)
132145
return
133146
end
134147
get_rustc_commit_hash(function(commit_hash)
135-
get_rustc_sysroot(function(rustc_sysroot)
148+
get_rustc_sysroot(function(sysroot)
136149
local src_path
137150
for _, src_dir in pairs { 'src', 'rustc-src' } do
138-
src_path = vim.fs.joinpath(rustc_sysroot, 'lib', 'rustlib', src_dir, 'rust')
151+
src_path = vim.fs.joinpath(sysroot, 'lib', 'rustlib', src_dir, 'rust')
139152
if vim.uv.fs_stat(src_path) then
140153
break
141154
end
@@ -160,8 +173,8 @@ local function get_lldb_commands(workspace_root)
160173
if not workspace_root or init_commands[workspace_root] then
161174
return
162175
end
163-
get_rustc_sysroot(function(rustc_sysroot)
164-
local script = vim.fs.joinpath(rustc_sysroot, 'lib', 'rustlib', 'etc', 'lldb_lookup.py')
176+
get_rustc_sysroot(function(sysroot)
177+
local script = vim.fs.joinpath(sysroot, 'lib', 'rustlib', 'etc', 'lldb_lookup.py')
165178
if not vim.uv.fs_stat(script) then
166179
return
167180
end

0 commit comments

Comments
 (0)