mirror of
https://github.com/folke/lazy.nvim.git
synced 2025-04-20 05:16:45 +00:00
perf: merge module/cache and use ffi to pack cache data
This commit is contained in:
parent
4438faf9a9
commit
e1c08d64b3
4 changed files with 90 additions and 134 deletions
|
@ -1,51 +1,50 @@
|
|||
local Cache = require("lazy.core.cache")
|
||||
local ffi = require("ffi")
|
||||
---@diagnostic disable-next-line: no-unknown
|
||||
local uv = vim.loop
|
||||
|
||||
local M = {}
|
||||
M.dirty = false
|
||||
|
||||
---@type table<string, string>
|
||||
M.hashes = {}
|
||||
local cache_path = vim.fn.stdpath("state") .. "/lazy.state"
|
||||
---@type CacheHash
|
||||
local cache_hash
|
||||
|
||||
---@alias CacheHash {mtime: {sec:number, nsec:number}, size:number}
|
||||
---@alias CacheEntry {hash:CacheHash, chunk:string, used:boolean}
|
||||
---@type table<string,CacheEntry?>
|
||||
M.cache = {}
|
||||
|
||||
---@param modname string
|
||||
---@param modpath string
|
||||
---@return any
|
||||
function M.load(modname, modpath)
|
||||
local err
|
||||
---@type (string|fun())?
|
||||
local chunk = Cache.get(modname)
|
||||
local entry = M.cache[modname]
|
||||
local hash = assert(M.hash(modpath))
|
||||
|
||||
local hash = Cache.hash(modpath)
|
||||
if hash ~= M.hashes[modname] then
|
||||
M.hashes[modname] = hash
|
||||
Cache.del(modname)
|
||||
chunk = nil
|
||||
if entry and not M.eq(entry.hash, hash) then
|
||||
entry = nil
|
||||
end
|
||||
|
||||
if chunk then
|
||||
chunk, err = load(chunk --[[@as string]], "@" .. modpath, "b")
|
||||
local chunk, err
|
||||
if entry then
|
||||
entry.used = true
|
||||
chunk, err = load(entry.chunk --[[@as string]], "@" .. modpath, "b")
|
||||
else
|
||||
vim.schedule(function()
|
||||
vim.notify("loadfile(" .. modname .. ")")
|
||||
end)
|
||||
chunk, err = loadfile(modpath)
|
||||
if chunk and not err then
|
||||
Cache.set(modname, string.dump(chunk))
|
||||
if chunk then
|
||||
M.dirty = true
|
||||
M.cache[modname] = { hash = hash, chunk = string.dump(chunk), used = true }
|
||||
end
|
||||
end
|
||||
|
||||
if chunk then
|
||||
return chunk()
|
||||
else
|
||||
error(err)
|
||||
end
|
||||
return chunk and chunk() or error(err)
|
||||
end
|
||||
|
||||
function M.setup()
|
||||
-- load cache
|
||||
local value = Cache.get("cache.modules")
|
||||
if value then
|
||||
M.hashes = vim.json.decode(value)
|
||||
end
|
||||
|
||||
M.load_cache()
|
||||
-- preload core modules
|
||||
local root = vim.fn.fnamemodify(debug.getinfo(1, "S").source:sub(2), ":p:h:h")
|
||||
for _, name in ipairs({ "util", "config", "loader", "plugin", "handler" }) do
|
||||
|
@ -58,8 +57,67 @@ function M.setup()
|
|||
return M
|
||||
end
|
||||
|
||||
function M.save()
|
||||
Cache.set("cache.modules", vim.json.encode(M.hashes))
|
||||
---@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()
|
||||
local f = assert(uv.fs_open(cache_path, "w", 438))
|
||||
vim.loop.fs_ftruncate(f, 0)
|
||||
for modname, entry in pairs(M.cache) do
|
||||
if entry.used then
|
||||
entry.modname = modname
|
||||
local header = { entry.hash.size, entry.hash.mtime.sec, entry.hash.mtime.nsec, #modname, #entry.chunk }
|
||||
uv.fs_write(f, ffi.string(ffi.new("const uint32_t[5]", header), 20))
|
||||
uv.fs_write(f, modname)
|
||||
uv.fs_write(f, entry.chunk)
|
||||
end
|
||||
end
|
||||
uv.fs_close(f)
|
||||
end
|
||||
|
||||
function M.load_cache()
|
||||
M.cache = {}
|
||||
local f = uv.fs_open(cache_path, "r", 438)
|
||||
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
|
||||
local header = ffi.cast("uint32_t*", ffi.new("const char[20]", data:sub(offset, offset + 19)))
|
||||
offset = offset + 20
|
||||
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]
|
||||
M.cache[modname] = { hash = { size = header[0], mtime = { sec = header[1], nsec = header[2] } }, chunk = chunk }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
function M.autosave()
|
||||
vim.api.nvim_create_autocmd("VimLeavePre", {
|
||||
callback = function()
|
||||
if M.dirty then
|
||||
local hash = M.hash(cache_path)
|
||||
-- abort when the file was changed in the meantime
|
||||
if hash == nil or M.eq(cache_hash, hash) then
|
||||
vim.fn.system("echo start >> foo.txt")
|
||||
M.save_cache()
|
||||
vim.fn.system("echo stop >> foo.txt")
|
||||
end
|
||||
end
|
||||
end,
|
||||
})
|
||||
end
|
||||
|
||||
return M
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue