mirror of https://github.com/folke/lazy.nvim.git
perf: new file-based cache that ensures correct rtp order (#532)
* perf: new file-based cache that ensures rtp is alweays correct and will cache all files, including those after startup * refactor: new cache * test: fix tests * fix(cache): cache file names on Windows * feat(cache): allow to disable the cache * docs: updated cache settings
This commit is contained in:
parent
06f835d0b4
commit
462633bae1
10
README.md
10
README.md
|
@ -347,7 +347,7 @@ return {
|
||||||
init = " ",
|
init = " ",
|
||||||
import = " ",
|
import = " ",
|
||||||
keys = " ",
|
keys = " ",
|
||||||
lazy = "鈴 ",
|
lazy = " ",
|
||||||
loaded = "●",
|
loaded = "●",
|
||||||
not_loaded = "○",
|
not_loaded = "○",
|
||||||
plugin = " ",
|
plugin = " ",
|
||||||
|
@ -409,14 +409,6 @@ return {
|
||||||
performance = {
|
performance = {
|
||||||
cache = {
|
cache = {
|
||||||
enabled = true,
|
enabled = true,
|
||||||
path = vim.fn.stdpath("cache") .. "/lazy/cache",
|
|
||||||
-- 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 = { "UIEnter", "BufReadPre" },
|
|
||||||
ttl = 3600 * 24 * 5, -- keep unused modules for up to 5 days
|
|
||||||
},
|
},
|
||||||
reset_packpath = true, -- reset the package path to improve startup time
|
reset_packpath = true, -- reset the package path to improve startup time
|
||||||
rtp = {
|
rtp = {
|
||||||
|
|
|
@ -1,240 +1,264 @@
|
||||||
local ffi = require("ffi")
|
local ffi = require("ffi")
|
||||||
---@diagnostic disable-next-line: no-unknown
|
|
||||||
local uv = vim.loop
|
local uv = vim.loop
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
M.dirty = false
|
|
||||||
M.VERSION = "1" .. jit.version
|
|
||||||
|
|
||||||
---@class LazyCacheConfig
|
|
||||||
M.config = {
|
|
||||||
enabled = true,
|
|
||||||
path = vim.fn.stdpath("cache") .. "/lazy/cache",
|
|
||||||
-- 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 = { "UIEnter", "BufReadPre" },
|
|
||||||
ttl = 3600 * 24 * 5, -- keep unused modules for up to 5 days
|
|
||||||
}
|
|
||||||
M.debug = false
|
|
||||||
|
|
||||||
---@type CacheHash
|
|
||||||
local cache_hash
|
|
||||||
|
|
||||||
---@alias CacheHash {mtime: {sec:number, nsec:number}, size:number}
|
---@alias CacheHash {mtime: {sec:number, nsec:number}, size:number}
|
||||||
---@alias CacheEntry {hash:CacheHash, modpath:string, chunk:string, used:number}
|
---@alias CacheEntry {hash:CacheHash, modpath:string, chunk:string}
|
||||||
---@type table<string,CacheEntry?>
|
|
||||||
M.cache = {}
|
---@class CacheFindOpts
|
||||||
M.enabled = true
|
---@field rtp? boolean Search for modname in the runtime path (defaults to `true`)
|
||||||
---@type string[]
|
---@field patterns? string[] Paterns to use (defaults to `{"/init.lua", ".lua"}`)
|
||||||
M.rtp = nil
|
---@field paths? string[] Extra paths to search for modname
|
||||||
M.rtp_total = 0
|
|
||||||
M.stats = {
|
M.VERSION = 1
|
||||||
find = { total = 0, time = 0, rtp = 0, unloaded = 0, index = 0, stat = 0, not_found = 0 },
|
M.path = vim.fn.stdpath("cache") .. "/lazy/luac"
|
||||||
autoload = { total = 0, time = 0 },
|
M.enabled = false
|
||||||
|
M.stats = { total = 0, time = 0, index = 0, stat = 0, not_found = 0 }
|
||||||
|
|
||||||
|
---@class ModuleCache
|
||||||
|
---@field _rtp string[]
|
||||||
|
---@field _rtp_key string
|
||||||
|
local Cache = {
|
||||||
|
---@type table<string, table<string,true>>
|
||||||
|
_topmods = {},
|
||||||
|
_loadfile = loadfile,
|
||||||
}
|
}
|
||||||
M.me = debug.getinfo(1, "S").source:sub(2)
|
|
||||||
M.me = vim.fn.fnamemodify(M.me, ":p:h:h:h:h"):gsub("\\", "/")
|
|
||||||
---@type table<string, string[]>
|
|
||||||
M.topmods = { lazy = { M.me } }
|
|
||||||
---@type table<string, string[]>
|
|
||||||
M.indexed = { [M.me] = { "lazy" } }
|
|
||||||
M.indexed_unloaded = false
|
|
||||||
M.indexed_rtp = 0
|
|
||||||
-- selene:allow(global_usage)
|
|
||||||
M._loadfile = _G.loadfile
|
|
||||||
|
|
||||||
-- checks whether the cached modpath is still valid
|
-- slightly faster/different version than vim.fs.normalize
|
||||||
function M.check_path(modname, modpath)
|
-- we also need to have it here, since the cache will load vim.fs
|
||||||
-- HACK: never return packer paths
|
---@private
|
||||||
if modpath:find("/site/pack/packer/", 1, true) then
|
function Cache.normalize(path)
|
||||||
return false
|
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)
|
||||||
end
|
end
|
||||||
|
path = home .. path:sub(2)
|
||||||
-- check rtp excluding 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
|
||||||
end
|
path = path:gsub("\\", "/"):gsub("/+", "/")
|
||||||
|
return path:sub(-1) == "/" and path:sub(1, -2) or path
|
||||||
-- the correct lazy path should be part of rtp.
|
|
||||||
-- so if we get here, this is folke using the local dev instance ;)
|
|
||||||
if modname and (modname == "lazy" or modname:sub(1, 5) == "lazy.") then
|
|
||||||
return false
|
|
||||||
end
|
|
||||||
|
|
||||||
return modname and M.check_autoload(modname, modpath)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.check_autoload(modname, modpath)
|
---@private
|
||||||
local start = uv.hrtime()
|
function Cache.get_rtp()
|
||||||
M.stats.autoload.total = M.stats.autoload.total + 1
|
if vim.in_fast_event() then
|
||||||
|
return Cache._rtp or {}
|
||||||
-- check plugins. Again fast, since we check the plugin name from the path.
|
|
||||||
-- only needed when the plugin mod has been loaded
|
|
||||||
---@type LazyCorePlugin
|
|
||||||
local Plugin = package.loaded["lazy.core.plugin"]
|
|
||||||
if Plugin then
|
|
||||||
local plugin = Plugin.find(modpath)
|
|
||||||
if plugin and modpath:find(plugin.dir, 1, true) == 1 then
|
|
||||||
-- we're not interested in loader time, so calculate delta here
|
|
||||||
M.stats.autoload.time = M.stats.autoload.time + uv.hrtime() - start
|
|
||||||
-- don't load if we're loading specs or if the plugin is already loaded
|
|
||||||
if not (Plugin.loading or plugin._.loaded) then
|
|
||||||
if plugin.module == false then
|
|
||||||
error("Plugin " .. plugin.name .. " is not loaded and is configured with module=false")
|
|
||||||
end
|
end
|
||||||
require("lazy.core.loader").load(plugin, { require = modname })
|
local key = vim.go.rtp
|
||||||
end
|
if key ~= Cache._rtp_key then
|
||||||
return true
|
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
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
M.stats.autoload.time = M.stats.autoload.time + uv.hrtime() - start
|
Cache._rtp_key = key
|
||||||
return false
|
end
|
||||||
|
return Cache._rtp
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.disable()
|
---@param name string can be a module name, or a file name
|
||||||
if not M.enabled then
|
---@private
|
||||||
|
function Cache.cache_file(name)
|
||||||
|
return M.path .. "/" .. name:gsub("[/\\:]", "%%") .. "c"
|
||||||
|
end
|
||||||
|
|
||||||
|
---@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)
|
||||||
|
end
|
||||||
|
|
||||||
|
---@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
|
return
|
||||||
end
|
end
|
||||||
-- selene:allow(global_usage)
|
local modpath = data:sub(21, 20 + header[4])
|
||||||
_G.loadfile = M._loadfile
|
return {
|
||||||
M.enabled = false
|
hash = { size = header[1], mtime = { sec = header[2], nsec = header[3] } },
|
||||||
if M.debug and vim.tbl_count(M.topmods) > 1 then
|
chunk = data:sub(20 + header[4] + 1),
|
||||||
M.log(M.topmods, { level = vim.log.levels.WARN, title = "topmods" })
|
modpath = modpath,
|
||||||
end
|
}
|
||||||
if M.debug and false then
|
|
||||||
local stats = vim.deepcopy(M.stats)
|
|
||||||
stats.time = (stats.time or 0) / 1e6
|
|
||||||
stats.find.time = (stats.find.time or 0) / 1e6
|
|
||||||
stats.autoload.time = (stats.autoload.time or 0) / 1e6
|
|
||||||
M.log(stats, { title = "stats" })
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
---@param msg string|table
|
|
||||||
---@param opts? LazyNotifyOpts
|
|
||||||
function M.log(msg, opts)
|
|
||||||
if M.debug then
|
|
||||||
msg = vim.deepcopy(msg)
|
|
||||||
vim.schedule(function()
|
|
||||||
require("lazy.core.util").debug(msg, opts)
|
|
||||||
end)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function M.check_loaded(modname)
|
|
||||||
---@diagnostic disable-next-line: no-unknown
|
|
||||||
local mod = package.loaded[modname]
|
|
||||||
if type(mod) == "table" then
|
|
||||||
return function()
|
|
||||||
return mod
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param modname string
|
---@param modname string
|
||||||
---@return fun()|string
|
---@private
|
||||||
function M.loader(modname)
|
function Cache.loader(modname)
|
||||||
modname = modname:gsub("/", ".")
|
modname = modname:gsub("/", ".")
|
||||||
local entry = M.cache[modname]
|
local modpath, hash = Cache.find(modname)
|
||||||
|
|
||||||
local chunk, err
|
|
||||||
if entry then
|
|
||||||
if M.check_path(modname, entry.modpath) then
|
|
||||||
M.stats.find.total = M.stats.find.total + 1
|
|
||||||
chunk, err = M.load(modname, entry.modpath)
|
|
||||||
else
|
|
||||||
M.cache[modname] = nil
|
|
||||||
M.dirty = true
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if not chunk then
|
|
||||||
-- find the modpath and load the module
|
|
||||||
local modpath = M.find(modname)
|
|
||||||
if modpath then
|
if modpath then
|
||||||
M.check_autoload(modname, modpath)
|
return Cache.load(modpath, { hash = hash })
|
||||||
if M.enabled then
|
|
||||||
chunk, err = M.load(modname, modpath)
|
|
||||||
else
|
|
||||||
chunk = M.check_loaded(modname)
|
|
||||||
if not chunk then
|
|
||||||
chunk, err = M._loadfile(modpath)
|
|
||||||
end
|
end
|
||||||
end
|
return "module " .. modname .. " not found"
|
||||||
end
|
|
||||||
end
|
|
||||||
return chunk or err or ("module " .. modname .. " not found")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param modpath string
|
---@param filename? string
|
||||||
---@return any, string?
|
---@param mode? "b"|"t"|"bt"
|
||||||
function M.loadfile(modpath)
|
---@param env? table
|
||||||
modpath = modpath:gsub("\\", "/")
|
|
||||||
return M.load(modpath, modpath)
|
|
||||||
end
|
|
||||||
|
|
||||||
---@param modkey string
|
|
||||||
---@param modpath string
|
|
||||||
---@return function?, string? error_message
|
---@return function?, string? error_message
|
||||||
function M.load(modkey, modpath)
|
---@private
|
||||||
local chunk, err
|
function Cache.loadfile(filename, mode, env)
|
||||||
chunk = M.check_loaded(modkey)
|
filename = Cache.normalize(filename)
|
||||||
if chunk then
|
return Cache.load(filename, { mode = mode, env = env })
|
||||||
return chunk
|
end
|
||||||
end
|
|
||||||
modpath = modpath:gsub("\\", "/")
|
---@param h1 CacheHash
|
||||||
local hash = M.hash(modpath)
|
---@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
|
||||||
|
end
|
||||||
|
|
||||||
|
---@param modpath string
|
||||||
|
---@param opts? {hash?: CacheHash, mode?: "b"|"t"|"bt", env?:table}
|
||||||
|
---@return function?, string? error_message
|
||||||
|
---@private
|
||||||
|
function Cache.load(modpath, opts)
|
||||||
|
opts = opts or {}
|
||||||
|
local hash = opts.hash or uv.fs_stat(modpath)
|
||||||
if not hash then
|
if not hash then
|
||||||
-- trigger correct error
|
-- trigger correct error
|
||||||
return M._loadfile(modpath)
|
return Cache._loadfile(modpath)
|
||||||
end
|
end
|
||||||
|
|
||||||
local entry = M.cache[modkey]
|
---@type function?, string?
|
||||||
if entry then
|
local chunk, err
|
||||||
entry.modpath = modpath
|
local entry = Cache.read(modpath)
|
||||||
entry.used = os.time()
|
if entry and Cache.eq(entry.hash, hash) then
|
||||||
if M.eq(entry.hash, hash) then
|
|
||||||
-- found in cache and up to date
|
-- found in cache and up to date
|
||||||
chunk, err = loadstring(entry.chunk --[[@as string]], "@" .. entry.modpath)
|
chunk, err = loadstring(entry.chunk --[[@as string]], "@" .. entry.modpath)
|
||||||
if not (err and err:find("cannot load incompatible bytecode", 1, true)) then
|
if not (err and err:find("cannot load incompatible bytecode", 1, true)) then
|
||||||
return chunk, err
|
return chunk, err
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
else
|
entry = { hash = hash, modpath = modpath }
|
||||||
entry = { hash = hash, modpath = modpath, used = os.time() }
|
|
||||||
M.cache[modkey] = entry
|
|
||||||
end
|
|
||||||
entry.hash = hash
|
|
||||||
|
|
||||||
if M.debug then
|
chunk, err = Cache._loadfile(entry.modpath)
|
||||||
M.log("`" .. modpath .. "`", { level = vim.log.levels.WARN, title = "Cache.load" })
|
|
||||||
end
|
|
||||||
|
|
||||||
chunk, err = M._loadfile(entry.modpath)
|
|
||||||
M.dirty = true
|
|
||||||
if chunk then
|
if chunk then
|
||||||
entry.chunk = string.dump(chunk)
|
entry.chunk = string.dump(chunk)
|
||||||
else
|
Cache.write(modpath, entry)
|
||||||
M.cache[modkey] = nil
|
|
||||||
end
|
end
|
||||||
return chunk, err
|
return chunk, err
|
||||||
end
|
end
|
||||||
|
|
||||||
-- index the top-level lua modules for this path
|
---@param modname string
|
||||||
function M._index(path)
|
---@param opts? CacheFindOpts
|
||||||
if not M.indexed[path] and path:sub(-6, -1) ~= "/after" then
|
---@return string? modpath, CacheHash? hash
|
||||||
M.stats.find.index = M.stats.find.index + 1
|
function Cache.find(modname, opts)
|
||||||
---@type LazyUtilCore
|
opts = opts or {}
|
||||||
local Util = package.loaded["lazy.core.util"]
|
local start = uv.hrtime()
|
||||||
if not Util then
|
M.stats.total = M.stats.total + 1
|
||||||
return false
|
modname = modname:gsub("/", ".")
|
||||||
|
local basename = modname:gsub("%.", "/")
|
||||||
|
local idx = modname:find(".", 1, true)
|
||||||
|
local topmod = idx and modname:sub(1, idx - 1) or modname
|
||||||
|
|
||||||
|
-- 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)
|
||||||
end
|
end
|
||||||
M.indexed[path] = {}
|
end
|
||||||
Util.ls(path .. "/lua", function(_, name, t)
|
for p, pattern in ipairs(patterns) do
|
||||||
|
patterns[p] = "/lua/" .. basename .. pattern
|
||||||
|
end
|
||||||
|
|
||||||
|
for _, path in ipairs(rtp) do
|
||||||
|
if M.lsmod(path)[topmod] then
|
||||||
|
for _, pattern in ipairs(patterns) do
|
||||||
|
local modpath = path .. pattern
|
||||||
|
M.stats.stat = M.stats.stat + 1
|
||||||
|
local hash = uv.fs_stat(modpath)
|
||||||
|
if hash then
|
||||||
|
M.stats.time = M.stats.time + uv.hrtime() - start
|
||||||
|
return modpath, hash
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- module not found
|
||||||
|
M.stats.not_found = M.stats.not_found + 1
|
||||||
|
M.stats.time = M.stats.time + uv.hrtime() - start
|
||||||
|
end
|
||||||
|
|
||||||
|
--- Resets the topmods cache for the path
|
||||||
|
---@param path string
|
||||||
|
function M.reset(path)
|
||||||
|
Cache._topmods[Cache.normalize(path)] = nil
|
||||||
|
end
|
||||||
|
|
||||||
|
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)
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.disable()
|
||||||
|
if not M.enabled then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
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)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
-- Return the top-level `/lua/*` modules for this path
|
||||||
|
---@return string[]
|
||||||
|
function M.lsmod(path)
|
||||||
|
if not Cache._topmods[path] then
|
||||||
|
M.stats.index = M.stats.index + 1
|
||||||
|
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
|
local topname
|
||||||
if name:sub(-4) == ".lua" then
|
if name:sub(-4) == ".lua" then
|
||||||
topname = name:sub(1, -5)
|
topname = name:sub(1, -5)
|
||||||
|
@ -242,283 +266,41 @@ function M._index(path)
|
||||||
topname = name
|
topname = name
|
||||||
end
|
end
|
||||||
if topname then
|
if topname then
|
||||||
M.topmods[topname] = M.topmods[topname] or {}
|
Cache._topmods[path][topname] = true
|
||||||
if not vim.tbl_contains(M.topmods[topname], path) then
|
|
||||||
table.insert(M.topmods[topname], path)
|
|
||||||
end
|
|
||||||
if not vim.tbl_contains(M.indexed[path], topname) then
|
|
||||||
table.insert(M.indexed[path], topname)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end)
|
|
||||||
return true
|
|
||||||
end
|
end
|
||||||
return false
|
return Cache._topmods[path]
|
||||||
end
|
|
||||||
|
|
||||||
function M.get_topmods(path)
|
|
||||||
M._index(path)
|
|
||||||
return M.indexed[path] or {}
|
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param modname string
|
---@param modname string
|
||||||
---@return string?
|
---@param opts? CacheFindOpts
|
||||||
function M.find_root(modname)
|
---@return string? modpath
|
||||||
if M.cache[modname] then
|
|
||||||
-- check if modname is in cache
|
|
||||||
local modpath = M.cache[modname].modpath
|
|
||||||
if M.check_path(modname, modpath) and uv.fs_stat(modpath) then
|
|
||||||
local root = modpath:gsub("/init%.lua$", ""):gsub("%.lua$", "")
|
|
||||||
return root
|
|
||||||
end
|
|
||||||
else
|
|
||||||
-- in case modname is just a directory and not a real mod,
|
|
||||||
-- check for any children in the cache
|
|
||||||
for child, entry in pairs(M.cache) do
|
|
||||||
if child:find(modname, 1, true) == 1 then
|
|
||||||
if M.check_path(child, entry.modpath) and uv.fs_stat(entry.modpath) then
|
|
||||||
local basename = modname:gsub("%.", "/")
|
|
||||||
local childbase = child:gsub("%.", "/")
|
|
||||||
local ret = entry.modpath:gsub("/init%.lua$", ""):gsub("%.lua$", "")
|
|
||||||
local idx = assert(ret:find(childbase, 1, true))
|
|
||||||
return ret:sub(1, idx - 1) .. basename
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- not found in cache, so find the root with the special pattern
|
|
||||||
local modpath = M.find(modname, { patterns = { "" } })
|
|
||||||
if modpath then
|
|
||||||
local root = modpath:gsub("/init%.lua$", ""):gsub("%.lua$", "")
|
|
||||||
return root
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
---@param modname string
|
|
||||||
---@param opts? {patterns?:string[]}
|
|
||||||
---@return string?
|
|
||||||
function M.find(modname, opts)
|
function M.find(modname, opts)
|
||||||
opts = opts or {}
|
local modpath = Cache.find(modname, opts)
|
||||||
|
|
||||||
M.stats.find.total = M.stats.find.total + 1
|
|
||||||
local start = uv.hrtime()
|
|
||||||
local basename = modname:gsub("%.", "/")
|
|
||||||
local idx = modname:find(".", 1, true)
|
|
||||||
local topmod = idx and modname:sub(1, idx - 1) or modname
|
|
||||||
|
|
||||||
-- search for a directory first when topmod == modname
|
|
||||||
local patterns = topmod == modname and { "/init.lua", ".lua" } or { ".lua", "/init.lua" }
|
|
||||||
|
|
||||||
if opts.patterns then
|
|
||||||
vim.list_extend(patterns, opts.patterns)
|
|
||||||
end
|
|
||||||
|
|
||||||
-- check top-level mods to find the module
|
|
||||||
local function _find()
|
|
||||||
for _, toppath in ipairs(M.topmods[topmod] or {}) do
|
|
||||||
for _, pattern in ipairs(patterns) do
|
|
||||||
local path = toppath .. "/lua/" .. basename .. pattern
|
|
||||||
M.stats.find.stat = M.stats.find.stat + 1
|
|
||||||
if uv.fs_stat(path) then
|
|
||||||
return path
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
local modpath = _find()
|
|
||||||
if not modpath then
|
|
||||||
-- update rtp
|
|
||||||
local rtp = M.list_rtp()
|
|
||||||
if #rtp ~= M.indexed_rtp then
|
|
||||||
M.indexed_rtp = #rtp
|
|
||||||
local updated = false
|
|
||||||
for _, path in ipairs(rtp) do
|
|
||||||
updated = M._index(path) or updated
|
|
||||||
end
|
|
||||||
if updated then
|
|
||||||
modpath = _find()
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- update unloaded
|
|
||||||
if not modpath and not M.indexed_unloaded then
|
|
||||||
M.indexed_unloaded = true
|
|
||||||
local updated = false
|
|
||||||
---@type LazyCoreConfig
|
|
||||||
local Config = package.loaded["lazy.core.config"]
|
|
||||||
if Config and Config.spec then
|
|
||||||
for _, plugin in pairs(Config.spec.plugins) do
|
|
||||||
if not (M.indexed[plugin.dir] or plugin._.loaded or plugin.module == false) then
|
|
||||||
updated = M._index(plugin.dir) or updated
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if updated then
|
|
||||||
modpath = _find()
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
-- module not found
|
|
||||||
if not modpath then
|
|
||||||
M.stats.find.not_found = M.stats.find.not_found + 1
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
M.stats.find.time = M.stats.find.time + uv.hrtime() - start
|
|
||||||
return modpath
|
return modpath
|
||||||
end
|
end
|
||||||
|
|
||||||
-- returns the cached RTP excluding plugin dirs
|
function M.inspect()
|
||||||
function M.get_rtp()
|
local function ms(nsec)
|
||||||
local rtp = M.list_rtp()
|
return math.floor(nsec / 1e6 * 1000 + 0.5) / 1000 .. "ms"
|
||||||
if not M.rtp or #rtp ~= M.rtp_total then
|
|
||||||
M.rtp_total = #rtp
|
|
||||||
M.rtp = {}
|
|
||||||
---@type table<string,true>
|
|
||||||
local skip = {}
|
|
||||||
-- only skip plugins once Config has been setup
|
|
||||||
---@type LazyCoreConfig
|
|
||||||
local Config = package.loaded["lazy.core.config"]
|
|
||||||
if Config then
|
|
||||||
for _, plugin in pairs(Config.plugins) do
|
|
||||||
if plugin.name ~= "lazy.nvim" then
|
|
||||||
skip[plugin.dir] = true
|
|
||||||
end
|
end
|
||||||
end
|
local props = {
|
||||||
end
|
{ "total", M.stats.total, "Number" },
|
||||||
for _, path in ipairs(rtp) do
|
{ "time", ms(M.stats.time), "Bold" },
|
||||||
---@type string
|
{ "avg time", ms(M.stats.time / M.stats.total), "Bold" },
|
||||||
path = path:gsub("\\", "/")
|
{ "index", M.stats.index, "Number" },
|
||||||
if not skip[path] and not path:find("after/?$") then
|
{ "fs_stat", M.stats.stat, "Number" },
|
||||||
M.rtp[#M.rtp + 1] = path
|
{ "not found", M.stats.not_found, "Number" },
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return M.rtp
|
|
||||||
end
|
|
||||||
|
|
||||||
function M.list_rtp()
|
|
||||||
return vim.api.nvim_get_runtime_file("", true)
|
|
||||||
end
|
|
||||||
|
|
||||||
---@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
|
|
||||||
---@diagnostic disable-next-line: no-unknown
|
|
||||||
for k, v in pairs(opts.performance.cache) do
|
|
||||||
---@diagnostic disable-next-line: no-unknown
|
|
||||||
M.config[k] = v
|
|
||||||
end
|
|
||||||
end
|
|
||||||
M.debug = opts and opts.debug
|
|
||||||
M.enabled = M.config.enabled
|
|
||||||
|
|
||||||
if M.enabled then
|
|
||||||
table.insert(package.loaders, 2, M.loader)
|
|
||||||
M.load_cache()
|
|
||||||
-- selene:allow(global_usage)
|
|
||||||
_G.loadfile = M.loadfile
|
|
||||||
if #M.config.disable_events > 0 then
|
|
||||||
vim.api.nvim_create_autocmd(M.config.disable_events, { once = true, callback = M.disable })
|
|
||||||
end
|
|
||||||
else
|
|
||||||
-- we need to always add the loader since this will autoload unloaded modules
|
|
||||||
table.insert(package.loaders, M.loader)
|
|
||||||
end
|
|
||||||
|
|
||||||
return M
|
|
||||||
end
|
|
||||||
|
|
||||||
---@return CacheHash?
|
|
||||||
function M.hash(file)
|
|
||||||
local ok, ret = pcall(uv.fs_stat, file)
|
|
||||||
return ok and ret or nil
|
|
||||||
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()
|
|
||||||
vim.fn.mkdir(vim.fn.fnamemodify(M.config.path, ":p:h"), "p")
|
|
||||||
local f = assert(uv.fs_open(M.config.path, "w", 438))
|
|
||||||
uv.fs_write(f, M.VERSION)
|
|
||||||
uv.fs_write(f, "\0")
|
|
||||||
for modname, entry in pairs(M.cache) do
|
|
||||||
if entry.used > os.time() - M.config.ttl then
|
|
||||||
entry.modname = modname
|
|
||||||
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))
|
local chunks = {} ---@type string[][]
|
||||||
uv.fs_write(f, modname)
|
for _, prop in ipairs(props) do
|
||||||
uv.fs_write(f, entry.chunk)
|
chunks[#chunks + 1] = { "* " .. prop[1] .. ": " }
|
||||||
uv.fs_write(f, entry.modpath)
|
chunks[#chunks + 1] = { tostring(prop[2]) .. "\n", prop[3] }
|
||||||
end
|
end
|
||||||
end
|
vim.api.nvim_echo(chunks, true, {})
|
||||||
uv.fs_close(f)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.load_cache()
|
M._Cache = Cache
|
||||||
M.cache = {}
|
|
||||||
local f = uv.fs_open(M.config.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 zero = data:find("\0", 1, true)
|
|
||||||
if not zero then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
if M.VERSION ~= data:sub(1, zero - 1) then
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
local offset = zero + 1
|
|
||||||
while offset + 1 < #data do
|
|
||||||
local header = ffi.cast("uint32_t*", ffi.new("const char[28]", data:sub(offset, offset + 27)))
|
|
||||||
offset = offset + 28
|
|
||||||
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]
|
|
||||||
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],
|
|
||||||
}
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
function M.autosave()
|
|
||||||
vim.api.nvim_create_autocmd("VimLeavePre", {
|
|
||||||
callback = function()
|
|
||||||
if M.dirty then
|
|
||||||
local hash = M.hash(M.config.path)
|
|
||||||
-- 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,
|
|
||||||
})
|
|
||||||
end
|
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
|
@ -113,8 +113,9 @@ M.defaults = {
|
||||||
notify = true, -- get a notification when changes are found
|
notify = true, -- get a notification when changes are found
|
||||||
},
|
},
|
||||||
performance = {
|
performance = {
|
||||||
---@type LazyCacheConfig
|
cache = {
|
||||||
cache = nil,
|
enabled = true,
|
||||||
|
},
|
||||||
reset_packpath = true, -- reset the package path to improve startup time
|
reset_packpath = true, -- reset the package path to improve startup time
|
||||||
rtp = {
|
rtp = {
|
||||||
reset = true, -- reset the runtime path to $VIMRUNTIME and your config directory
|
reset = true, -- reset the runtime path to $VIMRUNTIME and your config directory
|
||||||
|
@ -226,7 +227,6 @@ function M.setup(opts)
|
||||||
pattern = "VeryLazy",
|
pattern = "VeryLazy",
|
||||||
once = true,
|
once = true,
|
||||||
callback = function()
|
callback = function()
|
||||||
require("lazy.core.cache").autosave()
|
|
||||||
require("lazy.view.commands").setup()
|
require("lazy.view.commands").setup()
|
||||||
if M.options.change_detection.enabled then
|
if M.options.change_detection.enabled then
|
||||||
require("lazy.manage.reloader").enable()
|
require("lazy.manage.reloader").enable()
|
||||||
|
|
|
@ -72,7 +72,7 @@ function M.install_missing()
|
||||||
-- remove and installed plugins from indexed, so cache will index again
|
-- remove and installed plugins from indexed, so cache will index again
|
||||||
for _, p in pairs(Config.plugins) do
|
for _, p in pairs(Config.plugins) do
|
||||||
if p._.installed then
|
if p._.installed then
|
||||||
Cache.indexed[p.dir] = nil
|
Cache.reset(p.dir)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
-- reload plugins
|
-- reload plugins
|
||||||
|
@ -341,7 +341,7 @@ function M.get_main(plugin)
|
||||||
local normname = Util.normname(plugin.name)
|
local normname = Util.normname(plugin.name)
|
||||||
---@type string[]
|
---@type string[]
|
||||||
local mods = {}
|
local mods = {}
|
||||||
for _, modname in ipairs(Cache.get_topmods(plugin.dir)) do
|
for modname, _ in pairs(Cache.lsmod(plugin.dir)) do
|
||||||
mods[#mods + 1] = modname
|
mods[#mods + 1] = modname
|
||||||
local modnorm = Util.normname(modname)
|
local modnorm = Util.normname(modname)
|
||||||
-- if we found an exact match, then use that
|
-- if we found an exact match, then use that
|
||||||
|
@ -450,4 +450,28 @@ function M.colorscheme(name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@param modname string
|
||||||
|
function M.loader(modname)
|
||||||
|
local modpath = Cache.find(modname, { rtp = false, paths = Util.get_unloaded_rtp(modname) })
|
||||||
|
if modpath then
|
||||||
|
local plugin = Plugin.find(modpath)
|
||||||
|
if plugin and modpath:find(plugin.dir, 1, true) == 1 then
|
||||||
|
-- don't load if we're loading specs or if the plugin is already loaded
|
||||||
|
if not (Plugin.loading or plugin._.loaded) then
|
||||||
|
if plugin.module == false then
|
||||||
|
error("Plugin " .. plugin.name .. " is not loaded and is configured with module=false")
|
||||||
|
end
|
||||||
|
M.load(plugin, { require = modname })
|
||||||
|
end
|
||||||
|
local mod = package.loaded[modname]
|
||||||
|
if type(mod) == "table" then
|
||||||
|
return function()
|
||||||
|
return mod
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return loadfile(modpath)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
|
@ -217,11 +217,46 @@ function M.walkmods(root, fn, modname)
|
||||||
end)
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@param modname string
|
||||||
|
function M.get_unloaded_rtp(modname)
|
||||||
|
modname = modname:gsub("/", ".")
|
||||||
|
local idx = modname:find(".", 1, true)
|
||||||
|
local topmod = idx and modname:sub(1, idx - 1) or modname
|
||||||
|
topmod = M.normname(topmod)
|
||||||
|
|
||||||
|
local rtp = {}
|
||||||
|
local Config = require("lazy.core.config")
|
||||||
|
if Config.spec then
|
||||||
|
for _, plugin in pairs(Config.spec.plugins) do
|
||||||
|
if not (plugin._.loaded or plugin.module == false) then
|
||||||
|
if topmod == M.normname(plugin.name) then
|
||||||
|
table.insert(rtp, 1, plugin.dir)
|
||||||
|
else
|
||||||
|
table.insert(rtp, plugin.dir)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
return rtp
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.find_root(modname)
|
||||||
|
local Cache = require("lazy.core.cache")
|
||||||
|
local modpath = Cache.find(modname, {
|
||||||
|
rtp = true,
|
||||||
|
paths = M.get_unloaded_rtp(modname),
|
||||||
|
patterns = { "", ".lua" },
|
||||||
|
})
|
||||||
|
if modpath then
|
||||||
|
local root = modpath:gsub("/init%.lua$", ""):gsub("%.lua$", "")
|
||||||
|
return root
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
---@param modname string
|
---@param modname string
|
||||||
---@param fn fun(modname:string, modpath:string)
|
---@param fn fun(modname:string, modpath:string)
|
||||||
function M.lsmod(modname, fn)
|
function M.lsmod(modname, fn)
|
||||||
local Cache = require("lazy.core.cache")
|
local root = M.find_root(modname)
|
||||||
local root = Cache.find_root(modname)
|
|
||||||
if not root then
|
if not root then
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
|
@ -131,12 +131,7 @@ function M.colors()
|
||||||
end
|
end
|
||||||
|
|
||||||
function M.update()
|
function M.update()
|
||||||
local cache_config = M.extract("lua/lazy/core/cache.lua", "\nM%.config = ({.-\n})")
|
|
||||||
local config = M.extract("lua/lazy/core/config.lua", "\nM%.defaults = ({.-\n})")
|
local config = M.extract("lua/lazy/core/config.lua", "\nM%.defaults = ({.-\n})")
|
||||||
config = config:gsub(
|
|
||||||
"\n%s*%-%-%-@type LazyCacheConfig.*cache = nil,",
|
|
||||||
"\n" .. M.indent("cache = " .. cache_config .. ",", 4)
|
|
||||||
)
|
|
||||||
config = config:gsub("%s*debug = false.\n", "\n")
|
config = config:gsub("%s*debug = false.\n", "\n")
|
||||||
M.save({
|
M.save({
|
||||||
bootstrap = M.extract("lua/lazy/init.lua", "function M%.bootstrap%(%)\n(.-)\nend"),
|
bootstrap = M.extract("lua/lazy/init.lua", "function M%.bootstrap%(%)\n(.-)\nend"),
|
||||||
|
|
|
@ -34,13 +34,16 @@ function M.setup(spec, opts)
|
||||||
local start = vim.loop.hrtime()
|
local start = vim.loop.hrtime()
|
||||||
|
|
||||||
-- load module cache before anything else
|
-- load module cache before anything else
|
||||||
require("lazy.core.cache").setup(opts)
|
if not (opts and opts.performance and opts.performance.cache and opts.performance.cache.enabled == false) then
|
||||||
|
require("lazy.core.cache").enable()
|
||||||
|
end
|
||||||
|
|
||||||
require("lazy.stats").track("LazyStart")
|
require("lazy.stats").track("LazyStart")
|
||||||
|
|
||||||
local Util = require("lazy.core.util")
|
local Util = require("lazy.core.util")
|
||||||
local Config = require("lazy.core.config")
|
local Config = require("lazy.core.config")
|
||||||
local Loader = require("lazy.core.loader")
|
local Loader = require("lazy.core.loader")
|
||||||
|
table.insert(package.loaders, 3, Loader.loader)
|
||||||
|
|
||||||
Util.track({ plugin = "lazy.nvim" }) -- setup start
|
Util.track({ plugin = "lazy.nvim" }) -- setup start
|
||||||
Util.track("module", vim.loop.hrtime() - start)
|
Util.track("module", vim.loop.hrtime() - start)
|
||||||
|
|
|
@ -1,4 +1,3 @@
|
||||||
local Cache = require("lazy.core.cache")
|
|
||||||
local Config = require("lazy.core.config")
|
local Config = require("lazy.core.config")
|
||||||
local Util = require("lazy.util")
|
local Util = require("lazy.util")
|
||||||
local Plugin = require("lazy.core.plugin")
|
local Plugin = require("lazy.core.plugin")
|
||||||
|
@ -6,12 +5,11 @@ local Loader = require("lazy.core.loader")
|
||||||
|
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
---@type table<string, CacheHash>
|
---@type table<string, vim.loop.Stat>
|
||||||
M.files = {}
|
M.files = {}
|
||||||
|
|
||||||
---@type vim.loop.Timer
|
---@type vim.loop.Timer
|
||||||
M.timer = nil
|
M.timer = nil
|
||||||
M.root = nil
|
|
||||||
|
|
||||||
function M.enable()
|
function M.enable()
|
||||||
if M.timer then
|
if M.timer then
|
||||||
|
@ -19,7 +17,6 @@ function M.enable()
|
||||||
end
|
end
|
||||||
if #Config.spec.modules > 0 then
|
if #Config.spec.modules > 0 then
|
||||||
M.timer = vim.loop.new_timer()
|
M.timer = vim.loop.new_timer()
|
||||||
M.root = vim.fn.stdpath("config") .. "/lua"
|
|
||||||
M.check(true)
|
M.check(true)
|
||||||
M.timer:start(2000, 2000, M.check)
|
M.timer:start(2000, 2000, M.check)
|
||||||
end
|
end
|
||||||
|
@ -32,6 +29,12 @@ function M.disable()
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
---@param h1 vim.loop.Stat
|
||||||
|
---@param h2 vim.loop.Stat
|
||||||
|
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.check(start)
|
function M.check(start)
|
||||||
---@type table<string,true>
|
---@type table<string,true>
|
||||||
local checked = {}
|
local checked = {}
|
||||||
|
@ -41,10 +44,10 @@ function M.check(start)
|
||||||
-- spec is a module
|
-- spec is a module
|
||||||
local function check(_, modpath)
|
local function check(_, modpath)
|
||||||
checked[modpath] = true
|
checked[modpath] = true
|
||||||
local hash = Cache.hash(modpath)
|
local hash = vim.loop.fs_stat(modpath)
|
||||||
if hash then
|
if hash then
|
||||||
if M.files[modpath] then
|
if M.files[modpath] then
|
||||||
if not Cache.eq(M.files[modpath], hash) then
|
if not M.eq(M.files[modpath], hash) then
|
||||||
M.files[modpath] = hash
|
M.files[modpath] = hash
|
||||||
table.insert(changes, { file = modpath, what = "changed" })
|
table.insert(changes, { file = modpath, what = "changed" })
|
||||||
end
|
end
|
||||||
|
|
|
@ -672,33 +672,14 @@ function M:debug()
|
||||||
|
|
||||||
self:append("Cache.find()", "LazyH2"):nl()
|
self:append("Cache.find()", "LazyH2"):nl()
|
||||||
self:props({
|
self:props({
|
||||||
{ "total", Cache.stats.find.total, "Number" },
|
{ "total", Cache.stats.total, "Number" },
|
||||||
{ "time", self:ms(Cache.stats.find.time, 3), "Bold" },
|
{ "time", self:ms(Cache.stats.time, 3), "Bold" },
|
||||||
{ "avg time", self:ms(Cache.stats.find.time / Cache.stats.find.total, 3), "Bold" },
|
{ "avg time", self:ms(Cache.stats.time / Cache.stats.total, 3), "Bold" },
|
||||||
{ "index", Cache.stats.find.index, "Number" },
|
{ "index", Cache.stats.index, "Number" },
|
||||||
{ "fs_stat", Cache.stats.find.stat, "Number" },
|
{ "fs_stat", Cache.stats.stat, "Number" },
|
||||||
{ "not found", Cache.stats.find.not_found, "Number" },
|
{ "not found", Cache.stats.not_found, "Number" },
|
||||||
}, { indent = 2 })
|
}, { indent = 2 })
|
||||||
self:nl()
|
self:nl()
|
||||||
|
|
||||||
self:append("Cache.autoload()", "LazyH2"):nl()
|
|
||||||
self:props({
|
|
||||||
{ "total", Cache.stats.autoload.total, "Number" },
|
|
||||||
{ "time", self:ms(Cache.stats.autoload.time, 3), "Bold" },
|
|
||||||
{ "avg time", self:ms(Cache.stats.autoload.time / Cache.stats.autoload.total, 3), "Bold" },
|
|
||||||
}, { indent = 2 })
|
|
||||||
self:nl()
|
|
||||||
|
|
||||||
self:append("Cache", "LazyH2"):nl()
|
|
||||||
local Cache = require("lazy.core.cache")
|
|
||||||
Util.foreach(Cache.cache, function(modname, entry)
|
|
||||||
local kb = math.floor(#entry.chunk / 10.24) / 100
|
|
||||||
self:append("● ", "LazySpecial", { indent = 2 }):append(modname):append(" " .. kb .. "Kb", "Bold")
|
|
||||||
if entry.modpath ~= modname then
|
|
||||||
self:append(" " .. vim.fn.fnamemodify(entry.modpath, ":p:~:."), "LazyComment")
|
|
||||||
end
|
|
||||||
self:nl()
|
|
||||||
end)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
|
@ -51,10 +51,8 @@ describe("util", function()
|
||||||
local files = Helpers.fs_create(test.files)
|
local files = Helpers.fs_create(test.files)
|
||||||
|
|
||||||
-- test with empty cache
|
-- test with empty cache
|
||||||
Cache.cache = {}
|
package.loaded["lazy.core.cache"] = nil
|
||||||
Cache.indexed = {}
|
local root = Util.find_root(test.mod)
|
||||||
Cache.indexed_rtp = false
|
|
||||||
local root = Cache.find_root(test.mod)
|
|
||||||
assert(root, "no root found for " .. test.mod .. " (test " .. t .. ")")
|
assert(root, "no root found for " .. test.mod .. " (test " .. t .. ")")
|
||||||
assert.same(Helpers.path(test.root), root)
|
assert.same(Helpers.path(test.root), root)
|
||||||
local mods = {}
|
local mods = {}
|
||||||
|
@ -65,13 +63,8 @@ describe("util", function()
|
||||||
assert.same(expected, mods)
|
assert.same(expected, mods)
|
||||||
|
|
||||||
-- fill the cache
|
-- fill the cache
|
||||||
Cache.cache = {}
|
package.loaded["lazy.core.cache"] = nil
|
||||||
for i, file in ipairs(files) do
|
root = Util.find_root(test.mod)
|
||||||
Cache.cache[test.mods[i]] = { modpath = file }
|
|
||||||
end
|
|
||||||
Cache.indexed = {}
|
|
||||||
Cache.indexed_rtp = false
|
|
||||||
root = Cache.find_root(test.mod)
|
|
||||||
assert(root, "no root found for " .. test.mod .. " (test " .. t .. ")")
|
assert(root, "no root found for " .. test.mod .. " (test " .. t .. ")")
|
||||||
assert.same(Helpers.path(test.root), root)
|
assert.same(Helpers.path(test.root), root)
|
||||||
mods = {}
|
mods = {}
|
||||||
|
@ -85,12 +78,12 @@ describe("util", function()
|
||||||
|
|
||||||
it("find the correct root with dels", function()
|
it("find the correct root with dels", function()
|
||||||
Cache.cache = {}
|
Cache.cache = {}
|
||||||
Cache.indexed = {}
|
Cache._topmods = {}
|
||||||
Cache.indexed_rtp = false
|
Cache.topmods_rtp = false
|
||||||
vim.opt.rtp:append(Helpers.path("old"))
|
vim.opt.rtp:append(Helpers.path("old"))
|
||||||
Helpers.fs_create({ "old/lua/foobar/init.lua" })
|
Helpers.fs_create({ "old/lua/foobar/init.lua" })
|
||||||
Cache.cache["foobar"] = { modpath = Helpers.path("old/lua/foobar/init.lua") }
|
Cache.cache["foobar"] = { modpath = Helpers.path("old/lua/foobar/init.lua") }
|
||||||
local root = Cache.find_root("foobar")
|
local root = Util.find_root("foobar")
|
||||||
assert(root, "foobar root not found")
|
assert(root, "foobar root not found")
|
||||||
assert.same(Helpers.path("old/lua/foobar"), root)
|
assert.same(Helpers.path("old/lua/foobar"), root)
|
||||||
|
|
||||||
|
@ -98,24 +91,22 @@ describe("util", function()
|
||||||
assert(not vim.loop.fs_stat(Helpers.path("old/lua/foobar")), "old/lua/foobar should not exist")
|
assert(not vim.loop.fs_stat(Helpers.path("old/lua/foobar")), "old/lua/foobar should not exist")
|
||||||
|
|
||||||
-- vim.opt.rtp = rtp
|
-- vim.opt.rtp = rtp
|
||||||
Cache.indexed = {}
|
Cache._topmods = {}
|
||||||
Cache.indexed_rtp = false
|
|
||||||
vim.opt.rtp:append(Helpers.path("new"))
|
vim.opt.rtp:append(Helpers.path("new"))
|
||||||
Helpers.fs_create({ "new/lua/foobar/init.lua" })
|
Helpers.fs_create({ "new/lua/foobar/init.lua" })
|
||||||
root = Cache.find_root("foobar")
|
root = Util.find_root("foobar")
|
||||||
assert(root, "foobar root not found")
|
assert(root, "foobar root not found")
|
||||||
assert.same(Helpers.path("new/lua/foobar"), root)
|
assert.same(Helpers.path("new/lua/foobar"), root)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it("find the correct root with mod dels", function()
|
it("find the correct root with mod dels", function()
|
||||||
Cache.cache = {}
|
Cache.cache = {}
|
||||||
Cache.indexed = {}
|
Cache._topmods = {}
|
||||||
Cache.indexed_rtp = false
|
|
||||||
Cache.enabled = true
|
Cache.enabled = true
|
||||||
vim.opt.rtp:append(Helpers.path("old"))
|
vim.opt.rtp:append(Helpers.path("old"))
|
||||||
Helpers.fs_create({ "old/lua/foobar/test.lua" })
|
Helpers.fs_create({ "old/lua/foobar/test.lua" })
|
||||||
Cache.cache["foobar.test"] = { modpath = Helpers.path("old/lua/foobar/test.lua") }
|
Cache.cache["foobar.test"] = { modpath = Helpers.path("old/lua/foobar/test.lua") }
|
||||||
local root = Cache.find_root("foobar")
|
local root = Util.find_root("foobar")
|
||||||
assert(root, "foobar root not found")
|
assert(root, "foobar root not found")
|
||||||
assert.same(Helpers.path("old/lua/foobar"), root)
|
assert.same(Helpers.path("old/lua/foobar"), root)
|
||||||
assert(not Cache.cache["foobar"], "foobar should not be in cache")
|
assert(not Cache.cache["foobar"], "foobar should not be in cache")
|
||||||
|
@ -124,11 +115,10 @@ describe("util", function()
|
||||||
Helpers.fs_rm("old")
|
Helpers.fs_rm("old")
|
||||||
|
|
||||||
-- vim.opt.rtp = rtp
|
-- vim.opt.rtp = rtp
|
||||||
Cache.indexed = {}
|
Cache._topmods = {}
|
||||||
Cache.indexed_rtp = false
|
|
||||||
vim.opt.rtp:append(Helpers.path("new"))
|
vim.opt.rtp:append(Helpers.path("new"))
|
||||||
Helpers.fs_create({ "new/lua/foobar/test.lua" })
|
Helpers.fs_create({ "new/lua/foobar/test.lua" })
|
||||||
root = Cache.find_root("foobar")
|
root = Util.find_root("foobar")
|
||||||
assert(root, "foobar root not found")
|
assert(root, "foobar root not found")
|
||||||
assert.same(Helpers.path("new/lua/foobar"), root)
|
assert.same(Helpers.path("new/lua/foobar"), root)
|
||||||
end)
|
end)
|
||||||
|
|
Loading…
Reference in New Issue