2022-11-30 13:19:50 +00:00
|
|
|
local ffi = require("ffi")
|
|
|
|
---@diagnostic disable-next-line: no-unknown
|
|
|
|
local uv = vim.loop
|
2022-11-22 20:12:33 +00:00
|
|
|
|
|
|
|
local M = {}
|
2022-11-30 13:19:50 +00:00
|
|
|
M.dirty = false
|
2022-12-19 11:21:20 +00:00
|
|
|
M.VERSION = "1"
|
2022-11-22 20:12:33 +00:00
|
|
|
|
2022-12-02 11:43:34 +00:00
|
|
|
---@class LazyCacheConfig
|
|
|
|
M.config = {
|
|
|
|
enabled = true,
|
2022-12-19 12:55:00 +00:00
|
|
|
path = vim.fn.stdpath("state") .. "/lazy/cache",
|
2022-12-03 17:59:28 +00:00
|
|
|
-- Once one of the following events triggers, caching will be disabled.
|
|
|
|
-- To cache all modules, set this to `{}`, but that is not recommended.
|
|
|
|
-- The default is to disable on:
|
|
|
|
-- * VimEnter: not useful to cache anything else beyond startup
|
|
|
|
-- * BufReadPre: this will be triggered early when opening a file from the command line directly
|
|
|
|
disable_events = { "VimEnter", "BufReadPre" },
|
2022-12-02 11:43:34 +00:00
|
|
|
}
|
2022-12-02 15:52:22 +00:00
|
|
|
M.debug = false
|
2022-12-02 11:43:34 +00:00
|
|
|
|
2022-11-30 13:19:50 +00:00
|
|
|
---@type CacheHash
|
|
|
|
local cache_hash
|
|
|
|
|
|
|
|
---@alias CacheHash {mtime: {sec:number, nsec:number}, size:number}
|
2022-12-02 08:20:48 +00:00
|
|
|
---@alias CacheEntry {hash:CacheHash, modpath:string, chunk:string, used:number}
|
2022-11-30 13:19:50 +00:00
|
|
|
---@type table<string,CacheEntry?>
|
|
|
|
M.cache = {}
|
2022-12-02 08:20:48 +00:00
|
|
|
M.enabled = true
|
|
|
|
M.ttl = 3600 * 24 * 5 -- keep unused modules for up to 5 days
|
2022-12-19 11:21:20 +00:00
|
|
|
---@type string[]
|
|
|
|
M.rtp = nil
|
2022-12-19 12:34:37 +00:00
|
|
|
-- selene:allow(global_usage)
|
|
|
|
M._loadfile = _G.loadfile
|
2022-12-19 11:21:20 +00:00
|
|
|
|
|
|
|
-- checks wether the cached modpath is still valid
|
|
|
|
function M.check_path(modname, modpath)
|
|
|
|
-- check rtp exlcuding plugins. This is a very small list, so should be fast
|
|
|
|
for _, path in ipairs(M.get_rtp()) do
|
|
|
|
if modpath:find(path, 1, true) == 1 then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
2022-11-22 20:12:33 +00:00
|
|
|
|
2022-12-19 11:21:20 +00:00
|
|
|
-- the correct lazy path should be part of rtp.
|
|
|
|
-- so if we get here, this is folke using the local dev instance ;)
|
2022-12-02 08:20:48 +00:00
|
|
|
if modname:sub(1, 4) == "lazy" then
|
2022-12-19 11:21:20 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
-- check plugins. Again fast, since we check the plugin name from the path.
|
|
|
|
-- only needed when the plugin mod has been loaded
|
|
|
|
if package.loaded["lazy.core.plugin"] then
|
|
|
|
local plugin = require("lazy.core.plugin").find(modpath)
|
|
|
|
if plugin and modpath:find(plugin.dir, 1, true) == 1 then
|
|
|
|
if not plugin._.loaded then
|
|
|
|
require("lazy.core.loader").load(plugin, { require = modname })
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
2022-12-02 08:20:48 +00:00
|
|
|
end
|
2022-12-19 11:21:20 +00:00
|
|
|
return false
|
2022-12-02 08:20:48 +00:00
|
|
|
end
|
2022-11-24 21:04:23 +00:00
|
|
|
|
2022-12-03 17:59:28 +00:00
|
|
|
function M.disable()
|
2022-12-02 08:20:48 +00:00
|
|
|
if not M.enabled then
|
2022-12-02 11:43:34 +00:00
|
|
|
return
|
|
|
|
end
|
2022-12-19 12:34:37 +00:00
|
|
|
-- selene:allow(global_usage)
|
|
|
|
_G.loadfile = M._loadfile
|
|
|
|
---@diagnostic disable-next-line: no-unknown
|
|
|
|
for i, loader in ipairs(package.loaders) do
|
|
|
|
if loader == M.loader then
|
|
|
|
table.remove(package.loaders, i)
|
|
|
|
break
|
|
|
|
end
|
2022-12-02 11:43:34 +00:00
|
|
|
end
|
|
|
|
M.enabled = false
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param modname string
|
|
|
|
---@return any
|
|
|
|
function M.loader(modname)
|
2022-12-02 08:20:48 +00:00
|
|
|
local entry = M.cache[modname]
|
|
|
|
|
2022-11-30 13:19:50 +00:00
|
|
|
local chunk, err
|
2022-12-19 11:21:20 +00:00
|
|
|
if entry and M.check_path(modname, entry.modpath) then
|
2022-12-19 12:34:37 +00:00
|
|
|
chunk, err = M.load(modname, entry.modpath)
|
|
|
|
else
|
|
|
|
-- find the modpath and load the module
|
|
|
|
local modpath = M.find(modname)
|
|
|
|
if modpath then
|
|
|
|
chunk, err = M.load(modname, modpath)
|
2022-12-02 18:16:21 +00:00
|
|
|
end
|
2022-12-19 12:34:37 +00:00
|
|
|
end
|
|
|
|
return chunk or (err and error(err)) or "not found in lazy cache"
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param modpath string
|
|
|
|
---@return any, string?
|
|
|
|
function M.loadfile(modpath)
|
|
|
|
return M.load(modpath, modpath)
|
|
|
|
end
|
|
|
|
|
|
|
|
---@param modkey string
|
|
|
|
---@param modpath string
|
|
|
|
---@return function?, string? error_message
|
|
|
|
function M.load(modkey, modpath)
|
|
|
|
local hash = M.hash(modpath)
|
|
|
|
if not hash then
|
|
|
|
-- trigger correct error
|
|
|
|
return M._loadfile(modpath)
|
|
|
|
end
|
|
|
|
|
|
|
|
local entry = M.cache[modkey]
|
|
|
|
if entry then
|
|
|
|
entry.used = os.time()
|
2022-12-02 08:20:48 +00:00
|
|
|
if M.eq(entry.hash, hash) then
|
|
|
|
-- found in cache and up to date
|
2022-12-19 12:34:37 +00:00
|
|
|
return loadstring(entry.chunk --[[@as string]], "@" .. entry.modpath)
|
2022-12-02 08:20:48 +00:00
|
|
|
end
|
2022-11-24 21:04:23 +00:00
|
|
|
else
|
2022-12-19 12:34:37 +00:00
|
|
|
entry = { hash = hash, modpath = modpath, used = os.time() }
|
|
|
|
M.cache[modkey] = entry
|
2022-11-22 20:12:33 +00:00
|
|
|
end
|
2022-12-19 12:34:37 +00:00
|
|
|
entry.hash = hash
|
|
|
|
|
2022-12-02 15:52:22 +00:00
|
|
|
if M.debug then
|
|
|
|
vim.schedule(function()
|
2022-12-19 12:34:37 +00:00
|
|
|
vim.notify("[cache:load] " .. modpath)
|
2022-12-02 15:52:22 +00:00
|
|
|
end)
|
|
|
|
end
|
2022-12-19 12:34:37 +00:00
|
|
|
|
|
|
|
local chunk, err = M._loadfile(entry.modpath)
|
|
|
|
if chunk then
|
2022-12-02 08:20:48 +00:00
|
|
|
M.dirty = true
|
|
|
|
entry.chunk = string.dump(chunk)
|
|
|
|
end
|
2022-12-19 12:34:37 +00:00
|
|
|
return chunk, err
|
2022-11-22 20:12:33 +00:00
|
|
|
end
|
|
|
|
|
2022-12-16 14:08:09 +00:00
|
|
|
function M.require(modname)
|
|
|
|
return M.loader(modname)()
|
|
|
|
end
|
|
|
|
|
2022-12-02 11:43:34 +00:00
|
|
|
---@param modname string
|
2022-12-19 12:34:37 +00:00
|
|
|
---@return string?
|
2022-12-02 11:43:34 +00:00
|
|
|
function M.find(modname)
|
2022-12-19 12:34:37 +00:00
|
|
|
local basename = modname:gsub("%.", "/")
|
|
|
|
local paths = { "lua/" .. basename .. ".lua", "lua/" .. basename .. "/init.lua" }
|
|
|
|
return vim.api.nvim__get_runtime(paths, false, { is_lua = true })[1]
|
2022-12-02 08:20:48 +00:00
|
|
|
end
|
|
|
|
|
2022-12-19 12:34:37 +00:00
|
|
|
-- returns the cached RTP excluding plugin dirs
|
2022-12-19 11:21:20 +00:00
|
|
|
function M.get_rtp()
|
|
|
|
if not M.rtp then
|
|
|
|
M.rtp = {}
|
2022-12-19 12:34:37 +00:00
|
|
|
---@type table<string,true>
|
2022-12-19 11:21:20 +00:00
|
|
|
local skip = {}
|
|
|
|
-- only skip plugins once Config has been setup
|
|
|
|
if package.loaded["lazy.core.config"] then
|
|
|
|
local Config = require("lazy.core.config")
|
2022-12-19 12:34:37 +00:00
|
|
|
for _, plugin in pairs(Config.plugins) do
|
2022-12-19 11:21:20 +00:00
|
|
|
if plugin.name ~= "lazy.nvim" then
|
|
|
|
skip[plugin.dir] = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
for _, path in ipairs(vim.api.nvim_list_runtime_paths()) do
|
|
|
|
if not skip[path] then
|
|
|
|
M.rtp[#M.rtp + 1] = path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return M.rtp
|
|
|
|
end
|
|
|
|
|
2022-12-02 11:43:34 +00:00
|
|
|
---@param opts? LazyConfig
|
|
|
|
function M.setup(opts)
|
|
|
|
-- no fancy deep extend here. just set the options
|
|
|
|
if opts and opts.performance and opts.performance.cache then
|
2022-12-19 12:34:37 +00:00
|
|
|
---@diagnostic disable-next-line: no-unknown
|
2022-12-02 11:43:34 +00:00
|
|
|
for k, v in pairs(opts.performance.cache) do
|
2022-12-19 12:34:37 +00:00
|
|
|
---@diagnostic disable-next-line: no-unknown
|
2022-12-02 11:43:34 +00:00
|
|
|
M.config[k] = v
|
|
|
|
end
|
|
|
|
end
|
2022-12-02 15:52:22 +00:00
|
|
|
M.debug = opts and opts.debug
|
2022-12-02 11:43:34 +00:00
|
|
|
|
2022-12-02 08:20:48 +00:00
|
|
|
M.load_cache()
|
2022-12-19 12:34:37 +00:00
|
|
|
table.insert(package.loaders, 2, M.loader)
|
|
|
|
-- selene:allow(global_usage)
|
|
|
|
_G.loadfile = M.loadfile
|
2022-12-02 08:20:48 +00:00
|
|
|
|
2022-12-19 11:21:20 +00:00
|
|
|
-- reset rtp when it changes
|
|
|
|
vim.api.nvim_create_autocmd("OptionSet", {
|
|
|
|
pattern = "runtimepath",
|
|
|
|
callback = function()
|
|
|
|
M.rtp = nil
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
|
2022-12-03 17:59:28 +00:00
|
|
|
if #M.config.disable_events > 0 then
|
|
|
|
vim.api.nvim_create_autocmd(M.config.disable_events, { once = true, callback = M.disable })
|
2022-12-02 11:43:34 +00:00
|
|
|
end
|
|
|
|
return M
|
2022-11-22 20:12:33 +00:00
|
|
|
end
|
|
|
|
|
2022-11-30 13:19:50 +00:00
|
|
|
---@return CacheHash?
|
|
|
|
function M.hash(file)
|
2022-12-02 18:16:21 +00:00
|
|
|
local ok, ret = pcall(uv.fs_stat, file)
|
|
|
|
return ok and ret or nil
|
2022-11-30 13:19:50 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
---@param h1 CacheHash
|
|
|
|
---@param h2 CacheHash
|
|
|
|
function M.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
|
|
|
|
end
|
|
|
|
|
|
|
|
function M.save_cache()
|
2022-12-19 12:55:00 +00:00
|
|
|
vim.fn.mkdir(vim.fn.fnamemodify(M.config.path, ":p:h"), "p")
|
2022-12-02 11:43:34 +00:00
|
|
|
local f = assert(uv.fs_open(M.config.path, "w", 438))
|
2022-12-19 11:21:20 +00:00
|
|
|
uv.fs_write(f, M.VERSION)
|
2022-12-14 20:38:24 +00:00
|
|
|
uv.fs_write(f, "\0")
|
2022-11-30 13:19:50 +00:00
|
|
|
for modname, entry in pairs(M.cache) do
|
2022-12-02 08:20:48 +00:00
|
|
|
if entry.used > os.time() - M.ttl then
|
2022-11-30 13:19:50 +00:00
|
|
|
entry.modname = modname
|
2022-12-02 08:20:48 +00:00
|
|
|
local header = {
|
|
|
|
entry.hash.size,
|
|
|
|
entry.hash.mtime.sec,
|
|
|
|
entry.hash.mtime.nsec,
|
|
|
|
#modname,
|
|
|
|
#entry.chunk,
|
|
|
|
#entry.modpath,
|
|
|
|
entry.used,
|
|
|
|
}
|
|
|
|
uv.fs_write(f, ffi.string(ffi.new("const uint32_t[7]", header), 28))
|
2022-11-30 13:19:50 +00:00
|
|
|
uv.fs_write(f, modname)
|
|
|
|
uv.fs_write(f, entry.chunk)
|
2022-12-02 08:20:48 +00:00
|
|
|
uv.fs_write(f, entry.modpath)
|
2022-11-30 13:19:50 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
uv.fs_close(f)
|
|
|
|
end
|
|
|
|
|
|
|
|
function M.load_cache()
|
|
|
|
M.cache = {}
|
2022-12-02 11:43:34 +00:00
|
|
|
local f = uv.fs_open(M.config.path, "r", 438)
|
2022-11-30 13:19:50 +00:00
|
|
|
if f then
|
|
|
|
cache_hash = uv.fs_fstat(f) --[[@as CacheHash]]
|
|
|
|
local data = uv.fs_read(f, cache_hash.size, 0) --[[@as string]]
|
|
|
|
uv.fs_close(f)
|
|
|
|
|
2022-12-14 20:38:24 +00:00
|
|
|
local zero = data:find("\0", 1, true)
|
|
|
|
if not zero then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2022-12-19 11:21:20 +00:00
|
|
|
if M.VERSION ~= data:sub(1, zero - 1) then
|
2022-12-14 20:38:24 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local offset = zero + 1
|
2022-11-30 13:19:50 +00:00
|
|
|
while offset + 1 < #data do
|
2022-12-02 08:20:48 +00:00
|
|
|
local header = ffi.cast("uint32_t*", ffi.new("const char[28]", data:sub(offset, offset + 27)))
|
|
|
|
offset = offset + 28
|
2022-11-30 13:19:50 +00:00
|
|
|
local modname = data:sub(offset, offset + header[3] - 1)
|
|
|
|
offset = offset + header[3]
|
|
|
|
local chunk = data:sub(offset, offset + header[4] - 1)
|
|
|
|
offset = offset + header[4]
|
2022-12-02 08:20:48 +00:00
|
|
|
local file = data:sub(offset, offset + header[5] - 1)
|
|
|
|
offset = offset + header[5]
|
|
|
|
M.cache[modname] = {
|
|
|
|
hash = { size = header[0], mtime = { sec = header[1], nsec = header[2] } },
|
|
|
|
chunk = chunk,
|
|
|
|
modpath = file,
|
|
|
|
used = header[6],
|
|
|
|
}
|
2022-11-30 13:19:50 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function M.autosave()
|
|
|
|
vim.api.nvim_create_autocmd("VimLeavePre", {
|
|
|
|
callback = function()
|
|
|
|
if M.dirty then
|
2022-12-02 11:43:34 +00:00
|
|
|
local hash = M.hash(M.config.path)
|
2022-11-30 13:19:50 +00:00
|
|
|
-- abort when the file was changed in the meantime
|
|
|
|
if hash == nil or M.eq(cache_hash, hash) then
|
|
|
|
M.save_cache()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
})
|
2022-11-22 20:12:33 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return M
|