perf(loader): re-use topmod cache to find `setup()` module

This commit is contained in:
Folke Lemaitre 2022-12-30 20:14:46 +01:00
parent 887c602957
commit 730bb84364
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
2 changed files with 15 additions and 10 deletions

View File

@ -39,8 +39,8 @@ M.me = debug.getinfo(1, "S").source:sub(2)
M.me = vim.fn.fnamemodify(M.me, ":p:h:h:h:h"):gsub("\\", "/") M.me = vim.fn.fnamemodify(M.me, ":p:h:h:h:h"):gsub("\\", "/")
---@type table<string, table<string,string>> ---@type table<string, table<string,string>>
M.topmods = { lazy = { [M.me] = M.me } } M.topmods = { lazy = { [M.me] = M.me } }
---@type table<string, true> ---@type table<string, string[]>
M.indexed = { [M.me] = true } M.indexed = { [M.me] = { "lazy" } }
M.indexed_unloaded = false M.indexed_unloaded = false
M.indexed_rtp = 0 M.indexed_rtp = 0
-- selene:allow(global_usage) -- selene:allow(global_usage)
@ -219,7 +219,7 @@ function M._index(path)
if not Util then if not Util then
return false return false
end end
M.indexed[path] = true M.indexed[path] = {}
Util.ls(path .. "/lua", function(_, name, t) Util.ls(path .. "/lua", function(_, name, t)
local topname local topname
if name:sub(-4) == ".lua" then if name:sub(-4) == ".lua" then
@ -230,6 +230,7 @@ function M._index(path)
if topname then if topname then
M.topmods[topname] = M.topmods[topname] or {} M.topmods[topname] = M.topmods[topname] or {}
M.topmods[topname][path] = path M.topmods[topname][path] = path
table.insert(M.indexed[path], topname)
end end
end) end)
return true return true
@ -237,6 +238,11 @@ function M._index(path)
return false return false
end end
function M.get_topmods(path)
M._index(path)
return M.indexed[path] or {}
end
---@param modname string ---@param modname string
---@return string? ---@return string?
function M.find(modname) function M.find(modname)

View File

@ -1,6 +1,7 @@
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 Handler = require("lazy.core.handler") local Handler = require("lazy.core.handler")
local Cache = require("lazy.core.cache")
local M = {} local M = {}
@ -204,19 +205,17 @@ function M.config(plugin)
end end
else else
local normname = Util.normname(plugin.name) local normname = Util.normname(plugin.name)
---@type table<string, string> ---@type string[]
local mods = {} local mods = {}
Util.ls(plugin.dir .. "/lua", function(_, modname) for _, modname in ipairs(Cache.get_topmods(plugin.dir)) do
modname = modname:gsub("%.lua$", "") mods[#mods + 1] = modname
mods[modname] = 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
if modnorm == normname then if modnorm == normname then
mods = { modname } mods = { modname }
return false break
end
end end
end)
mods = vim.tbl_values(mods)
if #mods == 1 then if #mods == 1 then
fn = function() fn = function()
local opts = plugin.config local opts = plugin.config