1
+ local ffi = require (" ffi" )
2
+
1
3
local M = {}
2
4
3
5
--- @class LazyStats
@@ -10,35 +12,48 @@ M._stats = {
10
12
startuptime_cputime = false ,
11
13
count = 0 , -- total number of plugins
12
14
loaded = 0 , -- number of loaded plugins
15
+ --- @type table<string , number>
16
+ times = {},
13
17
}
14
18
19
+ --- @type ffi.namespace* | boolean
20
+ M .C = nil
21
+
15
22
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
19
27
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 ([[
23
38
typedef long time_t;
24
39
typedef int clockid_t;
25
-
26
40
typedef struct timespec {
27
41
time_t tv_sec; /* seconds */
28
42
long tv_nsec; /* nanoseconds */
29
43
} nanotime;
30
44
int clock_gettime(clockid_t clk_id, struct timespec *tp);
31
45
]] )
46
+ end )
47
+ M .C = ok and ffi .C or false
48
+ end
49
+ if M .C then
32
50
local pnano = assert (ffi .new (" nanotime[?]" , 1 ))
33
51
local CLOCK_PROCESS_CPUTIME_ID = jit .os == " OSX" and 12 or 2
34
52
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
40
56
end
41
- vim .cmd ([[ do User LazyVimStarted]] )
42
57
end
43
58
44
59
function M .stats ()
0 commit comments