Skip to content

Commit 54c78db

Browse files
authored
#791 add profiling for some operations (#1108)
1 parent 015234e commit 54c78db

File tree

8 files changed

+46
-5
lines changed

8 files changed

+46
-5
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,9 @@ require'nvim-tree'.setup { -- BEGIN_DEFAULT_OPTS
206206
types = {
207207
all = false,
208208
config = false,
209+
copy_paste = false,
209210
git = false,
211+
profile = false,
210212
},
211213
},
212214
} -- END_DEFAULT_OPTS

doc/nvim-tree-lua.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,9 @@ function.
173173
types = {
174174
all = false,
175175
config = false,
176+
copy_paste = false,
176177
git = false,
178+
profile = false,
177179
},
178180
},
179181
} -- END_DEFAULT_OPTS
@@ -492,6 +494,10 @@ Here is a list of the options available in the setup call:
492494
type: `boolean`
493495
default: `false`
494496

497+
- |log.types.profile|: timing of some operations
498+
type: `boolean`
499+
default: `false`
500+
495501
- |log.types.config|: options and mappings, at startup
496502
type: `boolean`
497503
default: `false`

lua/nvim-tree.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,9 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
424424
types = {
425425
all = false,
426426
config = false,
427+
copy_paste = false,
427428
git = false,
429+
profile = false,
428430
},
429431
},
430432
} -- END_DEFAULT_OPTS

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local a = vim.api
22

3+
local log = require "nvim-tree.log"
34
local utils = require "nvim-tree.utils"
45
local core = require "nvim-tree.core"
56

@@ -28,6 +29,8 @@ function M.fn(name, with_open)
2829
end
2930

3031
function M.force_dirchange(foldername, with_open)
32+
local ps = log.profile_start("change dir %s", foldername)
33+
3134
if M.options.change_cwd and vim.tbl_isempty(vim.v.event) then
3235
if M.options.global then
3336
vim.cmd("cd " .. vim.fn.fnameescape(foldername))
@@ -41,6 +44,8 @@ function M.force_dirchange(foldername, with_open)
4144
else
4245
require("nvim-tree.renderer").draw()
4346
end
47+
48+
log.profile_end(ps, "change dir %s", foldername)
4449
end
4550

4651
function M.setup(options)

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
local log = require "nvim-tree.log"
12
local uv = vim.loop
23
local view = require "nvim-tree.view"
34
local utils = require "nvim-tree.utils"
@@ -16,6 +17,7 @@ function M.fn(fname)
1617
end
1718
running[fname] = true
1819

20+
local ps = log.profile_start("find file %s", fname)
1921
-- always match against the real path
2022
local fname_real = uv.fs_realpath(fname)
2123
if not fname_real then
@@ -68,6 +70,8 @@ function M.fn(fname)
6870
view.set_cursor { index, 0 }
6971
end
7072
running[fname] = false
73+
74+
log.profile_end(ps, "find file %s", fname)
7175
end
7276

7377
return M

lua/nvim-tree/git/runner.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ end
128128

129129
-- This module runs a git process, which will be killed if it takes more than timeout which defaults to 400ms
130130
function Runner.run(opts)
131+
local ps = log.profile_start("git job %s", opts.project_root)
132+
131133
local self = setmetatable({
132134
project_root = opts.project_root,
133135
list_untracked = opts.list_untracked,
@@ -140,6 +142,8 @@ function Runner.run(opts)
140142
self:_run_git_job()
141143
self:_wait()
142144

145+
log.profile_end(ps, "git job %s", opts.project_root)
146+
143147
if self.rc == -1 then
144148
log.line("git", "job timed out")
145149
elseif self.rc ~= 0 then

lua/nvim-tree/log.lua

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,26 @@ function M.raw(typ, fmt, ...)
1919
io.close(file)
2020
end
2121

22+
--- Write to log file via M.line
23+
--- START is prefixed
24+
--- @return reltime to pass to profile_end
25+
function M.profile_start(fmt, ...)
26+
M.line("profile", "START " .. (fmt or "???"), ...)
27+
return vim.fn.reltime()
28+
end
29+
30+
--- Write to log file via M.line
31+
--- END is prefixed and duration in seconds is suffixed
32+
--- @param start reltime returned from profile_start
33+
function M.profile_end(start, fmt, ...)
34+
local dur = vim.fn.reltimestr(vim.fn.reltime(start, vim.fn.reltime()))
35+
M.line("profile", "END " .. (fmt or "???") .. " " .. dur .. "s", ...)
36+
end
37+
2238
-- Write to log file via M.raw
2339
-- time and typ are prefixed and a trailing newline is added
2440
function M.line(typ, fmt, ...)
25-
if not M.path or not M.config.types[typ] and not M.config.types.all then
26-
return
27-
end
28-
29-
M.raw(typ, string.format("[%s] [%s] %s\n", os.date "%Y:%m:%d %H:%M:%S", typ, fmt), ...)
41+
M.raw(typ, string.format("[%s] [%s] %s\n", os.date "%Y-%m-%d %H:%M:%S", typ, fmt), ...)
3042
end
3143

3244
function M.setup(opts)

lua/nvim-tree/renderer/init.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
local log = require "nvim-tree.log"
12
local utils = require "nvim-tree.utils"
23
local view = require "nvim-tree.view"
34
local _padding = require "nvim-tree.renderer.padding"
@@ -238,6 +239,9 @@ function M.draw()
238239
if not core.get_explorer() or not bufnr or not api.nvim_buf_is_loaded(bufnr) then
239240
return
240241
end
242+
243+
local ps = log.profile_start "draw"
244+
241245
local cursor
242246
if view.is_visible() then
243247
cursor = api.nvim_win_get_cursor(view.get_winnr())
@@ -266,6 +270,8 @@ function M.draw()
266270
if cursor and #lines >= cursor[1] then
267271
api.nvim_win_set_cursor(view.get_winnr(), cursor)
268272
end
273+
274+
log.profile_end(ps, "draw")
269275
end
270276

271277
function M.render_hl(bufnr)

0 commit comments

Comments
 (0)