mirror of https://github.com/folke/lazy.nvim.git
perf: move autoloader to cache and always use lazy's modname path resolver which is much faster
This commit is contained in:
parent
956164d27d
commit
34977c2b80
|
@ -30,6 +30,16 @@ M.cache = {}
|
||||||
M.enabled = true
|
M.enabled = true
|
||||||
---@type string[]
|
---@type string[]
|
||||||
M.rtp = nil
|
M.rtp = nil
|
||||||
|
M.stats = {
|
||||||
|
find = { total = 0, time = 0, rtp = 0, unloaded = 0, index = 0, stat = 0, not_found = 0 },
|
||||||
|
autoload = { total = 0, time = 0 },
|
||||||
|
}
|
||||||
|
---@type table<string, table<string,string>>
|
||||||
|
M.topmods = {}
|
||||||
|
---@type table<string, true>
|
||||||
|
M.indexed = {}
|
||||||
|
M.indexed_rtp = false
|
||||||
|
M.indexed_unloaded = false
|
||||||
-- selene:allow(global_usage)
|
-- selene:allow(global_usage)
|
||||||
M._loadfile = _G.loadfile
|
M._loadfile = _G.loadfile
|
||||||
|
|
||||||
|
@ -53,11 +63,21 @@ function M.check_path(modname, modpath)
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
return M.check_autoload(modname, modpath)
|
||||||
|
end
|
||||||
|
|
||||||
|
function M.check_autoload(modname, modpath)
|
||||||
|
local start = uv.hrtime()
|
||||||
|
M.stats.autoload.total = M.stats.autoload.total + 1
|
||||||
-- check plugins. Again fast, since we check the plugin name from the path.
|
-- check plugins. Again fast, since we check the plugin name from the path.
|
||||||
-- only needed when the plugin mod has been loaded
|
-- only needed when the plugin mod has been loaded
|
||||||
if package.loaded["lazy.core.plugin"] then
|
---@type LazyCorePlugin
|
||||||
local plugin = require("lazy.core.plugin").find(modpath)
|
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
|
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
|
||||||
if not plugin._.loaded then
|
if not plugin._.loaded then
|
||||||
if plugin.module == false then
|
if plugin.module == false then
|
||||||
error("Plugin " .. plugin.name .. " is not loaded and is configured with module=false")
|
error("Plugin " .. plugin.name .. " is not loaded and is configured with module=false")
|
||||||
|
@ -67,6 +87,7 @@ function M.check_path(modname, modpath)
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
M.stats.autoload.time = M.stats.autoload.time + uv.hrtime() - start
|
||||||
return false
|
return false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -76,13 +97,6 @@ function M.disable()
|
||||||
end
|
end
|
||||||
-- selene:allow(global_usage)
|
-- selene:allow(global_usage)
|
||||||
_G.loadfile = M._loadfile
|
_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
|
|
||||||
end
|
|
||||||
M.enabled = false
|
M.enabled = false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -93,6 +107,7 @@ function M.loader(modname)
|
||||||
|
|
||||||
local chunk, err
|
local chunk, err
|
||||||
if entry and M.check_path(modname, entry.modpath) then
|
if entry and M.check_path(modname, entry.modpath) then
|
||||||
|
M.stats.find.total = M.stats.find.total + 1
|
||||||
local mod = package.loaded[modname]
|
local mod = package.loaded[modname]
|
||||||
if type(mod) == "table" then
|
if type(mod) == "table" then
|
||||||
return function()
|
return function()
|
||||||
|
@ -105,7 +120,12 @@ function M.loader(modname)
|
||||||
-- find the modpath and load the module
|
-- find the modpath and load the module
|
||||||
local modpath = M.find(modname)
|
local modpath = M.find(modname)
|
||||||
if modpath then
|
if modpath then
|
||||||
chunk, err = M.load(modname, modpath)
|
M.check_autoload(modname, modpath)
|
||||||
|
if M.enabled then
|
||||||
|
chunk, err = M.load(modname, modpath)
|
||||||
|
else
|
||||||
|
chunk, err = M._loadfile(modpath)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
return chunk or (err and error(err)) or "not found in lazy cache"
|
return chunk or (err and error(err)) or "not found in lazy cache"
|
||||||
|
@ -163,12 +183,90 @@ function M.require(modname)
|
||||||
return mod
|
return mod
|
||||||
end
|
end
|
||||||
|
|
||||||
|
-- index the top-level lua modules for this path
|
||||||
|
function M._index(path)
|
||||||
|
if not M.indexed[path] and path:sub(-6, -1) ~= "/after" then
|
||||||
|
M.stats.find.index = M.stats.find.index + 1
|
||||||
|
---@type LazyUtilCore
|
||||||
|
local Util = package.loaded["lazy.core.util"]
|
||||||
|
if not Util then
|
||||||
|
return
|
||||||
|
end
|
||||||
|
M.indexed[path] = true
|
||||||
|
Util.ls(path .. "/lua", function(_, name, t)
|
||||||
|
local topname
|
||||||
|
if t == "directory" then
|
||||||
|
topname = name
|
||||||
|
elseif name:sub(-4) == ".lua" then
|
||||||
|
topname = name:sub(1, -5)
|
||||||
|
end
|
||||||
|
if topname then
|
||||||
|
M.topmods[topname] = M.topmods[topname] or {}
|
||||||
|
M.topmods[topname][path] = path
|
||||||
|
end
|
||||||
|
end)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
---@param modname string
|
---@param modname string
|
||||||
---@return string?
|
---@return string?
|
||||||
function M.find(modname)
|
function M.find(modname)
|
||||||
|
M.stats.find.total = M.stats.find.total + 1
|
||||||
|
local start = uv.hrtime()
|
||||||
local basename = modname:gsub("%.", "/")
|
local basename = modname:gsub("%.", "/")
|
||||||
local paths = { "lua/" .. basename .. ".lua", "lua/" .. basename .. "/init.lua" }
|
local idx = modname:find(".", 1, true)
|
||||||
return vim.api.nvim__get_runtime(paths, false, { is_lua = true })[1]
|
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" }
|
||||||
|
|
||||||
|
-- check top-level mods to find the module
|
||||||
|
local function _find()
|
||||||
|
for _, toppath in pairs(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
|
||||||
|
if not M.indexed_rtp then
|
||||||
|
for _, path in ipairs(vim.api.nvim_list_runtime_paths()) do
|
||||||
|
M._index(path)
|
||||||
|
end
|
||||||
|
M.indexed_rtp = true
|
||||||
|
modpath = _find()
|
||||||
|
end
|
||||||
|
|
||||||
|
-- update unloaded
|
||||||
|
if not modpath and not M.indexed_unloaded then
|
||||||
|
---@type LazyCoreConfig
|
||||||
|
local Config = package.loaded["lazy.core.config"]
|
||||||
|
if Config then
|
||||||
|
for _, plugin in pairs(Config.plugins) do
|
||||||
|
if not (M.indexed[plugin.dir] or plugin._.loaded or plugin.module == false) then
|
||||||
|
M._index(plugin.dir)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
M.indexed_unloaded = true
|
||||||
|
modpath = _find()
|
||||||
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
-- returns the cached RTP excluding plugin dirs
|
-- returns the cached RTP excluding plugin dirs
|
||||||
|
@ -207,23 +305,30 @@ function M.setup(opts)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
M.debug = opts and opts.debug
|
M.debug = opts and opts.debug
|
||||||
|
M.enabled = M.config.enabled
|
||||||
M.load_cache()
|
|
||||||
table.insert(package.loaders, 2, M.loader)
|
|
||||||
-- selene:allow(global_usage)
|
|
||||||
_G.loadfile = M.loadfile
|
|
||||||
|
|
||||||
-- reset rtp when it changes
|
-- reset rtp when it changes
|
||||||
vim.api.nvim_create_autocmd("OptionSet", {
|
vim.api.nvim_create_autocmd("OptionSet", {
|
||||||
pattern = "runtimepath",
|
pattern = "runtimepath",
|
||||||
callback = function()
|
callback = function()
|
||||||
M.rtp = nil
|
M.rtp = nil
|
||||||
|
M.indexed_rtp = false
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
if #M.config.disable_events > 0 then
|
if M.enabled then
|
||||||
vim.api.nvim_create_autocmd(M.config.disable_events, { once = true, callback = M.disable })
|
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
|
end
|
||||||
|
|
||||||
return M
|
return M
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
local Util = require("lazy.core.util")
|
local Util = require("lazy.core.util")
|
||||||
|
|
||||||
|
---@class LazyCoreConfig
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
---@class LazyConfig
|
---@class LazyConfig
|
||||||
|
|
|
@ -28,9 +28,6 @@ function M.setup()
|
||||||
end,
|
end,
|
||||||
})
|
})
|
||||||
|
|
||||||
-- autoload opt plugins
|
|
||||||
table.insert(package.loaders, M.autoload)
|
|
||||||
|
|
||||||
-- install missing plugins
|
-- install missing plugins
|
||||||
if Config.options.install.missing then
|
if Config.options.install.missing then
|
||||||
Util.track("install")
|
Util.track("install")
|
||||||
|
@ -298,32 +295,4 @@ function M.colorscheme(name)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
-- This loader is added as the very last one.
|
|
||||||
-- This only hits when the modname is not cached and
|
|
||||||
-- even then only once per plugin. So pretty much never.
|
|
||||||
---@param modname string
|
|
||||||
function M.autoload(modname)
|
|
||||||
-- check if a lazy plugin should be loaded
|
|
||||||
for _, plugin in pairs(Config.plugins) do
|
|
||||||
if not (plugin._.loaded or plugin.module == false) then
|
|
||||||
for _, pattern in ipairs({ ".lua", "/init.lua" }) do
|
|
||||||
local path = plugin.dir .. "/lua/" .. modname:gsub("%.", "/") .. pattern
|
|
||||||
if vim.loop.fs_stat(path) then
|
|
||||||
M.load(plugin, { require = modname })
|
|
||||||
-- check if the module has been loaded in the meantime
|
|
||||||
if type(package.loaded[modname]) == "table" then
|
|
||||||
local mod = package.loaded[modname]
|
|
||||||
return function()
|
|
||||||
return mod
|
|
||||||
end
|
|
||||||
end
|
|
||||||
local chunk, err = loadfile(path)
|
|
||||||
return chunk or error(err)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
return modname .. " not found in lazy plugins"
|
|
||||||
end
|
|
||||||
|
|
||||||
return M
|
return M
|
||||||
|
|
|
@ -3,6 +3,7 @@ local Util = require("lazy.core.util")
|
||||||
local Handler = require("lazy.core.handler")
|
local Handler = require("lazy.core.handler")
|
||||||
local Cache = require("lazy.core.cache")
|
local Cache = require("lazy.core.cache")
|
||||||
|
|
||||||
|
---@class LazyCorePlugin
|
||||||
local M = {}
|
local M = {}
|
||||||
|
|
||||||
---@class LazySpecLoader
|
---@class LazySpecLoader
|
||||||
|
|
|
@ -25,10 +25,8 @@ function M.setup(spec, opts)
|
||||||
end
|
end
|
||||||
local start = vim.loop.hrtime()
|
local start = vim.loop.hrtime()
|
||||||
|
|
||||||
if not (opts and opts.performance and opts.performance.cache and opts.performance.cache.enabled == false) then
|
-- load module cache before anything else
|
||||||
-- load module cache before anything else
|
require("lazy.core.cache").setup(opts)
|
||||||
require("lazy.core.cache").setup(opts)
|
|
||||||
end
|
|
||||||
|
|
||||||
local Util = require("lazy.core.util")
|
local Util = require("lazy.core.util")
|
||||||
local Config = require("lazy.core.config")
|
local Config = require("lazy.core.config")
|
||||||
|
|
Loading…
Reference in New Issue