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-11-22 20:12:33 +00:00
|
|
|
|
2022-12-02 11:43:34 +00:00
|
|
|
---@class LazyCacheConfig
|
|
|
|
M.config = {
|
|
|
|
enabled = true,
|
|
|
|
path = vim.fn.stdpath("state") .. "/lazy.state",
|
|
|
|
-- choose what should be cached
|
|
|
|
-- * lazy: cache all lazy.nvim core modules and your config files
|
|
|
|
-- * init: all of the above and any module needed to init your plugins
|
|
|
|
-- * VimEnter: any module till VimEnter
|
|
|
|
-- * VeryLazy: any module till VeryLazy
|
|
|
|
-- * allthethings: all mdules. Not recommended
|
|
|
|
strategy = "VimEnter", ---@type "lazy"|"init"|"VimEnter"|"allthethings"
|
|
|
|
}
|
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.loader_idx = 2 -- 2 so preload still works
|
|
|
|
M.enabled = true
|
|
|
|
M.ttl = 3600 * 24 * 5 -- keep unused modules for up to 5 days
|
2022-11-22 20:12:33 +00:00
|
|
|
|
2022-12-02 08:20:48 +00:00
|
|
|
-- Check if we need to load this plugin
|
2022-11-22 20:12:33 +00:00
|
|
|
---@param modname string
|
2022-11-24 21:04:23 +00:00
|
|
|
---@param modpath string
|
2022-12-02 08:20:48 +00:00
|
|
|
function M.check_load(modname, modpath)
|
|
|
|
if modname:sub(1, 4) == "lazy" then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
require("lazy.core.loader").autoload(modname, modpath)
|
|
|
|
end
|
2022-11-24 21:04:23 +00:00
|
|
|
|
2022-12-02 11:43:34 +00:00
|
|
|
---@param step? string
|
|
|
|
function M.disable(step)
|
2022-12-02 08:20:48 +00:00
|
|
|
if not M.enabled then
|
2022-12-02 11:43:34 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
if step and M.config.strategy ~= step then
|
|
|
|
return
|
2022-11-22 20:12:33 +00:00
|
|
|
end
|
|
|
|
|
2022-12-02 11:43:34 +00:00
|
|
|
local idx = M.idx()
|
|
|
|
if idx then
|
|
|
|
table.remove(package.loaders, idx)
|
|
|
|
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
|
|
|
|
if entry then
|
2022-12-02 08:20:48 +00:00
|
|
|
M.check_load(modname, entry.modpath)
|
|
|
|
entry.used = os.time()
|
|
|
|
local hash = assert(M.hash(entry.modpath))
|
|
|
|
if M.eq(entry.hash, hash) then
|
|
|
|
-- found in cache and up to date
|
|
|
|
chunk, err = load(entry.chunk --[[@as string]], "@" .. entry.modpath)
|
|
|
|
return chunk or error(err)
|
|
|
|
end
|
|
|
|
-- reload from file
|
|
|
|
entry.hash = hash
|
|
|
|
chunk, err = loadfile(entry.modpath)
|
2022-11-24 21:04:23 +00:00
|
|
|
else
|
2022-12-02 08:20:48 +00:00
|
|
|
-- load the module and find its modpath
|
|
|
|
local modpath
|
|
|
|
chunk, modpath = M.find(modname)
|
|
|
|
if modpath then
|
|
|
|
entry = { hash = M.hash(modpath), modpath = modpath, used = os.time() }
|
|
|
|
M.cache[modname] = entry
|
2022-11-22 20:12:33 +00:00
|
|
|
end
|
|
|
|
end
|
2022-12-02 15:52:22 +00:00
|
|
|
if M.debug then
|
|
|
|
vim.schedule(function()
|
|
|
|
vim.notify("[cache:load] " .. modname)
|
|
|
|
end)
|
|
|
|
end
|
2022-12-02 08:20:48 +00:00
|
|
|
if entry and chunk then
|
|
|
|
M.dirty = true
|
|
|
|
entry.chunk = string.dump(chunk)
|
|
|
|
end
|
|
|
|
return chunk or error(err)
|
2022-11-22 20:12:33 +00:00
|
|
|
end
|
|
|
|
|
2022-12-02 11:43:34 +00:00
|
|
|
function M.idx()
|
2022-12-02 08:20:48 +00:00
|
|
|
-- update our loader position if needed
|
|
|
|
if package.loaders[M.loader_idx] ~= M.loader then
|
2022-12-02 11:43:34 +00:00
|
|
|
M.loader_idx = nil
|
2022-11-24 21:04:23 +00:00
|
|
|
---@diagnostic disable-next-line: no-unknown
|
2022-12-02 08:20:48 +00:00
|
|
|
for i, loader in ipairs(package.loaders) do
|
|
|
|
if loader == M.loader then
|
|
|
|
M.loader_idx = i
|
|
|
|
break
|
|
|
|
end
|
2022-11-22 20:12:33 +00:00
|
|
|
end
|
2022-11-24 21:04:23 +00:00
|
|
|
end
|
2022-12-02 11:43:34 +00:00
|
|
|
return M.loader_idx
|
|
|
|
end
|
2022-12-02 08:20:48 +00:00
|
|
|
|
2022-12-02 11:43:34 +00:00
|
|
|
---@param modname string
|
|
|
|
function M.find(modname)
|
|
|
|
if M.idx() then
|
|
|
|
-- find the module and its modpath
|
|
|
|
for i = M.loader_idx + 1, #package.loaders do
|
|
|
|
---@diagnostic disable-next-line: no-unknown
|
|
|
|
local chunk = package.loaders[i](modname)
|
|
|
|
if type(chunk) == "function" then
|
|
|
|
local info = debug.getinfo(chunk, "S")
|
|
|
|
return chunk, (info.what ~= "C" and info.source:sub(2))
|
|
|
|
end
|
2022-12-02 08:20:48 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
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
|
|
|
|
for k, v in pairs(opts.performance.cache) do
|
|
|
|
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()
|
|
|
|
table.insert(package.loaders, M.loader_idx, M.loader)
|
|
|
|
|
2022-12-02 11:43:34 +00:00
|
|
|
if M.config.strategy == "VimEnter" then
|
|
|
|
vim.api.nvim_create_autocmd("VimEnter", {
|
|
|
|
once = true,
|
|
|
|
callback = function()
|
|
|
|
-- use schedule so all other VimEnter handlers will have run
|
|
|
|
vim.schedule(function()
|
|
|
|
-- startup done, so stop caching
|
|
|
|
M.disable()
|
|
|
|
end)
|
|
|
|
end,
|
|
|
|
})
|
|
|
|
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)
|
|
|
|
return uv.fs_stat(file)
|
|
|
|
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-02 11:43:34 +00:00
|
|
|
local f = assert(uv.fs_open(M.config.path, "w", 438))
|
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)
|
|
|
|
|
|
|
|
local offset = 1
|
|
|
|
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
|