Skip to content

Commit b712b82

Browse files
authored
fix(#1961): harden profiling functions (#1986)
1 parent 02fdc26 commit b712b82

File tree

11 files changed

+44
-46
lines changed

11 files changed

+44
-46
lines changed

lua/nvim-tree/actions/finders/find-file.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function M.fn(fname)
2828
end
2929
running[fname_real] = true
3030

31-
local ps = log.profile_start("find file %s", fname_real)
31+
local profile = log.profile_start("find file %s", fname_real)
3232

3333
-- we cannot wait for watchers
3434
reload.refresh_nodes_for_path(vim.fn.fnamemodify(fname_real, ":h"))
@@ -71,7 +71,7 @@ function M.fn(fname)
7171

7272
running[fname_real] = false
7373

74-
log.profile_end(ps, "find file %s", fname_real)
74+
log.profile_end(profile)
7575
end
7676

7777
return M

lua/nvim-tree/actions/root/change-dir.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ end
5656

5757
local function add_profiling_to(f)
5858
return function(foldername, should_open_view)
59-
local ps = log.profile_start("change dir %s", foldername)
59+
local profile = log.profile_start("change dir %s", foldername)
6060
f(foldername, should_open_view)
61-
log.profile_end(ps, "change dir %s", foldername)
61+
log.profile_end(profile)
6262
end
6363
end
6464

lua/nvim-tree/core.lua

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ TreeExplorer = nil
1010
local first_init_done = false
1111

1212
function M.init(foldername)
13-
local pn = string.format("core init %s", foldername)
14-
local ps = log.profile_start(pn)
13+
local profile = log.profile_start("core init %s", foldername)
1514

1615
if TreeExplorer then
1716
TreeExplorer:destroy()
@@ -21,7 +20,7 @@ function M.init(foldername)
2120
events._dispatch_ready()
2221
first_init_done = true
2322
end
24-
log.profile_end(ps, pn)
23+
log.profile_end(profile)
2524
end
2625

2726
function M.get_explorer()

lua/nvim-tree/diagnostics.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ function M.update()
9090
return
9191
end
9292
utils.debounce("diagnostics", M.debounce_delay, function()
93-
local ps = log.profile_start "diagnostics update"
93+
local profile = log.profile_start "diagnostics update"
9494
log.line("diagnostics", "update")
9595

9696
local buffer_severity
@@ -130,7 +130,7 @@ function M.update()
130130
end
131131
end
132132
end
133-
log.profile_end(ps, "diagnostics update")
133+
log.profile_end(profile)
134134
end)
135135
end
136136

lua/nvim-tree/explorer/explore.lua

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ local function populate_children(handle, cwd, node, git_status)
2626

2727
local abs = utils.path_join { cwd, name }
2828

29-
local pn = string.format("explore populate_children %s", abs)
30-
local ps = log.profile_start(pn)
29+
local profile = log.profile_start("explore populate_children %s", abs)
3130

3231
t = get_type_from(t, abs)
3332
if
@@ -53,7 +52,7 @@ local function populate_children(handle, cwd, node, git_status)
5352
end
5453
end
5554

56-
log.profile_end(ps, pn)
55+
log.profile_end(profile)
5756
end
5857
end
5958

@@ -64,8 +63,7 @@ function M.explore(node, status)
6463
return
6564
end
6665

67-
local pn = string.format("explore init %s", node.absolute_path)
68-
local ps = log.profile_start(pn)
66+
local profile = log.profile_start("explore init %s", node.absolute_path)
6967

7068
populate_children(handle, cwd, node, status)
7169

@@ -76,14 +74,14 @@ function M.explore(node, status)
7674
local ns = M.explore(child_folder_only, status)
7775
node.nodes = ns or {}
7876

79-
log.profile_end(ps, pn)
77+
log.profile_end(profile)
8078
return ns
8179
end
8280

8381
sorters.merge_sort(node.nodes, sorters.node_comparator)
8482
live_filter.apply_filter(node)
8583

86-
log.profile_end(ps, pn)
84+
log.profile_end(profile)
8785
return node.nodes
8886
end
8987

lua/nvim-tree/explorer/reload.lua

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function M.reload(node, git_status, unloaded_bufnr)
4141
return
4242
end
4343

44-
local ps = log.profile_start("reload %s", node.absolute_path)
44+
local profile = log.profile_start("reload %s", node.absolute_path)
4545

4646
local filter_status = filters.prepare(git_status, unloaded_bufnr)
4747

@@ -130,13 +130,13 @@ function M.reload(node, git_status, unloaded_bufnr)
130130
node.group_next = child_folder_only
131131
local ns = M.reload(child_folder_only, git_status)
132132
node.nodes = ns or {}
133-
log.profile_end(ps, "reload %s", node.absolute_path)
133+
log.profile_end(profile)
134134
return ns
135135
end
136136

137137
sorters.merge_sort(node.nodes, sorters.node_comparator)
138138
live_filter.apply_filter(node)
139-
log.profile_end(ps, "reload %s", node.absolute_path)
139+
log.profile_end(profile)
140140
return node.nodes
141141
end
142142

@@ -164,8 +164,7 @@ function M.refresh_nodes_for_path(path)
164164
return
165165
end
166166

167-
local pn = string.format("refresh_nodes_for_path %s", path)
168-
local ps = log.profile_start(pn)
167+
local profile = log.profile_start("refresh_nodes_for_path %s", path)
169168

170169
NodeIterator.builder({ explorer })
171170
:hidden()
@@ -186,7 +185,7 @@ function M.refresh_nodes_for_path(path)
186185
end)
187186
:iterate()
188187

189-
log.profile_end(ps, pn)
188+
log.profile_end(profile)
190189
end
191190

192191
function M.setup(opts)

lua/nvim-tree/git/runner.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ end
140140

141141
-- This module runs a git process, which will be killed if it takes more than timeout which defaults to 400ms
142142
function Runner.run(opts)
143-
local ps = log.profile_start("git job %s %s", opts.project_root, opts.path)
143+
local profile = log.profile_start("git job %s %s", opts.project_root, opts.path)
144144

145145
local self = setmetatable({
146146
project_root = opts.project_root,
@@ -155,7 +155,7 @@ function Runner.run(opts)
155155
self:_run_git_job()
156156
self:_wait()
157157

158-
log.profile_end(ps, "git job %s %s", opts.project_root, opts.path)
158+
log.profile_end(profile)
159159

160160
if self.rc == -1 then
161161
log.line("git", "job timed out %s %s", opts.project_root, opts.path)

lua/nvim-tree/git/utils.lua

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ local log = require "nvim-tree.log"
44
local has_cygpath = vim.fn.executable "cygpath" == 1
55

66
function M.get_toplevel(cwd)
7-
local ps = log.profile_start("git toplevel %s", cwd)
7+
local profile = log.profile_start("git toplevel %s", cwd)
88

99
local cmd = { "git", "-C", cwd, "rev-parse", "--show-toplevel" }
1010
log.line("git", "%s", vim.inspect(cmd))
1111

1212
local toplevel = vim.fn.system(cmd)
1313

1414
log.raw("git", toplevel)
15-
log.profile_end(ps, "git toplevel %s", cwd)
15+
log.profile_end(profile)
1616

1717
if vim.v.shell_error ~= 0 or not toplevel or #toplevel == 0 or toplevel:match "fatal" then
1818
return nil
@@ -41,15 +41,15 @@ function M.should_show_untracked(cwd)
4141
return untracked[cwd]
4242
end
4343

44-
local ps = log.profile_start("git untracked %s", cwd)
44+
local profile = log.profile_start("git untracked %s", cwd)
4545

4646
local cmd = { "git", "-C", cwd, "config", "status.showUntrackedFiles" }
4747
log.line("git", vim.inspect(cmd))
4848

4949
local has_untracked = vim.fn.system(cmd)
5050

5151
log.raw("git", has_untracked)
52-
log.profile_end(ps, "git untracked %s", cwd)
52+
log.profile_end(profile)
5353

5454
untracked[cwd] = vim.trim(has_untracked) ~= "no"
5555
return untracked[cwd]

lua/nvim-tree/log.lua

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,32 @@ function M.raw(typ, fmt, ...)
2121
end
2222
end
2323

24+
---@class Profile
25+
---@field start number nanos
26+
---@field tag string
27+
2428
--- Write profile start to log file
2529
--- START is prefixed
2630
--- @param fmt string for string.format
2731
--- @vararg any arguments for string.format
28-
--- @return number nanos to pass to profile_end
32+
--- @return Profile to pass to profile_end
2933
function M.profile_start(fmt, ...)
34+
local profile = {}
3035
if M.enabled "profile" then
31-
M.line("profile", "START " .. (fmt or "???"), ...)
32-
return vim.loop.hrtime()
33-
else
34-
return 0
36+
profile.start = vim.loop.hrtime()
37+
profile.tag = string.format((fmt or "???"), ...)
38+
M.line("profile", "START %s", profile.tag)
3539
end
40+
return profile
3641
end
3742

3843
--- Write profile end to log file
3944
--- END is prefixed and duration in seconds is suffixed
40-
--- @param start number nanos returned from profile_start
41-
--- @param fmt string for string.format
42-
--- @vararg any arguments for string.format
43-
function M.profile_end(start, fmt, ...)
44-
if M.enabled "profile" then
45-
local millis = start and math.modf((vim.loop.hrtime() - start) / 1000000) or -1
46-
M.line("profile", "END " .. (fmt or "???") .. " " .. millis .. "ms", ...)
45+
--- @param profile Profile returned from profile_start
46+
function M.profile_end(profile)
47+
if M.enabled "profile" and type(profile) == "table" then
48+
local millis = profile.start and math.modf((vim.loop.hrtime() - profile.start) / 1000000) or -1
49+
M.line("profile", "END %s %dms", profile.tag or "", millis)
4750
end
4851
end
4952

@@ -54,7 +57,7 @@ end
5457
--- @vararg any arguments for string.format
5558
function M.line(typ, fmt, ...)
5659
if M.enabled(typ) then
57-
M.raw(typ, string.format("[%s] [%s] %s\n", os.date "%Y-%m-%d %H:%M:%S", typ, fmt), ...)
60+
M.raw(typ, string.format("[%s] [%s] %s\n", os.date "%Y-%m-%d %H:%M:%S", typ, (fmt or "???")), ...)
5861
end
5962
end
6063

lua/nvim-tree/renderer/init.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ function M.draw(unloaded_bufnr)
5555
return
5656
end
5757

58-
local ps = log.profile_start "draw"
58+
local profile = log.profile_start "draw"
5959

6060
local cursor = vim.api.nvim_win_get_cursor(view.get_winnr())
6161
icon_component.reset_config()
@@ -100,7 +100,7 @@ function M.draw(unloaded_bufnr)
100100

101101
view.grow_from_content()
102102

103-
log.profile_end(ps, "draw")
103+
log.profile_end(profile)
104104
end
105105

106106
function M.setup(opts)

lua/nvim-tree/view.lua

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,7 @@ function M.open(options)
238238
return
239239
end
240240

241-
local pn = string.format "view open"
242-
local ps = log.profile_start(pn)
241+
local profile = log.profile_start "view open"
243242

244243
create_buffer()
245244
open_window()
@@ -251,7 +250,7 @@ function M.open(options)
251250
end
252251
events._dispatch_on_tree_open()
253252

254-
log.profile_end(ps, pn)
253+
log.profile_end(profile)
255254
end
256255

257256
local function grow()

0 commit comments

Comments
 (0)