2022-11-30 21:19:50 +08:00
|
|
|
local ffi = require("ffi")
|
|
|
|
local uv = vim.loop
|
2022-11-23 04:12:33 +08:00
|
|
|
|
|
|
|
local M = {}
|
2022-11-30 21:19:50 +08:00
|
|
|
|
|
|
|
---@alias CacheHash {mtime: {sec:number, nsec:number}, size:number}
|
2023-02-13 19:01:56 +08:00
|
|
|
---@alias CacheEntry {hash:CacheHash, modpath:string, chunk:string}
|
|
|
|
|
|
|
|
---@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
|
|
|
|
|
|
|
|
M.VERSION = 1
|
|
|
|
M.path = vim.fn.stdpath("cache") .. "/lazy/luac"
|
|
|
|
M.enabled = false
|
2023-02-14 00:24:15 +08:00
|
|
|
M.stats = { find = { total = 0, time = 0, index = 0, stat = 0, not_found = 0 } }
|
2023-02-13 19:01:56 +08:00
|
|
|
|
|
|
|
---@class ModuleCache
|
|
|
|
---@field _rtp string[]
|
|
|
|
---@field _rtp_key string
|
|
|
|
local Cache = {
|
|
|
|
---@type table<string, table<string,true>>
|
|
|
|
_topmods = {},
|
|
|
|
_loadfile = loadfile,
|
2022-12-29 00:57:59 +08:00
|
|
|
}
|
2022-12-27 01:35:02 +08:00
|
|
|
|
2023-02-13 19:01:56 +08: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
|
|
|
|
local home = vim.loop.os_homedir()
|
|
|
|
if home:sub(-1) == "\\" or home:sub(-1) == "/" then
|
|
|
|
home = home:sub(1, -2)
|
2022-12-19 19:21:20 +08:00
|
|
|
end
|
2023-02-13 19:01:56 +08:00
|
|
|
path = home .. path:sub(2)
|
2022-12-19 19:21:20 +08:00
|
|
|
end
|
2023-02-13 19:01:56 +08:00
|
|
|
path = path:gsub("\\", "/"):gsub("/+", "/")
|
|
|
|
return path:sub(-1) == "/" and path:sub(1, -2) or path
|
2022-12-29 00:57:59 +08:00
|
|
|
end
|
|
|
|
|
2023-02-13 19:01:56 +08:00
|
|
|
---@private
|
|
|
|
function Cache.get_rtp()
|
|
|
|
if vim.in_fast_event() then
|
|
|
|
return Cache._rtp or {}
|
|
|
|
end
|
|
|
|
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
|
|
|
|
if path:sub(-6, -1) ~= "/after" then
|
|
|
|
Cache._rtp[#Cache._rtp + 1] = path
|
2022-12-19 19:21:20 +08:00
|
|
|
end
|
|
|
|
end
|
2023-02-13 19:01:56 +08:00
|
|
|
Cache._rtp_key = key
|
2022-12-02 16:20:48 +08:00
|
|
|
end
|
2023-02-13 19:01:56 +08:00
|
|
|
return Cache._rtp
|
2022-12-02 16:20:48 +08:00
|
|
|
end
|
2022-11-25 05:04:23 +08:00
|
|
|
|
2023-02-13 19:01:56 +08:00
|
|
|
---@param name string can be a module name, or a file name
|
|
|
|
---@private
|
|
|
|
function Cache.cache_file(name)
|
|
|
|
return M.path .. "/" .. name:gsub("[/\\:]", "%%") .. "c"
|
2023-01-03 23:17:15 +08:00
|
|
|
end
|
|
|
|
|
2023-02-13 19:01:56 +08: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,
|
|
|
|
#entry.modpath,
|
|
|
|
}
|
|
|
|
uv.fs_write(f, ffi.string(ffi.new("const uint32_t[5]", header), 20))
|
|
|
|
uv.fs_write(f, entry.modpath)
|
|
|
|
uv.fs_write(f, entry.chunk)
|
|
|
|
uv.fs_close(f)
|
2022-12-02 19:43:34 +08:00
|
|
|
end
|
|
|
|
|
2023-02-13 19:01:56 +08:00
|
|
|
---@return CacheEntry?
|
|
|
|
---@private
|
|
|
|
function Cache.read(name)
|
|
|
|
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}
|
|
|
|
local header = ffi.cast("uint32_t*", ffi.new("const char[20]", data:sub(1, 20)))
|
|
|
|
if header[0] ~= M.VERSION then
|
|
|
|
return
|
2022-12-29 16:06:23 +08:00
|
|
|
end
|
2023-02-13 19:01:56 +08:00
|
|
|
local modpath = data:sub(21, 20 + header[4])
|
|
|
|
return {
|
|
|
|
hash = { size = header[1], mtime = { sec = header[2], nsec = header[3] } },
|
|
|
|
chunk = data:sub(20 + header[4] + 1),
|
|
|
|
modpath = modpath,
|
|
|
|
}
|
2022-12-29 16:06:23 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-12-02 19:43:34 +08:00
|
|
|
---@param modname string
|
2023-02-13 19:01:56 +08:00
|
|
|
---@private
|
|
|
|
function Cache.loader(modname)
|
2022-12-30 01:26:39 +08:00
|
|
|
modname = modname:gsub("/", ".")
|
2023-02-13 19:01:56 +08:00
|
|
|
local modpath, hash = Cache.find(modname)
|
2023-02-14 00:22:00 +08:00
|
|
|
local modpath, hash
|
|
|
|
modpath, hash = Cache.find(modname)
|
2023-02-13 19:01:56 +08:00
|
|
|
if modpath then
|
2023-02-14 00:22:00 +08:00
|
|
|
return Cache.load(modname, modpath, { hash = hash })
|
2022-12-19 20:34:37 +08:00
|
|
|
end
|
2023-02-13 19:01:56 +08:00
|
|
|
return "module " .. modname .. " not found"
|
2022-12-19 20:34:37 +08:00
|
|
|
end
|
|
|
|
|
2023-02-13 19:01:56 +08:00
|
|
|
---@param filename? string
|
|
|
|
---@param mode? "b"|"t"|"bt"
|
|
|
|
---@param env? table
|
|
|
|
---@return function?, string? error_message
|
|
|
|
---@private
|
|
|
|
function Cache.loadfile(filename, mode, env)
|
|
|
|
filename = Cache.normalize(filename)
|
2023-02-14 00:22:00 +08:00
|
|
|
return Cache.load(filename, filename, { mode = mode, env = env })
|
2023-02-13 19:01:56 +08: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 20:34:37 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param modpath string
|
2023-02-14 00:22:00 +08:00
|
|
|
---@param opts? {hash?: CacheHash, mode?: "b"|"t"|"bt", env?:table, entry?: CacheEntry}
|
2022-12-19 20:34:37 +08:00
|
|
|
---@return function?, string? error_message
|
2023-02-13 19:01:56 +08:00
|
|
|
---@private
|
2023-02-14 00:22:00 +08:00
|
|
|
function Cache.load(modkey, modpath, opts)
|
2023-02-13 19:01:56 +08:00
|
|
|
opts = opts or {}
|
|
|
|
local hash = opts.hash or uv.fs_stat(modpath)
|
2022-12-19 20:34:37 +08:00
|
|
|
if not hash then
|
|
|
|
-- trigger correct error
|
2023-02-13 19:01:56 +08:00
|
|
|
return Cache._loadfile(modpath)
|
2022-12-19 20:34:37 +08:00
|
|
|
end
|
|
|
|
|
2023-02-13 19:01:56 +08:00
|
|
|
---@type function?, string?
|
|
|
|
local chunk, err
|
2023-02-14 00:22:00 +08:00
|
|
|
local entry = opts.entry or Cache.read(modkey)
|
2023-02-13 19:01:56 +08:00
|
|
|
if entry and Cache.eq(entry.hash, hash) then
|
|
|
|
-- found in cache and up to date
|
2023-02-14 00:22:00 +08:00
|
|
|
chunk, err = load(entry.chunk --[[@as string]], "@" .. entry.modpath)
|
2023-02-13 19:01:56 +08:00
|
|
|
if not (err and err:find("cannot load incompatible bytecode", 1, true)) then
|
|
|
|
return chunk, err
|
2022-12-02 16:20:48 +08:00
|
|
|
end
|
2022-11-23 04:12:33 +08:00
|
|
|
end
|
2023-02-13 19:01:56 +08:00
|
|
|
entry = { hash = hash, modpath = modpath }
|
2022-12-19 20:34:37 +08:00
|
|
|
|
2023-02-13 19:01:56 +08:00
|
|
|
chunk, err = Cache._loadfile(entry.modpath)
|
2022-12-19 20:34:37 +08:00
|
|
|
if chunk then
|
2022-12-02 16:20:48 +08:00
|
|
|
entry.chunk = string.dump(chunk)
|
2023-02-14 00:22:00 +08:00
|
|
|
Cache.write(modkey, entry)
|
2022-12-02 16:20:48 +08:00
|
|
|
end
|
2022-12-19 20:34:37 +08:00
|
|
|
return chunk, err
|
2022-11-23 04:12:33 +08:00
|
|
|
end
|
|
|
|
|
2023-01-02 03:19:09 +08:00
|
|
|
---@param modname string
|
2023-02-13 19:01:56 +08:00
|
|
|
---@param opts? CacheFindOpts
|
|
|
|
---@return string? modpath, CacheHash? hash
|
|
|
|
function Cache.find(modname, opts)
|
2023-01-02 03:19:09 +08:00
|
|
|
opts = opts or {}
|
2022-12-29 00:57:59 +08:00
|
|
|
local start = uv.hrtime()
|
2023-02-14 00:24:15 +08:00
|
|
|
M.stats.find.total = M.stats.find.total + 1
|
2023-02-13 19:01:56 +08:00
|
|
|
modname = modname:gsub("/", ".")
|
2022-12-19 20:34:37 +08:00
|
|
|
local basename = modname:gsub("%.", "/")
|
2022-12-29 00:57:59 +08:00
|
|
|
local idx = modname:find(".", 1, true)
|
|
|
|
local topmod = idx and modname:sub(1, idx - 1) or modname
|
|
|
|
|
2023-02-13 19:01:56 +08: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" })
|
|
|
|
local rtp = opts.rtp ~= false and Cache.get_rtp() or {}
|
|
|
|
if opts.paths then
|
|
|
|
rtp = vim.deepcopy(rtp)
|
|
|
|
for _, dir in ipairs(opts.paths) do
|
|
|
|
rtp[#rtp + 1] = Cache.normalize(dir)
|
2022-12-29 00:57:59 +08:00
|
|
|
end
|
|
|
|
end
|
2023-02-13 19:01:56 +08:00
|
|
|
for p, pattern in ipairs(patterns) do
|
|
|
|
patterns[p] = "/lua/" .. basename .. pattern
|
|
|
|
end
|
2022-12-29 00:57:59 +08:00
|
|
|
|
2023-02-13 19:01:56 +08:00
|
|
|
for _, path in ipairs(rtp) do
|
|
|
|
if M.lsmod(path)[topmod] then
|
|
|
|
for _, pattern in ipairs(patterns) do
|
|
|
|
local modpath = path .. pattern
|
2023-02-14 00:24:15 +08:00
|
|
|
M.stats.find.stat = M.stats.find.stat + 1
|
2023-02-13 19:01:56 +08:00
|
|
|
local hash = uv.fs_stat(modpath)
|
|
|
|
if hash then
|
2023-02-14 00:24:15 +08:00
|
|
|
M.stats.find.time = M.stats.find.time + uv.hrtime() - start
|
2023-02-13 19:01:56 +08:00
|
|
|
return modpath, hash
|
2022-12-29 00:57:59 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-02-13 19:01:56 +08:00
|
|
|
-- module not found
|
2023-02-14 00:24:15 +08:00
|
|
|
M.stats.find.not_found = M.stats.find.not_found + 1
|
|
|
|
M.stats.find.time = M.stats.find.time + uv.hrtime() - start
|
2022-12-02 16:20:48 +08:00
|
|
|
end
|
|
|
|
|
2023-02-13 19:01:56 +08:00
|
|
|
--- Resets the topmods cache for the path
|
|
|
|
---@param path string
|
|
|
|
function M.reset(path)
|
|
|
|
Cache._topmods[Cache.normalize(path)] = nil
|
2022-12-19 19:21:20 +08:00
|
|
|
end
|
|
|
|
|
2023-02-13 19:01:56 +08: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
|
|
|
|
table.insert(package.loaders, 2, Cache.loader)
|
2022-12-29 08:25:05 +08:00
|
|
|
end
|
|
|
|
|
2023-02-13 19:01:56 +08:00
|
|
|
function M.disable()
|
|
|
|
if not M.enabled then
|
|
|
|
return
|
2022-12-02 19:43:34 +08:00
|
|
|
end
|
2023-02-13 19:01:56 +08: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
|
|
|
|
if loader == Cache.loader then
|
|
|
|
table.remove(package.loaders, l)
|
2022-12-29 00:57:59 +08:00
|
|
|
end
|
2022-12-02 19:43:34 +08:00
|
|
|
end
|
2022-11-30 21:19:50 +08:00
|
|
|
end
|
|
|
|
|
2023-02-13 19:01:56 +08:00
|
|
|
-- Return the top-level `/lua/*` modules for this path
|
|
|
|
---@return string[]
|
|
|
|
function M.lsmod(path)
|
|
|
|
if not Cache._topmods[path] then
|
2023-02-14 00:24:15 +08:00
|
|
|
M.stats.find.index = M.stats.find.index + 1
|
2023-02-13 19:01:56 +08:00
|
|
|
Cache._topmods[path] = {}
|
|
|
|
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
|
|
|
|
t = t or vim.loop.fs_stat(path .. "/" .. name).type
|
|
|
|
---@type string
|
|
|
|
local topname
|
|
|
|
if name:sub(-4) == ".lua" then
|
|
|
|
topname = name:sub(1, -5)
|
|
|
|
elseif t == "link" or t == "directory" then
|
|
|
|
topname = name
|
|
|
|
end
|
|
|
|
if topname then
|
|
|
|
Cache._topmods[path][topname] = true
|
|
|
|
end
|
2022-11-30 21:19:50 +08:00
|
|
|
end
|
|
|
|
end
|
2023-02-13 19:01:56 +08:00
|
|
|
return Cache._topmods[path]
|
2022-11-30 21:19:50 +08:00
|
|
|
end
|
|
|
|
|
2023-02-13 19:01:56 +08: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-15 04:38:24 +08:00
|
|
|
|
2023-02-13 19:01:56 +08:00
|
|
|
function M.inspect()
|
|
|
|
local function ms(nsec)
|
|
|
|
return math.floor(nsec / 1e6 * 1000 + 0.5) / 1000 .. "ms"
|
2022-11-30 21:19:50 +08:00
|
|
|
end
|
2023-02-13 19:01:56 +08:00
|
|
|
local props = {
|
2023-02-14 00:24:15 +08:00
|
|
|
{ "total", M.stats.find.total, "Number" },
|
|
|
|
{ "time", ms(M.stats.find.time), "Bold" },
|
|
|
|
{ "avg time", ms(M.stats.find.time / M.stats.find.total), "Bold" },
|
|
|
|
{ "index", M.stats.find.index, "Number" },
|
|
|
|
{ "fs_stat", M.stats.find.stat, "Number" },
|
|
|
|
{ "not found", M.stats.find.not_found, "Number" },
|
2023-02-13 19:01:56 +08:00
|
|
|
}
|
|
|
|
local chunks = {} ---@type string[][]
|
|
|
|
for _, prop in ipairs(props) do
|
|
|
|
chunks[#chunks + 1] = { "* " .. prop[1] .. ": " }
|
|
|
|
chunks[#chunks + 1] = { tostring(prop[2]) .. "\n", prop[3] }
|
|
|
|
end
|
|
|
|
vim.api.nvim_echo(chunks, true, {})
|
2022-11-30 21:19:50 +08:00
|
|
|
end
|
|
|
|
|
2023-02-13 19:01:56 +08:00
|
|
|
M._Cache = Cache
|
2022-11-23 04:12:33 +08:00
|
|
|
|
|
|
|
return M
|