Skip to content

Commit 20ff5fa

Browse files
committedNov 29, 2022
feat: added profiler view
1 parent 08b7e42 commit 20ff5fa

File tree

9 files changed

+81
-61
lines changed

9 files changed

+81
-61
lines changed
 

‎README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@
2525

2626
- [ ] health checks: check merge conflicts async
2727
- [ ] defaults for git log
28-
- [ ] view keybindings for update/clean/...
29-
- [ ] add profiler to view
30-
- [ ] add buttons for actions
28+
- [x] view keybindings for update/clean/...
29+
- [x] add profiler to view
30+
- [x] add buttons for actions
3131
- [x] show time taken for op in view
3232
- [ ] package meta index (package.lua cache for all packages)
3333
- [ ] migrate from Packer

‎lua/lazy/core/config.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ M.defaults = {
1717
view = {
1818
icons = {
1919
start = "",
20-
plugin = "",
20+
plugin = " ",
2121
source = "",
2222
config = "",
2323
event = "",

‎lua/lazy/core/handler.lua

+6-10
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function M.handlers.event(grouped)
5353
once = true,
5454
pattern = pattern,
5555
callback = function()
56-
Util.track("event: " .. (_event == "User" and pattern or event))
56+
Util.track({ event = event })
5757
Loader.load(plugins, { event = event })
5858
Util.track()
5959
end,
@@ -67,7 +67,7 @@ function M.handlers.keys(grouped)
6767
---@cast keys string
6868
vim.keymap.set("n", keys, function()
6969
vim.keymap.del("n", keys)
70-
Util.track("keys: " .. keys)
70+
Util.track({ keys = keys })
7171
Loader.load(plugins, { keys = keys })
7272
vim.api.nvim_input(keys)
7373
Util.track()
@@ -84,7 +84,7 @@ function M.handlers.ft(grouped)
8484
pattern = ft,
8585
group = group,
8686
callback = function()
87-
Util.track("filetype: " .. ft)
87+
Util.track({ ft = ft })
8888
Loader.load(plugins, { ft = ft })
8989
Util.track()
9090
end,
@@ -95,13 +95,9 @@ end
9595
function M.handlers.cmd(grouped)
9696
for cmd, plugins in pairs(grouped) do
9797
---@cast cmd string
98-
local function _load(complete)
98+
local function _load()
9999
vim.api.nvim_del_user_command(cmd)
100-
if complete then
101-
Util.track("cmd-complete: " .. cmd)
102-
else
103-
Util.track("cmd: " .. cmd)
104-
end
100+
Util.track({ cmd = cmd })
105101
Loader.load(plugins, { cmd = cmd })
106102
Util.track()
107103
end
@@ -120,7 +116,7 @@ function M.handlers.cmd(grouped)
120116
bang = true,
121117
nargs = "*",
122118
complete = function()
123-
_load(true)
119+
_load()
124120
-- HACK: trick Neovim to show the newly loaded command completion
125121
vim.api.nvim_input("<space><bs><tab>")
126122
end,

‎lua/lazy/core/loader.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ function M.init_plugins()
2222
Util.track("plugin_init")
2323
for _, plugin in pairs(Config.plugins) do
2424
if plugin.init then
25-
Util.track(plugin.name)
25+
Util.track({ plugin = plugin.name, start = "init" })
2626
Util.try(plugin.init, "Failed to run `init` for **" .. plugin.name .. "**")
2727
Util.track()
2828
end
@@ -58,7 +58,7 @@ function M.load(plugins, reason, opts)
5858

5959
table.insert(M.loading, plugin)
6060

61-
Util.track(plugin.name)
61+
Util.track({ plugin = plugin.name, start = reason.start })
6262
M.packadd(plugin, opts and opts.load_start)
6363

6464
if plugin.requires then

‎lua/lazy/core/util.lua

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
local M = {}
22

3-
---@alias LazyProfile {name: string, time: number, [number]:LazyProfile}
3+
---@alias LazyProfile {data: string|{[string]:string}, time: number, [number]:LazyProfile}
44

55
---@type LazyProfile[]
66
M._profiles = { { name = "lazy" } }
77

8-
---@param name string?
8+
---@param data (string|{[string]:string})?
99
---@param time number?
10-
function M.track(name, time)
11-
if name then
10+
function M.track(data, time)
11+
if data then
1212
local entry = {
13-
name = name,
13+
data = data,
1414
time = time or vim.loop.hrtime(),
1515
}
1616
table.insert(M._profiles[#M._profiles], entry)

‎lua/lazy/util.lua

-26
Original file line numberDiff line numberDiff line change
@@ -52,32 +52,6 @@ function M.throttle(ms, fn)
5252
end
5353
end
5454

55-
function M.profile()
56-
local lines = { "# Profile" }
57-
58-
---@param entry LazyProfile
59-
local function _profile(entry, depth)
60-
if entry.time < 0.5 then
61-
-- Nothing
62-
end
63-
64-
table.insert(
65-
lines,
66-
(" "):rep(depth) .. "- " .. entry.name .. ": **" .. math.floor((entry.time or 0) / 1e6 * 100) / 100 .. "ms**"
67-
)
68-
69-
for _, child in ipairs(entry) do
70-
_profile(child, depth + 1)
71-
end
72-
end
73-
74-
for _, entry in ipairs(M._profiles[1]) do
75-
_profile(entry, 1)
76-
end
77-
78-
M.markdown(lines)
79-
end
80-
8155
---@return string?
8256
function M.head(file)
8357
local f = io.open(file)

‎lua/lazy/view/commands.lua

+3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ M.commands = {
3636
help = function()
3737
View.show("help")
3838
end,
39+
profile = function()
40+
View.show("profile")
41+
end,
3942
sync = function()
4043
Manage.clean({ interactive = true, clear = true, wait = true, mode = "sync" })
4144
Manage.update({ interactive = true })

‎lua/lazy/view/init.lua

+7-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ M.modes = {
1212
{ name = "check", key = "C", desc = "Check for updates and show the log (git fetch)" },
1313
{ name = "log", key = "L", desc = "Show recent updates for all plugins" },
1414
{ name = "restore", key = "R", desc = "Updates all plugins to the state in the lockfile" },
15-
{ name = "help", key = "g?", hide = true, desc = "Toggle this help page" },
15+
{ name = "profile", key = "P", desc = "Show detailed profiling", toggle = true },
16+
{ name = "help", key = "g?", hide = true, desc = "Toggle this help page", toggle = true },
1617

1718
{ plugin = true, name = "update", key = "u", desc = "Update this plugin. This will also update the lockfile" },
1819
{
@@ -36,11 +37,7 @@ function M.setup()
3637
end
3738

3839
function M.show(mode)
39-
if mode == "help" and M.mode == "help" then
40-
M.mode = nil
41-
else
42-
M.mode = mode or M.mode
43-
end
40+
M.mode = mode or M.mode
4441
require("lazy.view.colors").setup()
4542

4643
if M._buf and vim.api.nvim_buf_is_valid(M._buf) then
@@ -171,6 +168,10 @@ function M.show(mode)
171168
Commands.cmd(m.name, { plugin })
172169
end
173170
else
171+
if M.mode == m.name and m.toggle then
172+
M.mode = nil
173+
return update()
174+
end
174175
Commands.cmd(m.name)
175176
end
176177
end, { buffer = buf })

‎lua/lazy/view/render.lua

+54-8
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ function M:update()
6060

6161
if mode == "help" then
6262
self:help()
63+
elseif mode == "profile" then
64+
self:profile()
6365
else
6466
for _, section in ipairs(Sections) do
6567
self:section(section)
@@ -109,7 +111,7 @@ function M:title()
109111
end
110112
self:nl()
111113

112-
if View.mode ~= "help" then
114+
if View.mode ~= "help" and View.mode ~= "profile" then
113115
if self.progress.done < self.progress.total then
114116
self:append("Tasks: ", "LazyH2")
115117
self:append(self.progress.done .. "/" .. self.progress.total, "LazyMuted")
@@ -181,9 +183,14 @@ function M:diagnostic(diag)
181183
table.insert(self._diagnostics, diag)
182184
end
183185

184-
---@param plugin LazyPlugin
185-
function M:reason(plugin)
186-
local reason = vim.deepcopy(plugin._.loaded or {})
186+
---@param reason? {[string]:string, time:number}
187+
---@param opts? {time_right?:boolean}
188+
function M:reason(reason, opts)
189+
opts = opts or {}
190+
if not reason then
191+
return
192+
end
193+
reason = vim.deepcopy(reason)
187194
---@type string?
188195
local source = reason.source
189196
if source then
@@ -207,12 +214,16 @@ function M:reason(plugin)
207214
end
208215
end
209216
end
210-
self:append(" " .. math.floor((reason.time or 0) / 1e6 * 100) / 100 .. "ms", "Bold")
217+
local time = " " .. math.floor((reason.time or 0) / 1e6 * 100) / 100 .. "ms"
218+
if not opts.time_right then
219+
self:append(time, "Bold")
220+
end
211221
self:append(" ")
212222
-- self:append(" (", "Conceal")
213223
local first = true
214224
for key, value in pairs(reason) do
215-
if key == "require" then
225+
if type(key) == "number" then
226+
elseif key == "require" then
216227
-- self:append("require", "@function.builtin")
217228
-- self:append("(", "@punctuation.bracket")
218229
-- self:append('"' .. value .. '"', "@string")
@@ -237,6 +248,9 @@ function M:reason(plugin)
237248
end
238249
end
239250
end
251+
if opts.time_right then
252+
self:append(time, "Bold")
253+
end
240254
-- self:append(")", "Conceal")
241255
end
242256

@@ -270,10 +284,14 @@ end
270284

271285
---@param plugin LazyPlugin
272286
function M:plugin(plugin)
273-
self:append(" - ", "LazySpecial"):append(plugin.name)
287+
if plugin._.loaded then
288+
self:append("", "LazySpecial"):append(plugin.name)
289+
else
290+
self:append("", "LazySpecial"):append(plugin.name)
291+
end
274292
local plugin_start = self:row()
275293
if plugin._.loaded then
276-
self:reason(plugin)
294+
self:reason(plugin._.loaded)
277295
end
278296
self:diagnostics(plugin)
279297
self:nl()
@@ -377,4 +395,32 @@ function M:details(plugin)
377395
self:nl()
378396
end
379397

398+
function M:profile()
399+
self:append("Profile", "LazyH2"):nl():nl()
400+
local symbols = {
401+
"",
402+
"",
403+
"",
404+
"",
405+
}
406+
407+
---@param entry LazyProfile
408+
local function _profile(entry, depth)
409+
local data = type(entry.data) == "string" and { source = entry.data } or entry.data
410+
data.time = entry.time
411+
local symbol = symbols[depth] or symbols[#symbols]
412+
self:append((" "):rep(depth)):append(" " .. symbol, "LazySpecial")
413+
self:reason(data, { time_right = true })
414+
self:nl()
415+
416+
for _, child in ipairs(entry) do
417+
_profile(child, depth + 1)
418+
end
419+
end
420+
421+
for _, entry in ipairs(Util._profiles[1]) do
422+
_profile(entry, 1)
423+
end
424+
end
425+
380426
return M

0 commit comments

Comments
 (0)
Please sign in to comment.