perf: move autoloader to cache and always use lazy's modname path resolver which is much faster

This commit is contained in:
Folke Lemaitre 2022-12-28 17:57:59 +01:00
parent 956164d27d
commit 34977c2b80
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
5 changed files with 128 additions and 54 deletions

View file

@ -28,9 +28,6 @@ function M.setup()
end,
})
-- autoload opt plugins
table.insert(package.loaders, M.autoload)
-- install missing plugins
if Config.options.install.missing then
Util.track("install")
@ -298,32 +295,4 @@ function M.colorscheme(name)
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