From 730bb84364afee156ad1dde03fc30de3d96af63a Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Fri, 30 Dec 2022 20:14:46 +0100 Subject: [PATCH] perf(loader): re-use topmod cache to find `setup()` module --- lua/lazy/core/cache.lua | 12 +++++++++--- lua/lazy/core/loader.lua | 13 ++++++------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/lua/lazy/core/cache.lua b/lua/lazy/core/cache.lua index 0589454..4bb80b9 100644 --- a/lua/lazy/core/cache.lua +++ b/lua/lazy/core/cache.lua @@ -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("\\", "/") ---@type table> M.topmods = { lazy = { [M.me] = M.me } } ----@type table -M.indexed = { [M.me] = true } +---@type table +M.indexed = { [M.me] = { "lazy" } } M.indexed_unloaded = false M.indexed_rtp = 0 -- selene:allow(global_usage) @@ -219,7 +219,7 @@ function M._index(path) if not Util then return false end - M.indexed[path] = true + M.indexed[path] = {} Util.ls(path .. "/lua", function(_, name, t) local topname if name:sub(-4) == ".lua" then @@ -230,6 +230,7 @@ function M._index(path) if topname then M.topmods[topname] = M.topmods[topname] or {} M.topmods[topname][path] = path + table.insert(M.indexed[path], topname) end end) return true @@ -237,6 +238,11 @@ function M._index(path) return false end +function M.get_topmods(path) + M._index(path) + return M.indexed[path] or {} +end + ---@param modname string ---@return string? function M.find(modname) diff --git a/lua/lazy/core/loader.lua b/lua/lazy/core/loader.lua index 70fc5a0..899df1b 100644 --- a/lua/lazy/core/loader.lua +++ b/lua/lazy/core/loader.lua @@ -1,6 +1,7 @@ local Util = require("lazy.core.util") local Config = require("lazy.core.config") local Handler = require("lazy.core.handler") +local Cache = require("lazy.core.cache") local M = {} @@ -204,19 +205,17 @@ function M.config(plugin) end else local normname = Util.normname(plugin.name) - ---@type table + ---@type string[] local mods = {} - Util.ls(plugin.dir .. "/lua", function(_, modname) - modname = modname:gsub("%.lua$", "") - mods[modname] = modname + for _, modname in ipairs(Cache.get_topmods(plugin.dir)) do + mods[#mods + 1] = modname local modnorm = Util.normname(modname) -- if we found an exact match, then use that if modnorm == normname then mods = { modname } - return false + break end - end) - mods = vim.tbl_values(mods) + end if #mods == 1 then fn = function() local opts = plugin.config