fix(#1961): harden profiling functions (#1986)

This commit is contained in:
Alexander Courtis 2023-02-11 17:05:01 +11:00 committed by GitHub
parent 02fdc262eb
commit b712b82b0c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 44 additions and 46 deletions

View File

@ -28,7 +28,7 @@ function M.fn(fname)
end end
running[fname_real] = true running[fname_real] = true
local ps = log.profile_start("find file %s", fname_real) local profile = log.profile_start("find file %s", fname_real)
-- we cannot wait for watchers -- we cannot wait for watchers
reload.refresh_nodes_for_path(vim.fn.fnamemodify(fname_real, ":h")) reload.refresh_nodes_for_path(vim.fn.fnamemodify(fname_real, ":h"))
@ -71,7 +71,7 @@ function M.fn(fname)
running[fname_real] = false running[fname_real] = false
log.profile_end(ps, "find file %s", fname_real) log.profile_end(profile)
end end
return M return M

View File

@ -56,9 +56,9 @@ end
local function add_profiling_to(f) local function add_profiling_to(f)
return function(foldername, should_open_view) return function(foldername, should_open_view)
local ps = log.profile_start("change dir %s", foldername) local profile = log.profile_start("change dir %s", foldername)
f(foldername, should_open_view) f(foldername, should_open_view)
log.profile_end(ps, "change dir %s", foldername) log.profile_end(profile)
end end
end end

View File

@ -10,8 +10,7 @@ TreeExplorer = nil
local first_init_done = false local first_init_done = false
function M.init(foldername) function M.init(foldername)
local pn = string.format("core init %s", foldername) local profile = log.profile_start("core init %s", foldername)
local ps = log.profile_start(pn)
if TreeExplorer then if TreeExplorer then
TreeExplorer:destroy() TreeExplorer:destroy()
@ -21,7 +20,7 @@ function M.init(foldername)
events._dispatch_ready() events._dispatch_ready()
first_init_done = true first_init_done = true
end end
log.profile_end(ps, pn) log.profile_end(profile)
end end
function M.get_explorer() function M.get_explorer()

View File

@ -90,7 +90,7 @@ function M.update()
return return
end end
utils.debounce("diagnostics", M.debounce_delay, function() utils.debounce("diagnostics", M.debounce_delay, function()
local ps = log.profile_start "diagnostics update" local profile = log.profile_start "diagnostics update"
log.line("diagnostics", "update") log.line("diagnostics", "update")
local buffer_severity local buffer_severity
@ -130,7 +130,7 @@ function M.update()
end end
end end
end end
log.profile_end(ps, "diagnostics update") log.profile_end(profile)
end) end)
end end

View File

@ -26,8 +26,7 @@ local function populate_children(handle, cwd, node, git_status)
local abs = utils.path_join { cwd, name } local abs = utils.path_join { cwd, name }
local pn = string.format("explore populate_children %s", abs) local profile = log.profile_start("explore populate_children %s", abs)
local ps = log.profile_start(pn)
t = get_type_from(t, abs) t = get_type_from(t, abs)
if if
@ -53,7 +52,7 @@ local function populate_children(handle, cwd, node, git_status)
end end
end end
log.profile_end(ps, pn) log.profile_end(profile)
end end
end end
@ -64,8 +63,7 @@ function M.explore(node, status)
return return
end end
local pn = string.format("explore init %s", node.absolute_path) local profile = log.profile_start("explore init %s", node.absolute_path)
local ps = log.profile_start(pn)
populate_children(handle, cwd, node, status) populate_children(handle, cwd, node, status)
@ -76,14 +74,14 @@ function M.explore(node, status)
local ns = M.explore(child_folder_only, status) local ns = M.explore(child_folder_only, status)
node.nodes = ns or {} node.nodes = ns or {}
log.profile_end(ps, pn) log.profile_end(profile)
return ns return ns
end end
sorters.merge_sort(node.nodes, sorters.node_comparator) sorters.merge_sort(node.nodes, sorters.node_comparator)
live_filter.apply_filter(node) live_filter.apply_filter(node)
log.profile_end(ps, pn) log.profile_end(profile)
return node.nodes return node.nodes
end end

View File

@ -41,7 +41,7 @@ function M.reload(node, git_status, unloaded_bufnr)
return return
end end
local ps = log.profile_start("reload %s", node.absolute_path) local profile = log.profile_start("reload %s", node.absolute_path)
local filter_status = filters.prepare(git_status, unloaded_bufnr) local filter_status = filters.prepare(git_status, unloaded_bufnr)
@ -130,13 +130,13 @@ function M.reload(node, git_status, unloaded_bufnr)
node.group_next = child_folder_only node.group_next = child_folder_only
local ns = M.reload(child_folder_only, git_status) local ns = M.reload(child_folder_only, git_status)
node.nodes = ns or {} node.nodes = ns or {}
log.profile_end(ps, "reload %s", node.absolute_path) log.profile_end(profile)
return ns return ns
end end
sorters.merge_sort(node.nodes, sorters.node_comparator) sorters.merge_sort(node.nodes, sorters.node_comparator)
live_filter.apply_filter(node) live_filter.apply_filter(node)
log.profile_end(ps, "reload %s", node.absolute_path) log.profile_end(profile)
return node.nodes return node.nodes
end end
@ -164,8 +164,7 @@ function M.refresh_nodes_for_path(path)
return return
end end
local pn = string.format("refresh_nodes_for_path %s", path) local profile = log.profile_start("refresh_nodes_for_path %s", path)
local ps = log.profile_start(pn)
NodeIterator.builder({ explorer }) NodeIterator.builder({ explorer })
:hidden() :hidden()
@ -186,7 +185,7 @@ function M.refresh_nodes_for_path(path)
end) end)
:iterate() :iterate()
log.profile_end(ps, pn) log.profile_end(profile)
end end
function M.setup(opts) function M.setup(opts)

View File

@ -140,7 +140,7 @@ end
-- This module runs a git process, which will be killed if it takes more than timeout which defaults to 400ms -- This module runs a git process, which will be killed if it takes more than timeout which defaults to 400ms
function Runner.run(opts) function Runner.run(opts)
local ps = log.profile_start("git job %s %s", opts.project_root, opts.path) local profile = log.profile_start("git job %s %s", opts.project_root, opts.path)
local self = setmetatable({ local self = setmetatable({
project_root = opts.project_root, project_root = opts.project_root,
@ -155,7 +155,7 @@ function Runner.run(opts)
self:_run_git_job() self:_run_git_job()
self:_wait() self:_wait()
log.profile_end(ps, "git job %s %s", opts.project_root, opts.path) log.profile_end(profile)
if self.rc == -1 then if self.rc == -1 then
log.line("git", "job timed out %s %s", opts.project_root, opts.path) log.line("git", "job timed out %s %s", opts.project_root, opts.path)

View File

@ -4,7 +4,7 @@ local log = require "nvim-tree.log"
local has_cygpath = vim.fn.executable "cygpath" == 1 local has_cygpath = vim.fn.executable "cygpath" == 1
function M.get_toplevel(cwd) function M.get_toplevel(cwd)
local ps = log.profile_start("git toplevel %s", cwd) local profile = log.profile_start("git toplevel %s", cwd)
local cmd = { "git", "-C", cwd, "rev-parse", "--show-toplevel" } local cmd = { "git", "-C", cwd, "rev-parse", "--show-toplevel" }
log.line("git", "%s", vim.inspect(cmd)) log.line("git", "%s", vim.inspect(cmd))
@ -12,7 +12,7 @@ function M.get_toplevel(cwd)
local toplevel = vim.fn.system(cmd) local toplevel = vim.fn.system(cmd)
log.raw("git", toplevel) log.raw("git", toplevel)
log.profile_end(ps, "git toplevel %s", cwd) log.profile_end(profile)
if vim.v.shell_error ~= 0 or not toplevel or #toplevel == 0 or toplevel:match "fatal" then if vim.v.shell_error ~= 0 or not toplevel or #toplevel == 0 or toplevel:match "fatal" then
return nil return nil
@ -41,7 +41,7 @@ function M.should_show_untracked(cwd)
return untracked[cwd] return untracked[cwd]
end end
local ps = log.profile_start("git untracked %s", cwd) local profile = log.profile_start("git untracked %s", cwd)
local cmd = { "git", "-C", cwd, "config", "status.showUntrackedFiles" } local cmd = { "git", "-C", cwd, "config", "status.showUntrackedFiles" }
log.line("git", vim.inspect(cmd)) log.line("git", vim.inspect(cmd))
@ -49,7 +49,7 @@ function M.should_show_untracked(cwd)
local has_untracked = vim.fn.system(cmd) local has_untracked = vim.fn.system(cmd)
log.raw("git", has_untracked) log.raw("git", has_untracked)
log.profile_end(ps, "git untracked %s", cwd) log.profile_end(profile)
untracked[cwd] = vim.trim(has_untracked) ~= "no" untracked[cwd] = vim.trim(has_untracked) ~= "no"
return untracked[cwd] return untracked[cwd]

View File

@ -21,29 +21,32 @@ function M.raw(typ, fmt, ...)
end end
end end
---@class Profile
---@field start number nanos
---@field tag string
--- Write profile start to log file --- Write profile start to log file
--- START is prefixed --- START is prefixed
--- @param fmt string for string.format --- @param fmt string for string.format
--- @vararg any arguments for string.format --- @vararg any arguments for string.format
--- @return number nanos to pass to profile_end --- @return Profile to pass to profile_end
function M.profile_start(fmt, ...) function M.profile_start(fmt, ...)
local profile = {}
if M.enabled "profile" then if M.enabled "profile" then
M.line("profile", "START " .. (fmt or "???"), ...) profile.start = vim.loop.hrtime()
return vim.loop.hrtime() profile.tag = string.format((fmt or "???"), ...)
else M.line("profile", "START %s", profile.tag)
return 0
end end
return profile
end end
--- Write profile end to log file --- Write profile end to log file
--- END is prefixed and duration in seconds is suffixed --- END is prefixed and duration in seconds is suffixed
--- @param start number nanos returned from profile_start --- @param profile Profile returned from profile_start
--- @param fmt string for string.format function M.profile_end(profile)
--- @vararg any arguments for string.format if M.enabled "profile" and type(profile) == "table" then
function M.profile_end(start, fmt, ...) local millis = profile.start and math.modf((vim.loop.hrtime() - profile.start) / 1000000) or -1
if M.enabled "profile" then M.line("profile", "END %s %dms", profile.tag or "", millis)
local millis = start and math.modf((vim.loop.hrtime() - start) / 1000000) or -1
M.line("profile", "END " .. (fmt or "???") .. " " .. millis .. "ms", ...)
end end
end end
@ -54,7 +57,7 @@ end
--- @vararg any arguments for string.format --- @vararg any arguments for string.format
function M.line(typ, fmt, ...) function M.line(typ, fmt, ...)
if M.enabled(typ) then if M.enabled(typ) then
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 or "???")), ...)
end end
end end

View File

@ -55,7 +55,7 @@ function M.draw(unloaded_bufnr)
return return
end end
local ps = log.profile_start "draw" local profile = log.profile_start "draw"
local cursor = vim.api.nvim_win_get_cursor(view.get_winnr()) local cursor = vim.api.nvim_win_get_cursor(view.get_winnr())
icon_component.reset_config() icon_component.reset_config()
@ -100,7 +100,7 @@ function M.draw(unloaded_bufnr)
view.grow_from_content() view.grow_from_content()
log.profile_end(ps, "draw") log.profile_end(profile)
end end
function M.setup(opts) function M.setup(opts)

View File

@ -238,8 +238,7 @@ function M.open(options)
return return
end end
local pn = string.format "view open" local profile = log.profile_start "view open"
local ps = log.profile_start(pn)
create_buffer() create_buffer()
open_window() open_window()
@ -251,7 +250,7 @@ function M.open(options)
end end
events._dispatch_on_tree_open() events._dispatch_on_tree_open()
log.profile_end(ps, pn) log.profile_end(profile)
end end
local function grow() local function grow()