Skip to content

Commit

Permalink
perf: cache handler groups
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Nov 26, 2022
1 parent 05a0da5 commit 42c2fb4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 40 deletions.
61 changes: 32 additions & 29 deletions lua/lazy/core/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,39 @@ local Loader = require("lazy.core.loader")

local M = {}

---@alias LazyHandler fun(plugins:LazyPlugin[])
---@alias LazyHandler fun(grouped:table<string, string[]>)

---@type table<string, table<string, string[]>>
M._groups = nil

---@param plugins LazyPlugin[]
---@param key string
---@return table<any, LazyPlugin[]>
function M.group(plugins, key)
---@type table<any, LazyPlugin[]>
local ret = {}
for _, plugin in pairs(plugins) do
---@diagnostic disable-next-line: no-unknown
for _, value in pairs(type(plugin[key]) == "table" and plugin[key] or { plugin[key] }) do
ret[value] = ret[value] or {}
table.insert(ret[value], plugin)
---@param rebuild? boolean
function M.group(plugins, rebuild)
if M._groups == nil or rebuild then
M._groups = {}
local types = vim.tbl_keys(M.handlers) --[[@as string[] ]]
for _, key in ipairs(types) do
M._groups[key] = {}
for _, plugin in pairs(plugins) do
if plugin[key] then
---@diagnostic disable-next-line: no-unknown
for _, value in pairs(type(plugin[key]) == "table" and plugin[key] or { plugin[key] }) do
M._groups[key][value] = M._groups[key][value] or {}
table.insert(M._groups[key][value], plugin.name)
end
end
end
end
end
return ret
return M._groups
end
---@type table<string, LazyHandler>
M.handlers = {}
---@param plugins LazyPlugin[]
function M.handlers.event(plugins)
function M.handlers.event(grouped)
local group = vim.api.nvim_create_augroup("lazy_handler_event", { clear = true })
---@diagnostic disable-next-line: redefined-local
for event, plugins in pairs(M.group(plugins, "event")) do
for event, plugins in pairs(grouped) do
---@cast event string
if event == "VimEnter" and vim.v.vim_did_enter == 1 then
Loader.load(plugins, { event = event })
Expand All @@ -48,9 +55,8 @@ function M.handlers.event(plugins)
end
end
function M.handlers.keys(plugins)
---@diagnostic disable-next-line: redefined-local
for keys, plugins in pairs(M.group(plugins, "keys")) do
function M.handlers.keys(grouped)
for keys, plugins in pairs(grouped) do
---@cast keys string
vim.keymap.set("n", keys, function()
vim.keymap.del("n", keys)
Expand All @@ -62,10 +68,9 @@ function M.handlers.keys(plugins)
end
end
function M.handlers.ft(plugins)
function M.handlers.ft(grouped)
local group = vim.api.nvim_create_augroup("lazy_handler_ft", { clear = true })
---@diagnostic disable-next-line: redefined-local
for ft, plugins in pairs(M.group(plugins, "ft")) do
for ft, plugins in pairs(grouped) do
---@cast ft string
vim.api.nvim_create_autocmd("FileType", {
once = true,
Expand All @@ -80,9 +85,8 @@ function M.handlers.ft(plugins)
end
end
function M.handlers.cmd(plugins)
---@diagnostic disable-next-line: redefined-local
for cmd, plugins in pairs(M.group(plugins, "cmd")) do
function M.handlers.cmd(grouped)
for cmd, plugins in pairs(grouped) do
---@cast cmd string
local function _load(complete)
vim.api.nvim_del_user_command(cmd)
Expand Down Expand Up @@ -117,17 +121,16 @@ function M.handlers.cmd(plugins)
end
end
function M.handlers.module(plugins)
local modules = M.group(plugins, "module")
function M.handlers.module(grouped)
---@param modname string
table.insert(package.loaders, 2, function(modname)
local idx = modname:find(".", 1, true) or #modname + 1
while idx do
local name = modname:sub(1, idx - 1)
---@diagnostic disable-next-line: redefined-local
local plugins = modules[name]
local plugins = grouped[name]
if plugins then
modules[name] = nil
grouped[name] = nil
local reason = { require = modname }
if #Loader.loading == 0 then
local f = 3
Expand Down
16 changes: 5 additions & 11 deletions lua/lazy/core/loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,13 @@ M.loading = {}

function M.setup()
local Handler = require("lazy.core.handler")
local groups = Handler.group(Config.plugins)
for t, handler in pairs(Handler.handlers) do
Util.track(t)
---@type LazyPlugin[]
local plugins = {}
for _, plugin in pairs(Config.plugins) do
if plugin[t] ~= nil then
table.insert(plugins, plugin)
end
end
if #plugins > 0 then
handler(plugins)
if groups[t] then
Util.track(t)
handler(groups[t])
Util.track()
end
Util.track()
end
end

Expand Down
3 changes: 3 additions & 0 deletions lua/lazy/core/plugin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ function M.load()

if M.dirty then
Cache.dirty = true
elseif state then
require("lazy.core.handler")._groups = state.handlers
end
end

Expand All @@ -215,6 +217,7 @@ function M.save()
local state = {
---@type table<string, LazySpec>
specs = {},
handlers = require("lazy.core.handler").group(Config.plugins, true),
config = Config.options,
}

Expand Down

0 comments on commit 42c2fb4

Please sign in to comment.