Skip to content

Commit d992387

Browse files
committed
perf: track some additional cputimes
1 parent 46997de commit d992387

File tree

3 files changed

+48
-15
lines changed

3 files changed

+48
-15
lines changed

lua/lazy/init.lua

+3-1
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ function M.setup(spec, opts)
3636
-- load module cache before anything else
3737
require("lazy.core.cache").setup(opts)
3838

39+
require("lazy.stats").track("LazyStart")
40+
3941
local Util = require("lazy.core.util")
4042
local Config = require("lazy.core.config")
4143
local Loader = require("lazy.core.loader")
42-
local Plugin = require("lazy.core.plugin")
4344

4445
Util.track({ plugin = "lazy.nvim" }) -- setup start
4546
Util.track("module", vim.loop.hrtime() - start)
@@ -64,6 +65,7 @@ function M.setup(spec, opts)
6465

6566
-- all done!
6667
vim.cmd("do User LazyDone")
68+
require("lazy.stats").track("LazyDone")
6769
end
6870

6971
function M.stats()

lua/lazy/stats.lua

+28-13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
local ffi = require("ffi")
2+
13
local M = {}
24

35
---@class LazyStats
@@ -10,35 +12,48 @@ M._stats = {
1012
startuptime_cputime = false,
1113
count = 0, -- total number of plugins
1214
loaded = 0, -- number of loaded plugins
15+
---@type table<string, number>
16+
times = {},
1317
}
1418

19+
---@type ffi.namespace*|boolean
20+
M.C = nil
21+
1522
function M.on_ui_enter()
16-
if not M.C then
17-
pcall(function() end)
18-
end
23+
M._stats.startuptime = M.track("UIEnter")
24+
M._stats.startuptime_cputime = M.C ~= false
25+
vim.cmd([[do User LazyVimStarted]])
26+
end
1927

20-
local ok = pcall(function()
21-
local ffi = require("ffi")
22-
ffi.cdef([[
28+
function M.track(event)
29+
local time = M.cputime()
30+
M._stats.times[event] = time
31+
return time
32+
end
33+
34+
function M.cputime()
35+
if M.C == nil then
36+
local ok = pcall(function()
37+
ffi.cdef([[
2338
typedef long time_t;
2439
typedef int clockid_t;
25-
2640
typedef struct timespec {
2741
time_t tv_sec; /* seconds */
2842
long tv_nsec; /* nanoseconds */
2943
} nanotime;
3044
int clock_gettime(clockid_t clk_id, struct timespec *tp);
3145
]])
46+
end)
47+
M.C = ok and ffi.C or false
48+
end
49+
if M.C then
3250
local pnano = assert(ffi.new("nanotime[?]", 1))
3351
local CLOCK_PROCESS_CPUTIME_ID = jit.os == "OSX" and 12 or 2
3452
ffi.C.clock_gettime(CLOCK_PROCESS_CPUTIME_ID, pnano)
35-
M._stats.startuptime = tonumber(pnano[0].tv_sec) / 1e6 + tonumber(pnano[0].tv_nsec) / 1e6
36-
M._stats.startuptime_cputime = true
37-
end)
38-
if not ok then
39-
M._stats.startuptime = (vim.loop.hrtime() - require("lazy")._start) / 1e6
53+
return tonumber(pnano[0].tv_sec) / 1e6 + tonumber(pnano[0].tv_nsec) / 1e6
54+
else
55+
return (vim.loop.hrtime() - require("lazy")._start) / 1e6
4056
end
41-
vim.cmd([[do User LazyVimStarted]])
4257
end
4358

4459
function M.stats()

lua/lazy/view/render.lua

+17-1
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,23 @@ function M:profile()
556556
:append("UIEnter", "LazySpecial")
557557
self:append(".")
558558
end
559-
self:nl():nl()
559+
self:nl()
560+
561+
local times = {}
562+
for event, time in pairs(require("lazy.stats").stats().times) do
563+
times[#times + 1] = { event, self:ms(time * 1e6), "Bold", time = time }
564+
end
565+
table.sort(times, function(a, b)
566+
return a.time < b.time
567+
end)
568+
for p, prop in ipairs(times) do
569+
if p > 1 then
570+
prop[2] = prop[2] .. " (+" .. self:ms((prop.time - times[p - 1].time) * 1e6) .. ")"
571+
end
572+
end
573+
self:props(times, { indent = 2 })
574+
575+
self:nl()
560576

561577
self:append("Profile", "LazyH2"):nl():nl()
562578
self

0 commit comments

Comments
 (0)