Skip to content

#791 add profiling for some operations #1108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,9 @@ require'nvim-tree'.setup { -- BEGIN_DEFAULT_OPTS
types = {
all = false,
config = false,
copy_paste = false,
git = false,
profile = false,
},
},
} -- END_DEFAULT_OPTS
Expand Down
6 changes: 6 additions & 0 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,9 @@ function.
types = {
all = false,
config = false,
copy_paste = false,
git = false,
profile = false,
},
},
} -- END_DEFAULT_OPTS
Expand Down Expand Up @@ -492,6 +494,10 @@ Here is a list of the options available in the setup call:
type: `boolean`
default: `false`

- |log.types.profile|: timing of some operations
type: `boolean`
default: `false`

- |log.types.config|: options and mappings, at startup
type: `boolean`
default: `false`
Expand Down
2 changes: 2 additions & 0 deletions lua/nvim-tree.lua
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,9 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
types = {
all = false,
config = false,
copy_paste = false,
git = false,
profile = false,
},
},
} -- END_DEFAULT_OPTS
Expand Down
5 changes: 5 additions & 0 deletions lua/nvim-tree/actions/change-dir.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local a = vim.api

local log = require "nvim-tree.log"
local utils = require "nvim-tree.utils"
local core = require "nvim-tree.core"

Expand Down Expand Up @@ -28,6 +29,8 @@ function M.fn(name, with_open)
end

function M.force_dirchange(foldername, with_open)
local ps = log.profile_start("change dir %s", foldername)

if M.options.change_cwd and vim.tbl_isempty(vim.v.event) then
if M.options.global then
vim.cmd("cd " .. vim.fn.fnameescape(foldername))
Expand All @@ -41,6 +44,8 @@ function M.force_dirchange(foldername, with_open)
else
require("nvim-tree.renderer").draw()
end

log.profile_end(ps, "change dir %s", foldername)
end

function M.setup(options)
Expand Down
4 changes: 4 additions & 0 deletions lua/nvim-tree/actions/find-file.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local log = require "nvim-tree.log"
local uv = vim.loop
local view = require "nvim-tree.view"
local utils = require "nvim-tree.utils"
Expand All @@ -16,6 +17,7 @@ function M.fn(fname)
end
running[fname] = true

local ps = log.profile_start("find file %s", fname)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is being invoked (with draw) twice when

		update_focused_file = {
			enable = true,
			update_cwd = true,
		},

That might explain some of the slowness in #791.

Either way, we probably should fix that ;)

-- always match against the real path
local fname_real = uv.fs_realpath(fname)
if not fname_real then
Expand Down Expand Up @@ -68,6 +70,8 @@ function M.fn(fname)
view.set_cursor { index, 0 }
end
running[fname] = false

log.profile_end(ps, "find file %s", fname)
end

return M
4 changes: 4 additions & 0 deletions lua/nvim-tree/git/runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ end

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

local self = setmetatable({
project_root = opts.project_root,
list_untracked = opts.list_untracked,
Expand All @@ -140,6 +142,8 @@ function Runner.run(opts)
self:_run_git_job()
self:_wait()

log.profile_end(ps, "git job %s", opts.project_root)

if self.rc == -1 then
log.line("git", "job timed out")
elseif self.rc ~= 0 then
Expand Down
22 changes: 17 additions & 5 deletions lua/nvim-tree/log.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,26 @@ function M.raw(typ, fmt, ...)
io.close(file)
end

--- Write to log file via M.line
--- START is prefixed
--- @return reltime to pass to profile_end
function M.profile_start(fmt, ...)
M.line("profile", "START " .. (fmt or "???"), ...)
return vim.fn.reltime()
end

--- Write to log file via M.line
--- END is prefixed and duration in seconds is suffixed
--- @param start reltime returned from profile_start
function M.profile_end(start, fmt, ...)
local dur = vim.fn.reltimestr(vim.fn.reltime(start, vim.fn.reltime()))
M.line("profile", "END " .. (fmt or "???") .. " " .. dur .. "s", ...)
end

-- Write to log file via M.raw
-- time and typ are prefixed and a trailing newline is added
function M.line(typ, fmt, ...)
if not M.path or not M.config.types[typ] and not M.config.types.all then
return
end

M.raw(typ, string.format("[%s] [%s] %s\n", os.date "%Y:%m:%d %H:%M:%S", typ, fmt), ...)
M.raw(typ, string.format("[%s] [%s] %s\n", os.date "%Y-%m-%d %H:%M:%S", typ, fmt), ...)
end

function M.setup(opts)
Expand Down
6 changes: 6 additions & 0 deletions lua/nvim-tree/renderer/init.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local log = require "nvim-tree.log"
local utils = require "nvim-tree.utils"
local view = require "nvim-tree.view"
local _padding = require "nvim-tree.renderer.padding"
Expand Down Expand Up @@ -238,6 +239,9 @@ function M.draw()
if not core.get_explorer() or not bufnr or not api.nvim_buf_is_loaded(bufnr) then
return
end

local ps = log.profile_start "draw"

local cursor
if view.is_visible() then
cursor = api.nvim_win_get_cursor(view.get_winnr())
Expand Down Expand Up @@ -266,6 +270,8 @@ function M.draw()
if cursor and #lines >= cursor[1] then
api.nvim_win_set_cursor(view.get_winnr(), cursor)
end

log.profile_end(ps, "draw")
end

function M.render_hl(bufnr)
Expand Down