Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ii14
Last active July 9, 2022 23:36
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ii14/c6070ed07102038122a959313e01cdac to your computer and use it in GitHub Desktop.
Save ii14/c6070ed07102038122a959313e01cdac to your computer and use it in GitHub Desktop.
Shows lua's `require` in `--startuptime` logs
-- OUTDATED: it got merged to neovim: https://github.com/neovim/neovim/commit/2c739431e8e5a2aacc39ef429401f5e4427d027a
-- nvim --cmd 'so startuptime.lua' --startuptime nvim.log
local ffi = require('ffi')
local C = ffi.C
ffi.cdef([[
typedef void FILE;
typedef uint64_t proftime_T;
extern FILE *time_fd;
void time_msg(const char *mesg, const proftime_T *start);
void time_push(proftime_T *rel, proftime_T *start);
void time_pop(proftime_T tp);
]])
if C.time_fd == nil then
return
end
local function pack(...)
return select('#', ...), {...}
end
local _require = require
function _G.require(modname)
if package.loaded[modname] ~= nil then
return _require(modname)
end
local rel_time = ffi.new('proftime_T[1]')
local start_time = ffi.new('proftime_T[1]')
local time_fd = C.time_fd
if time_fd ~= nil then
C.time_push(rel_time, start_time)
end
local n, res = pack(pcall(_require, modname))
if time_fd ~= nil then
C.time_msg(('require %s'):format(modname), start_time)
C.time_pop(rel_time[0])
end
if res[1] then
return unpack(res, 2, n)
else
error(res[2])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment