2022-11-30 13:19:50 +00:00
|
|
|
local ffi = require("ffi")
|
|
|
|
local uv = vim.loop
|
2022-11-22 20:12:33 +00:00
|
|
|
|
|
|
|
local M = {}
|
2022-11-30 13:19:50 +00:00
|
|
|
|
|
|
|
---@alias CacheHash {mtime: {sec:number, nsec:number}, size:number}
|
2023-02-14 10:00:56 +00:00
|
|
|
---@alias CacheEntry {hash:CacheHash, chunk:string}
|
2023-02-13 11:01:56 +00:00
|
|
|
|
|
|
|
---@class CacheFindOpts
|
|
|
|
---@field rtp? boolean Search for modname in the runtime path (defaults to `true`)
|
|
|
|
---@field patterns? string[] Paterns to use (defaults to `{"/init.lua", ".lua"}`)
|
|
|
|
---@field paths? string[] Extra paths to search for modname
|
|
|
|
|
2023-02-14 10:00:56 +00:00
|
|
|
M.VERSION = 2
|
2023-02-13 11:01:56 +00:00
|
|
|
M.path = vim.fn.stdpath("cache") .. "/lazy/luac"
|
|
|
|
M.enabled = false
|
2023-02-14 10:00:56 +00:00
|
|
|
M.stats = {
|
|
|
|
find = { total = 0, time = 0, not_found = 0 },
|
|
|
|
}
|
2023-02-13 11:01:56 +00:00
|
|
|
|
|
|
|
---@class ModuleCache
|
|
|
|
---@field _rtp string[]
|
2023-02-14 10:00:56 +00:00
|
|
|
---@field _rtp_pure string[]
|
2023-02-13 11:01:56 +00:00
|
|
|
---@field _rtp_key string
|
|
|
|
local Cache = {
|
|
|
|
---@type table<string, table<string,true>>
|
2023-02-14 10:00:56 +00:00
|
|
|
_indexed = {},
|
|
|
|
---@type table<string, string[]>
|
2023-02-13 11:01:56 +00:00
|
|
|
_topmods = {},
|
|
|
|
_loadfile = loadfile,
|
2022-12-28 16:57:59 +00:00
|
|
|
}
|
2022-12-26 17:35:02 +00:00
|
|
|
|
2023-02-14 10:00:56 +00:00
|
|
|
function M.track(stat, start)
|
|
|
|
M.stats[stat] = M.stats[stat] or { total = 0, time = 0 }
|
|
|
|
M.stats[stat].total = M.stats[stat].total + 1
|
|
|
|
M.stats[stat].time = M.stats[stat].time + uv.hrtime() - start
|
|
|
|
end
|
|
|
|
|
2023-02-13 11:01:56 +00:00
|
|
|
-- slightly faster/different version than vim.fs.normalize
|
|
|
|
-- we also need to have it here, since the cache will load vim.fs
|
|
|
|
---@private
|
|
|
|
function Cache.normalize(path)
|
|
|
|
if path:sub(1, 1) == "~" then
|
2023-02-14 10:00:56 +00:00
|
|
|
local home = vim.loop.os_homedir() or "~"
|
2023-02-13 11:01:56 +00:00
|
|
|
if home:sub(-1) == "\\" or home:sub(-1) == "/" then
|
|
|
|
home = home:sub(1, -2)
|
2022-12-19 11:21:20 +00:00
|
|
|
end
|
2023-02-13 11:01:56 +00:00
|
|
|
path = home .. path:sub(2)
|
2022-12-19 11:21:20 +00:00
|
|
|
end
|
2023-02-13 11:01:56 +00:00
|
|
|
path = path:gsub("\\", "/"):gsub("/+", "/")
|
|
|
|
return path:sub(-1) == "/" and path:sub(1, -2) or path
|
2022-12-28 16:57:59 +00:00
|
|
|
end
|
|
|
|
|
2023-02-13 11:01:56 +00:00
|
|
|
---@private
|
|
|
|
function Cache.get_rtp()
|
2023-02-14 10:00:56 +00:00
|
|
|
local start = uv.hrtime()
|
2023-02-13 11:01:56 +00:00
|
|
|
if vim.in_fast_event() then
|
2023-02-14 10:00:56 +00:00
|
|
|
M.track("get_rtp", start)
|
|
|
|
return (Cache._rtp or {}), false
|
2023-02-13 11:01:56 +00:00
|
|
|
end
|
2023-02-14 10:00:56 +00:00
|
|
|
local updated = false
|
2023-02-13 11:01:56 +00:00
|
|
|
local key = vim.go.rtp
|
|
|
|
if key ~= Cache._rtp_key then
|
|
|
|
Cache._rtp = {}
|
|
|
|
for _, path in ipairs(vim.api.nvim_get_runtime_file("", true)) do
|
|
|
|
path = Cache.normalize(path)
|
|
|
|
-- skip after directories
|
2023-02-14 10:00:56 +00:00
|
|
|
if path:sub(-6, -1) ~= "/after" and not (Cache._indexed[path] and vim.tbl_isempty(Cache._indexed[path])) then
|
2023-02-13 11:01:56 +00:00
|
|
|
Cache._rtp[#Cache._rtp + 1] = path
|
2022-12-19 11:21:20 +00:00
|
|
|
end
|
|
|
|
end
|
2023-02-14 10:00:56 +00:00
|
|
|
updated = true
|
2023-02-13 11:01:56 +00:00
|
|
|
Cache._rtp_key = key
|
2022-12-02 08:20:48 +00:00
|
|
|
end
|
2023-02-14 10:00:56 +00:00
|
|
|
M.track("get_rtp", start)
|
|
|
|
return Cache._rtp, updated
|
2022-12-02 08:20:48 +00:00
|
|
|
end
|
2022-11-24 21:04:23 +00:00
|
|
|
|
2023-02-13 11:01:56 +00:00
|
|
|
---@param name string can be a module name, or a file name
|
|
|
|
---@private
|
|
|
|
function Cache.cache_file(name)
|
2023-02-14 10:00:56 +00:00
|
|
|
local ret = M.path .. "/" .. name:gsub("[/\\:]", "%%")
|
|
|
|
return ret:sub(-4) == ".lua" and (ret .. "c") or (ret .. ".luac")
|
2023-01-03 15:17:15 +00:00
|
|
|
end
|
|
|
|
|
2023-02-13 11:01:56 +00:00
|
|
|
---@param entry CacheEntry
|
|
|
|
---@private
|
|
|
|
function Cache.write(name, entry)
|
|
|
|
local cname = Cache.cache_file(name)
|
|
|
|
local f = assert(uv.fs_open(cname, "w", 438))
|
|
|
|
local header = {
|
|
|
|
M.VERSION,
|
|
|
|
entry.hash.size,
|
|
|
|
entry.hash.mtime.sec,
|
|
|
|
entry.hash.mtime.nsec,
|
|
|
|
}
|
2023-02-14 10:00:56 +00:00
|
|
|
uv.fs_write(f, ffi.string(ffi.new("const uint32_t[4]", header), 16))
|
2023-02-13 11:01:56 +00:00
|
|
|
uv.fs_write(f, entry.chunk)
|
|
|
|
uv.fs_close(f)
|
2022-12-02 11:43:34 +00:00
|
|
|
end
|
|
|
|
|
2023-02-13 11:01:56 +00:00
|
|
|
---@return CacheEntry?
|
|
|
|
---@private
|
|
|
|
function Cache.read(name)
|
2023-02-14 10:00:56 +00:00
|
|
|
local start = uv.hrtime()
|
2023-02-13 11:01:56 +00:00
|
|
|
local cname = Cache.cache_file(name)
|
|
|
|
local f = uv.fs_open(cname, "r", 438)
|
|
|
|
if f then
|
|
|
|
local hash = uv.fs_fstat(f) --[[@as CacheHash]]
|
|
|
|
local data = uv.fs_read(f, hash.size, 0) --[[@as string]]
|
|
|
|
uv.fs_close(f)
|
|
|
|
|
|
|
|
---@type integer[]|{[0]:integer}
|
2023-02-14 10:00:56 +00:00
|
|
|
local header = ffi.cast("uint32_t*", ffi.new("const char[16]", data:sub(1, 16)))
|
2023-02-13 11:01:56 +00:00
|
|
|
if header[0] ~= M.VERSION then
|
|
|
|
return
|
2022-12-29 08:06:23 +00:00
|
|
|
end
|
2023-02-14 10:00:56 +00:00
|
|
|
M.track("read", start)
|
2023-02-13 11:01:56 +00:00
|
|
|
return {
|
|
|
|
hash = { size = header[1], mtime = { sec = header[2], nsec = header[3] } },
|
2023-02-14 10:00:56 +00:00
|
|
|
chunk = data:sub(16 + 1),
|
2023-02-13 11:01:56 +00:00
|
|
|
}
|
2022-12-29 08:06:23 +00:00
|
|
|
end
|
2023-02-14 10:00:56 +00:00
|
|
|
M.track("read", start)
|
2022-12-29 08:06:23 +00:00
|
|
|
end
|
|
|
|
|
2022-12-02 11:43:34 +00:00
|
|
|
---@param modname string
|
2023-02-13 11:01:56 +00:00
|
|
|
---@private
|
|
|
|
function Cache.loader(modname)
|
2023-02-14 10:00:56 +00:00
|
|
|
local start = uv.hrtime()
|
2023-02-13 11:01:56 +00:00
|
|
|
local modpath, hash = Cache.find(modname)
|
|
|
|
if modpath then
|
2023-02-15 16:08:36 +00:00
|
|
|
local chunk, err = M.load(modpath, { hash = hash })
|
|
|
|
M.track("loader", start)
|
|
|
|
return chunk or error(err)
|
2022-12-19 12:34:37 +00:00
|
|
|
end
|
2023-02-14 10:00:56 +00:00
|
|
|
M.track("loader", start)
|
2023-02-15 16:08:36 +00:00
|
|
|
return "\nlazy_loader: module " .. modname .. " not found"
|
2023-02-14 10:00:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param modname string
|
|
|
|
---@private
|
|
|
|
function Cache.loader_lib(modname)
|
|
|
|
local start = uv.hrtime()
|
|
|
|
local modpath = Cache.find(modname, { patterns = jit.os:find("Windows") and { ".dll" } or { ".so" } })
|
|
|
|
---@type function?, string?
|
|
|
|
if modpath then
|
|
|
|
-- Making function name in Lua 5.1 (see src/loadlib.c:mkfuncname) is
|
|
|
|
-- a) strip prefix up to and including the first dash, if any
|
|
|
|
-- b) replace all dots by underscores
|
|
|
|
-- c) prepend "luaopen_"
|
|
|
|
-- So "foo-bar.baz" should result in "luaopen_bar_baz"
|
|
|
|
local dash = modname:find("-", 1, true)
|
|
|
|
local funcname = dash and modname:sub(dash + 1) or modname
|
|
|
|
local chunk, err = package.loadlib(modpath, "luaopen_" .. funcname:gsub("%.", "_"))
|
|
|
|
M.track("loader_lib", start)
|
2023-02-15 16:08:36 +00:00
|
|
|
return chunk or error(err)
|
2023-02-14 10:00:56 +00:00
|
|
|
end
|
|
|
|
M.track("loader_lib", start)
|
2023-02-15 16:08:36 +00:00
|
|
|
return "\nlazy_loader_lib: module " .. modname .. " not found"
|
2022-12-19 12:34:37 +00:00
|
|
|
end
|
|
|
|
|
2023-02-13 11:01:56 +00:00
|
|
|
---@param filename? string
|
|
|
|
---@param mode? "b"|"t"|"bt"
|
|
|
|
---@param env? table
|
|
|
|
---@return function?, string? error_message
|
|
|
|
---@private
|
|
|
|
function Cache.loadfile(filename, mode, env)
|
2023-02-14 10:00:56 +00:00
|
|
|
local start = uv.hrtime()
|
2023-02-13 11:01:56 +00:00
|
|
|
filename = Cache.normalize(filename)
|
2023-02-14 10:00:56 +00:00
|
|
|
local chunk, err = M.load(filename, { mode = mode, env = env })
|
|
|
|
M.track("loadfile", start)
|
|
|
|
return chunk, err
|
2023-02-13 11:01:56 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param h1 CacheHash
|
|
|
|
---@param h2 CacheHash
|
|
|
|
---@private
|
|
|
|
function Cache.eq(h1, h2)
|
|
|
|
return h1 and h2 and h1.size == h2.size and h1.mtime.sec == h2.mtime.sec and h1.mtime.nsec == h2.mtime.nsec
|
2022-12-19 12:34:37 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param modpath string
|
2023-02-14 10:00:56 +00:00
|
|
|
---@param opts? {hash?: CacheHash, mode?: "b"|"t"|"bt", env?:table}
|
2022-12-19 12:34:37 +00:00
|
|
|
---@return function?, string? error_message
|
2023-02-13 11:01:56 +00:00
|
|
|
---@private
|
2023-02-14 10:00:56 +00:00
|
|
|
function M.load(modpath, opts)
|
|
|
|
local start = uv.hrtime()
|
|
|
|
|
2023-02-13 11:01:56 +00:00
|
|
|
opts = opts or {}
|
|
|
|
local hash = opts.hash or uv.fs_stat(modpath)
|
2023-02-14 10:00:56 +00:00
|
|
|
---@type function?, string?
|
|
|
|
local chunk, err
|
|
|
|
|
2022-12-19 12:34:37 +00:00
|
|
|
if not hash then
|
|
|
|
-- trigger correct error
|
2023-02-14 10:00:56 +00:00
|
|
|
chunk, err = Cache._loadfile(modpath, opts.mode, opts.env)
|
|
|
|
M.track("load", start)
|
|
|
|
return chunk, err
|
2022-12-19 12:34:37 +00:00
|
|
|
end
|
|
|
|
|
2023-02-14 10:00:56 +00:00
|
|
|
local entry = Cache.read(modpath)
|
2023-02-13 11:01:56 +00:00
|
|
|
if entry and Cache.eq(entry.hash, hash) then
|
|
|
|
-- found in cache and up to date
|
2023-02-14 10:00:56 +00:00
|
|
|
-- selene: allow(incorrect_standard_library_use)
|
|
|
|
chunk, err = load(entry.chunk --[[@as string]], "@" .. modpath, opts.mode, opts.env)
|
2023-02-13 11:01:56 +00:00
|
|
|
if not (err and err:find("cannot load incompatible bytecode", 1, true)) then
|
2023-02-14 10:00:56 +00:00
|
|
|
M.track("load", start)
|
2023-02-13 11:01:56 +00:00
|
|
|
return chunk, err
|
2022-12-02 08:20:48 +00:00
|
|
|
end
|
2022-11-22 20:12:33 +00:00
|
|
|
end
|
2023-02-13 11:01:56 +00:00
|
|
|
entry = { hash = hash, modpath = modpath }
|
2022-12-19 12:34:37 +00:00
|
|
|
|
2023-02-14 10:00:56 +00:00
|
|
|
chunk, err = Cache._loadfile(modpath, opts.mode, opts.env)
|
2022-12-19 12:34:37 +00:00
|
|
|
if chunk then
|
2022-12-02 08:20:48 +00:00
|
|
|
entry.chunk = string.dump(chunk)
|
2023-02-14 10:00:56 +00:00
|
|
|
Cache.write(modpath, entry)
|
2022-12-02 08:20:48 +00:00
|
|
|
end
|
2023-02-14 10:00:56 +00:00
|
|
|
M.track("load", start)
|
2022-12-19 12:34:37 +00:00
|
|
|
return chunk, err
|
2022-11-22 20:12:33 +00:00
|
|
|
end
|
|
|
|
|
2023-01-01 19:19:09 +00:00
|
|
|
---@param modname string
|
2023-02-13 11:01:56 +00:00
|
|
|
---@param opts? CacheFindOpts
|
2023-02-14 10:00:56 +00:00
|
|
|
---@return string? modpath, CacheHash? hash, CacheEntry? entry
|
2023-02-13 11:01:56 +00:00
|
|
|
function Cache.find(modname, opts)
|
2022-12-28 16:57:59 +00:00
|
|
|
local start = uv.hrtime()
|
2023-02-14 10:00:56 +00:00
|
|
|
opts = opts or {}
|
|
|
|
|
2023-02-13 11:01:56 +00:00
|
|
|
modname = modname:gsub("/", ".")
|
2022-12-19 12:34:37 +00:00
|
|
|
local basename = modname:gsub("%.", "/")
|
2022-12-28 16:57:59 +00:00
|
|
|
local idx = modname:find(".", 1, true)
|
2023-02-15 17:19:57 +00:00
|
|
|
|
2023-02-28 11:38:10 +00:00
|
|
|
-- HACK: fix incorrect require statements. Really not a fan of keeping this
|
|
|
|
if idx == 1 then
|
|
|
|
modname = modname:gsub("^%.+", "")
|
|
|
|
basename = modname:gsub("%.", "/")
|
|
|
|
idx = modname:find(".", 1, true)
|
|
|
|
end
|
2022-12-28 16:57:59 +00:00
|
|
|
local topmod = idx and modname:sub(1, idx - 1) or modname
|
|
|
|
|
2023-02-13 11:01:56 +00:00
|
|
|
-- OPTIM: search for a directory first when topmod == modname
|
|
|
|
local patterns = opts.patterns or (topmod == modname and { "/init.lua", ".lua" } or { ".lua", "/init.lua" })
|
|
|
|
for p, pattern in ipairs(patterns) do
|
|
|
|
patterns[p] = "/lua/" .. basename .. pattern
|
|
|
|
end
|
2022-12-28 16:57:59 +00:00
|
|
|
|
2023-02-14 10:00:56 +00:00
|
|
|
---@param paths string[]
|
|
|
|
local function _find(paths)
|
|
|
|
for _, path in ipairs(paths) do
|
|
|
|
if M.lsmod(path)[topmod] then
|
|
|
|
for _, pattern in ipairs(patterns) do
|
|
|
|
local modpath = path .. pattern
|
|
|
|
M.stats.find.stat = (M.stats.find.stat or 0) + 1
|
|
|
|
local hash = uv.fs_stat(modpath)
|
|
|
|
if hash then
|
|
|
|
return modpath, hash
|
|
|
|
end
|
2022-12-28 16:57:59 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-02-14 10:00:56 +00:00
|
|
|
---@type string, CacheHash
|
|
|
|
local modpath, hash
|
|
|
|
|
|
|
|
if opts.rtp ~= false then
|
|
|
|
modpath, hash = _find(Cache._rtp or {})
|
|
|
|
if not modpath then
|
|
|
|
local rtp, updated = Cache.get_rtp()
|
|
|
|
if updated then
|
|
|
|
modpath, hash = _find(rtp)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if (not modpath) and opts.paths then
|
|
|
|
modpath, hash = _find(opts.paths)
|
|
|
|
end
|
|
|
|
|
|
|
|
M.track("find", start)
|
|
|
|
if modpath then
|
|
|
|
return modpath, hash
|
|
|
|
end
|
2023-02-13 11:01:56 +00:00
|
|
|
-- module not found
|
2023-02-13 16:24:15 +00:00
|
|
|
M.stats.find.not_found = M.stats.find.not_found + 1
|
2022-12-02 08:20:48 +00:00
|
|
|
end
|
|
|
|
|
2023-02-13 11:01:56 +00:00
|
|
|
--- Resets the topmods cache for the path
|
|
|
|
---@param path string
|
|
|
|
function M.reset(path)
|
2023-02-14 10:00:56 +00:00
|
|
|
Cache._indexed[Cache.normalize(path)] = nil
|
2022-12-19 11:21:20 +00:00
|
|
|
end
|
|
|
|
|
2023-02-13 11:01:56 +00:00
|
|
|
function M.enable()
|
|
|
|
if M.enabled then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
M.enabled = true
|
|
|
|
vim.fn.mkdir(vim.fn.fnamemodify(M.path, ":p"), "p")
|
|
|
|
-- selene: allow(global_usage)
|
|
|
|
_G.loadfile = Cache.loadfile
|
2023-02-14 10:00:56 +00:00
|
|
|
-- add lua loader
|
2023-02-13 11:01:56 +00:00
|
|
|
table.insert(package.loaders, 2, Cache.loader)
|
2023-02-14 10:00:56 +00:00
|
|
|
-- add libs loader
|
|
|
|
table.insert(package.loaders, 3, Cache.loader_lib)
|
|
|
|
-- remove Neovim loader
|
|
|
|
for l, loader in ipairs(package.loaders) do
|
|
|
|
if loader == vim._load_package then
|
|
|
|
table.remove(package.loaders, l)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
2022-12-29 00:25:05 +00:00
|
|
|
end
|
|
|
|
|
2023-02-13 11:01:56 +00:00
|
|
|
function M.disable()
|
|
|
|
if not M.enabled then
|
|
|
|
return
|
2022-12-02 11:43:34 +00:00
|
|
|
end
|
2023-02-13 11:01:56 +00:00
|
|
|
M.enabled = false
|
|
|
|
-- selene: allow(global_usage)
|
|
|
|
_G.loadfile = Cache._loadfile
|
|
|
|
---@diagnostic disable-next-line: no-unknown
|
|
|
|
for l, loader in ipairs(package.loaders) do
|
2023-02-14 10:00:56 +00:00
|
|
|
if loader == Cache.loader or loader == Cache.loader_lib then
|
2023-02-13 11:01:56 +00:00
|
|
|
table.remove(package.loaders, l)
|
2022-12-28 16:57:59 +00:00
|
|
|
end
|
2022-12-02 11:43:34 +00:00
|
|
|
end
|
2023-02-14 10:00:56 +00:00
|
|
|
table.insert(package.loaders, 2, vim._load_package)
|
2022-11-30 13:19:50 +00:00
|
|
|
end
|
|
|
|
|
2023-02-13 11:01:56 +00:00
|
|
|
-- Return the top-level `/lua/*` modules for this path
|
|
|
|
---@return string[]
|
|
|
|
function M.lsmod(path)
|
2023-02-14 10:00:56 +00:00
|
|
|
if not Cache._indexed[path] then
|
|
|
|
local start = uv.hrtime()
|
|
|
|
Cache._indexed[path] = {}
|
2023-02-13 11:01:56 +00:00
|
|
|
local handle = vim.loop.fs_scandir(path .. "/lua")
|
|
|
|
while handle do
|
|
|
|
local name, t = vim.loop.fs_scandir_next(handle)
|
|
|
|
if not name then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
-- HACK: type is not always returned due to a bug in luv
|
2023-02-14 10:00:56 +00:00
|
|
|
t = t or uv.fs_stat(path .. "/" .. name).type
|
2023-02-13 11:01:56 +00:00
|
|
|
---@type string
|
|
|
|
local topname
|
2023-02-15 16:08:36 +00:00
|
|
|
local ext = name:sub(-4)
|
|
|
|
if ext == ".lua" or ext == ".dll" then
|
2023-02-13 11:01:56 +00:00
|
|
|
topname = name:sub(1, -5)
|
2023-02-15 16:08:36 +00:00
|
|
|
elseif name:sub(-3) == ".so" then
|
|
|
|
topname = name:sub(1, -4)
|
2023-02-13 11:01:56 +00:00
|
|
|
elseif t == "link" or t == "directory" then
|
|
|
|
topname = name
|
|
|
|
end
|
|
|
|
if topname then
|
2023-02-14 10:00:56 +00:00
|
|
|
Cache._indexed[path][topname] = true
|
|
|
|
Cache._topmods[topname] = Cache._topmods[topname] or {}
|
|
|
|
if not vim.tbl_contains(Cache._topmods[topname], path) then
|
|
|
|
table.insert(Cache._topmods[topname], path)
|
|
|
|
end
|
2023-02-13 11:01:56 +00:00
|
|
|
end
|
2022-11-30 13:19:50 +00:00
|
|
|
end
|
2023-02-14 10:00:56 +00:00
|
|
|
M.track("lsmod", start)
|
2022-11-30 13:19:50 +00:00
|
|
|
end
|
2023-02-14 10:00:56 +00:00
|
|
|
return Cache._indexed[path]
|
2022-11-30 13:19:50 +00:00
|
|
|
end
|
|
|
|
|
2023-02-13 11:01:56 +00:00
|
|
|
---@param modname string
|
|
|
|
---@param opts? CacheFindOpts
|
|
|
|
---@return string? modpath
|
|
|
|
function M.find(modname, opts)
|
|
|
|
local modpath = Cache.find(modname, opts)
|
|
|
|
return modpath
|
|
|
|
end
|
2022-12-14 20:38:24 +00:00
|
|
|
|
2023-02-14 10:00:56 +00:00
|
|
|
function M.profile_loaders()
|
|
|
|
for l, loader in pairs(package.loaders) do
|
|
|
|
local loc = debug.getinfo(loader, "Sn").source:sub(2)
|
|
|
|
package.loaders[l] = function(modname)
|
|
|
|
local start = vim.loop.hrtime()
|
|
|
|
local ret = loader(modname)
|
|
|
|
M.track("loader " .. l .. ": " .. loc, start)
|
|
|
|
M.track("loader_all", start)
|
|
|
|
return ret
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-02-13 11:01:56 +00:00
|
|
|
function M.inspect()
|
|
|
|
local function ms(nsec)
|
|
|
|
return math.floor(nsec / 1e6 * 1000 + 0.5) / 1000 .. "ms"
|
2022-11-30 13:19:50 +00:00
|
|
|
end
|
2023-02-13 11:01:56 +00:00
|
|
|
local chunks = {} ---@type string[][]
|
2023-02-14 10:00:56 +00:00
|
|
|
---@type string[]
|
|
|
|
local stats = vim.tbl_keys(M.stats)
|
|
|
|
table.sort(stats)
|
|
|
|
for _, stat in ipairs(stats) do
|
|
|
|
vim.list_extend(chunks, {
|
|
|
|
{ "\n" .. stat .. "\n", "Title" },
|
|
|
|
{ "* total: " },
|
|
|
|
{ tostring(M.stats[stat].total) .. "\n", "Number" },
|
|
|
|
{ "* time: " },
|
|
|
|
{ ms(M.stats[stat].time) .. "\n", "Bold" },
|
|
|
|
{ "* avg time: " },
|
|
|
|
{ ms(M.stats[stat].time / M.stats[stat].total) .. "\n", "Bold" },
|
|
|
|
})
|
|
|
|
for k, v in pairs(M.stats[stat]) do
|
|
|
|
if not vim.tbl_contains({ "time", "total" }, k) then
|
|
|
|
chunks[#chunks + 1] = { "* " .. k .. ":" .. string.rep(" ", 9 - #k) }
|
|
|
|
chunks[#chunks + 1] = { tostring(v) .. "\n", "Number" }
|
|
|
|
end
|
|
|
|
end
|
2023-02-13 11:01:56 +00:00
|
|
|
end
|
|
|
|
vim.api.nvim_echo(chunks, true, {})
|
2022-11-30 13:19:50 +00:00
|
|
|
end
|
|
|
|
|
2023-02-13 11:01:56 +00:00
|
|
|
M._Cache = Cache
|
2022-11-22 20:12:33 +00:00
|
|
|
|
|
|
|
return M
|