perf: removed partial spec caching. not worth the tiny performance boost

This commit is contained in:
Folke Lemaitre 2022-11-30 00:18:59 +01:00
parent 9be3d3d840
commit 4438faf9a9
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
8 changed files with 77 additions and 228 deletions

View File

@ -68,10 +68,6 @@ function M.setup()
end end
function M.autosave() function M.autosave()
vim.api.nvim_create_autocmd("User", {
pattern = "LazyDone",
once = true,
callback = function()
vim.api.nvim_create_autocmd("VimLeavePre", { vim.api.nvim_create_autocmd("VimLeavePre", {
callback = function() callback = function()
if M.dirty then if M.dirty then
@ -83,12 +79,9 @@ function M.autosave()
end end
end, end,
}) })
end,
})
end end
function M.save() function M.save()
require("lazy.core.plugin").save()
require("lazy.core.module").save() require("lazy.core.module").save()
local f = assert(io.open(cache_path, "wb")) local f = assert(io.open(cache_path, "wb"))

View File

@ -1,5 +1,6 @@
local Util = require("lazy.core.util") local Util = require("lazy.core.util")
local Loader = require("lazy.core.loader") local Loader = require("lazy.core.loader")
local Config = require("lazy.core.config")
---@class LazyPluginHandlers ---@class LazyPluginHandlers
---@field event? string|string[] ---@field event? string|string[]
@ -12,34 +13,25 @@ local M = {}
---@alias LazyHandler fun(grouped:table<string, string[]>) ---@alias LazyHandler fun(grouped:table<string, string[]>)
---@type table<string, table<string, string[]>> function M.setup()
M._groups = nil for key, handler in pairs(M.handlers) do
---@type table<string, string[]>
---@param plugins LazyPlugin[] local group = {}
---@param rebuild? boolean for _, plugin in pairs(Config.plugins) do
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 if plugin[key] then
---@diagnostic disable-next-line: no-unknown ---@diagnostic disable-next-line: no-unknown
for _, value in pairs(type(plugin[key]) == "table" and plugin[key] or { plugin[key] }) do for _, value in pairs(type(plugin[key]) == "table" and plugin[key] or { plugin[key] }) do
M._groups[key][value] = M._groups[key][value] or {} group[value] = group[value] or {}
table.insert(M._groups[key][value], plugin.name) table.insert(group[value], plugin.name)
end end
end end
end end
handler(group)
end end
end
return M._groups
end end
---@type table<string, LazyHandler> ---@type table<string, LazyHandler>
M.handlers = {} M.handlers = {}
function M.handlers.event(grouped) function M.handlers.event(grouped)
local group = vim.api.nvim_create_augroup("lazy_handler_event", { clear = true }) local group = vim.api.nvim_create_augroup("lazy_handler_event", { clear = true })
for event, plugins in pairs(grouped) do for event, plugins in pairs(grouped) do

View File

@ -6,18 +6,6 @@ local M = {}
---@type LazyPlugin[] ---@type LazyPlugin[]
M.loading = {} 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
if groups[t] then
Util.track(t)
handler(groups[t])
Util.track()
end
end
end
function M.init_plugins() function M.init_plugins()
Util.track("plugin_init") Util.track("plugin_init")
for _, plugin in pairs(Config.plugins) do for _, plugin in pairs(Config.plugins) do

View File

@ -5,13 +5,9 @@ local M = {}
---@type table<string, string> ---@type table<string, string>
M.hashes = {} M.hashes = {}
function M.is_dirty(modname, modpath)
return not (Cache.get(modname) and M.hashes[modname] and M.hashes[modname] == Cache.hash(modpath))
end
---@param modname string ---@param modname string
---@param modpath string ---@param modpath string
---@return any, boolean ---@return any
function M.load(modname, modpath) function M.load(modname, modpath)
local err local err
---@type (string|fun())? ---@type (string|fun())?
@ -24,9 +20,7 @@ function M.load(modname, modpath)
chunk = nil chunk = nil
end end
local cached = false
if chunk then if chunk then
cached = true
chunk, err = load(chunk --[[@as string]], "@" .. modpath, "b") chunk, err = load(chunk --[[@as string]], "@" .. modpath, "b")
else else
vim.schedule(function() vim.schedule(function()
@ -39,7 +33,7 @@ function M.load(modname, modpath)
end end
if chunk then if chunk then
return chunk(), cached return chunk()
else else
error(err) error(err)
end end

View File

@ -1,17 +1,10 @@
local Config = require("lazy.core.config") local Config = require("lazy.core.config")
local Util = require("lazy.core.util") local Util = require("lazy.core.util")
local Module = require("lazy.core.module") local Module = require("lazy.core.module")
local Cache = require("lazy.core.cache")
local Handler = require("lazy.core.handler") local Handler = require("lazy.core.handler")
local M = {} local M = {}
---@alias CachedPlugin LazyPlugin | {_funs: string[]}
local skip = { _ = true, dir = true }
local funs = { config = true, init = true, run = true }
M.dirty = false
---@class LazyPluginHooks ---@class LazyPluginHooks
---@field init? fun(LazyPlugin) Will always be run ---@field init? fun(LazyPlugin) Will always be run
---@field config? fun(LazyPlugin) Will be executed when loading the plugin ---@field config? fun(LazyPlugin) Will be executed when loading the plugin
@ -48,10 +41,7 @@ M.dirty = false
---@alias LazySpec string|LazyPlugin|LazySpec[]|{dependencies:LazySpec} ---@alias LazySpec string|LazyPlugin|LazySpec[]|{dependencies:LazySpec}
---@class LazySpecLoader ---@class LazySpecLoader
---@field modname string
---@field modpath string
---@field plugins table<string, LazyPlugin> ---@field plugins table<string, LazyPlugin>
---@field funs? table<string, string[]>
local Spec = {} local Spec = {}
M.Spec = Spec M.Spec = Spec
@ -59,30 +49,12 @@ M.Spec = Spec
function Spec.new(spec) function Spec.new(spec)
local self = setmetatable({}, { __index = Spec }) local self = setmetatable({}, { __index = Spec })
self.plugins = {} self.plugins = {}
self.modname = nil
self.modpath = nil
if spec then if spec then
self:normalize(spec) self:normalize(spec)
end end
return self return self
end end
---@param modname string
---@param modpath string
function Spec.load(modname, modpath)
local self = setmetatable({}, { __index = Spec })
self.plugins = {}
self.modname = modname
self.modpath = modpath
local mod, cached = Module.load(modname, modpath)
M.dirty = M.dirty or not cached
self:normalize(assert(mod))
if modname == Config.options.plugins and not self.plugins["lazy.nvim"] then
self:add({ "folke/lazy.nvim", opt = false })
end
return self
end
---@param plugin LazyPlugin ---@param plugin LazyPlugin
---@param is_dep? boolean ---@param is_dep? boolean
function Spec:add(plugin, is_dep) function Spec:add(plugin, is_dep)
@ -113,9 +85,16 @@ function Spec:add(plugin, is_dep)
plugin.dep = is_dep plugin.dep = is_dep
M.process_local(plugin) -- check for plugins that should be local
for _, pattern in ipairs(Config.options.plugins_local.patterns) do
if plugin[1]:find(pattern, 1, true) then
plugin.uri = Config.options.plugins_local.path .. "/" .. plugin.name
break
end
end
local other = self.plugins[plugin.name] local other = self.plugins[plugin.name]
self.plugins[plugin.name] = other and M.merge(other, plugin) or plugin self.plugins[plugin.name] = other and self:merge(other, plugin) or plugin
return self.plugins[plugin.name] return self.plugins[plugin.name]
end end
@ -140,28 +119,10 @@ function Spec:normalize(spec, results, is_dep)
return results return results
end end
---@param spec LazySpecLoader
function Spec.revive(spec)
if spec.funs then
---@type LazySpecLoader
local loaded = nil
for fun, plugins in pairs(spec.funs) do
for _, name in pairs(plugins) do
---@diagnostic disable-next-line: no-unknown
spec.plugins[name][fun] = function(...)
loaded = loaded or Spec.load(spec.modname, spec.modpath)
return loaded.plugins[name][fun](...)
end
end
end
end
return spec
end
---@param old LazyPlugin ---@param old LazyPlugin
---@param new LazyPlugin ---@param new LazyPlugin
---@return LazyPlugin ---@return LazyPlugin
function M.merge(old, new) function Spec:merge(old, new)
local is_dep = old.dep and new.dep local is_dep = old.dep and new.dep
---@diagnostic disable-next-line: no-unknown ---@diagnostic disable-next-line: no-unknown
@ -185,13 +146,9 @@ function M.merge(old, new)
return old return old
end end
---@param opts? {clean:boolean, installed:boolean, plugins?: LazyPlugin[]} function M.update_state()
function M.update_state(opts)
opts = opts or {}
---@type table<"opt"|"start", table<string,FileType>> ---@type table<"opt"|"start", table<string,FileType>>
local installed = { opt = {}, start = {} } local installed = { opt = {}, start = {} }
if opts.installed ~= false then
for opt, packs in pairs(installed) do for opt, packs in pairs(installed) do
Util.ls(Config.options.packpath .. "/" .. opt, function(_, name, type) Util.ls(Config.options.packpath .. "/" .. opt, function(_, name, type)
if type == "directory" or type == "link" then if type == "directory" or type == "link" then
@ -199,11 +156,9 @@ function M.update_state(opts)
end end
end) end)
end end
end
for _, plugin in pairs(opts.plugins or Config.plugins) do for _, plugin in pairs(Config.plugins) do
plugin._ = plugin._ or {} plugin._ = plugin._ or {}
plugin[1] = plugin["1"] or plugin[1]
if plugin.opt == nil then if plugin.opt == nil then
plugin.opt = plugin.dep plugin.opt = plugin.dep
or Config.options.opt or Config.options.opt
@ -224,121 +179,52 @@ function M.update_state(opts)
end end
end end
if opts.clean then
Config.to_clean = {} Config.to_clean = {}
for opt, packs in pairs(installed) do for opt, packs in pairs(installed) do
for pack in pairs(packs) do for pack, dir_type in pairs(packs) do
table.insert(Config.to_clean, { table.insert(Config.to_clean, {
name = pack, name = pack,
pack = pack,
dir = Config.options.packpath .. "/" .. opt .. "/" .. pack, dir = Config.options.packpath .. "/" .. opt .. "/" .. pack,
opt = opt == "opt", opt = opt == "opt",
_ = { _ = {
installed = true, installed = true,
is_symlink = dir_type == "link",
is_local = dir_type == "link",
}, },
}) })
end end
end end
end
end end
---@param plugin LazyPlugin function M.spec()
function M.process_local(plugin) local spec = Spec.new()
for _, pattern in ipairs(Config.options.plugins_local.patterns) do
if plugin[1]:find(pattern, 1, true) then
plugin.uri = Config.options.plugins_local.path .. "/" .. plugin.name
return
end
end
end
---@param cache? table<string,LazySpecLoader>
function M.specs(cache)
---@type LazySpecLoader[]
local specs = {}
local function _load(name, modpath) local function _load(name, modpath)
local modname = Config.options.plugins .. (name and ("." .. name) or "") local modname = Config.options.plugins .. (name and ("." .. name) or "")
Util.try(function() Util.try(function()
local spec = cache and cache[modname] local mod = Module.load(modname, modpath)
spec = spec and not Module.is_dirty(modname, modpath) and Spec.revive(spec) or Spec.load(modname, modpath) spec:normalize(assert(mod))
table.insert(specs, spec)
end, "Failed to load **" .. modname .. "**") end, "Failed to load **" .. modname .. "**")
end end
_load(nil, Config.paths.main) _load(nil, Config.paths.main)
Util.lsmod(Config.paths.plugins, _load) Util.lsmod(Config.paths.plugins, _load)
return specs return spec
end end
function M.load() function M.load()
---@type boolean, LazyState?
local ok, state = pcall(vim.json.decode, Cache.get("cache.state"))
if not (ok and state and vim.deep_equal(Config.options, state.config)) then
M.dirty = true
state = nil
end
-- load specs -- load specs
Util.track("specs") Util.track("spec")
local specs = M.specs(state and state.specs) local spec = M.spec()
if not spec.plugins["lazy.nvim"] then
spec:add({ "folke/lazy.nvim", opt = false })
end
Config.plugins = spec.plugins
Util.track() Util.track()
-- merge
Config.plugins = {}
for _, spec in ipairs(specs) do
for _, plugin in pairs(spec.plugins) do
local other = Config.plugins[plugin.name]
Config.plugins[plugin.name] = other and M.merge(other, plugin) or plugin
end
end
Util.track("state") Util.track("state")
M.update_state() M.update_state()
Util.track() Util.track()
if M.dirty then
Cache.dirty = true
elseif state then
require("lazy.core.handler")._groups = state.handlers
end
end
function M.save()
---@class LazyState
local state = {
---@type table<string, LazySpecLoader>
specs = {},
handlers = require("lazy.core.handler").group(Config.plugins, true),
config = Config.options,
}
for _, spec in ipairs(M.specs()) do
spec.funs = {}
state.specs[spec.modname] = spec
for _, plugin in pairs(spec.plugins) do
if plugin.init or (plugin.opt == false and plugin.config) then
-- no use in caching specs that need init,
-- or specs that are in start and have a config,
-- since we'll load the real spec during startup anyway
state.specs[spec.modname] = nil
break
end
---@cast plugin CachedPlugin
for k, v in pairs(plugin) do
if type(v) == "function" then
if funs[k] then
spec.funs[k] = spec.funs[k] or {}
table.insert(spec.funs[k], plugin.name)
end
plugin[k] = nil
elseif skip[k] then
plugin[k] = nil
end
end
end
end
Cache.set("cache.state", vim.json.encode(state))
end end
return M return M

View File

@ -10,6 +10,7 @@ function M.setup(opts)
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 Loader = require("lazy.core.loader") local Loader = require("lazy.core.loader")
local Handler = require("lazy.core.handler")
local Plugin = require("lazy.core.plugin") local Plugin = require("lazy.core.plugin")
Util.track("cache", module_start - cache_start) Util.track("cache", module_start - cache_start)
@ -21,9 +22,7 @@ function M.setup(opts)
Config.setup(opts) Config.setup(opts)
Util.track() Util.track()
Util.track("state")
Plugin.load() Plugin.load()
Util.track()
if Config.options.install_missing then if Config.options.install_missing then
Util.track("install") Util.track("install")
@ -40,12 +39,13 @@ function M.setup(opts)
Util.track() Util.track()
end end
Util.track("loader") Util.track("handlers")
Loader.setup() Handler.setup()
Util.track() Util.track()
local lazy_delta = vim.loop.hrtime() - cache_start local lazy_delta = vim.loop.hrtime() - cache_start
Util.track() -- end setup
Loader.init_plugins() Loader.init_plugins()
if Config.plugins["lazy.nvim"] then if Config.plugins["lazy.nvim"] then
@ -53,23 +53,16 @@ function M.setup(opts)
end end
vim.cmd("do User LazyDone") vim.cmd("do User LazyDone")
Util.track() -- end setup
end end
function M.stats() function M.stats()
local ret = { local ret = { count = 0, loaded = 0 }
count = 0,
loaded = 0,
}
for _, plugin in pairs(require("lazy.core.config").plugins) do for _, plugin in pairs(require("lazy.core.config").plugins) do
ret.count = ret.count + 1 ret.count = ret.count + 1
if plugin._.loaded then if plugin._.loaded then
ret.loaded = ret.loaded + 1 ret.loaded = ret.loaded + 1
end end
end end
return ret return ret
end end

View File

@ -115,7 +115,7 @@ end
---@param opts? ManagerOpts ---@param opts? ManagerOpts
function M.clean(opts) function M.clean(opts)
Plugin.update_state({ clean = true }) Plugin.update_state()
M.run({ M.run({
pipeline = { "fs.clean" }, pipeline = { "fs.clean" },
plugins = Config.to_clean, plugins = Config.to_clean,
@ -123,7 +123,7 @@ function M.clean(opts)
end end
function M.clear() function M.clear()
Plugin.update_state({ clean = true }) Plugin.update_state()
for _, plugin in pairs(Config.plugins) do for _, plugin in pairs(Config.plugins) do
plugin._.updated = nil plugin._.updated = nil
plugin._.cloned = nil plugin._.cloned = nil

View File

@ -38,7 +38,8 @@ describe("plugin spec opt", function()
} }
for _, test in ipairs(tests) do for _, test in ipairs(tests) do
local spec = Plugin.Spec.new(test) local spec = Plugin.Spec.new(test)
Plugin.update_state({ plugins = spec.plugins }) Config.plugins = spec.plugins
Plugin.update_state()
assert(vim.tbl_count(spec.plugins) == 3) assert(vim.tbl_count(spec.plugins) == 3)
assert(#spec.plugins.bar.dependencies == 2) assert(#spec.plugins.bar.dependencies == 2)
assert(spec.plugins.bar.dep ~= true) assert(spec.plugins.bar.dep ~= true)
@ -53,7 +54,8 @@ describe("plugin spec opt", function()
it("handles opt from dep", function() it("handles opt from dep", function()
Config.options.opt = false Config.options.opt = false
local spec = Plugin.Spec.new({ "foo/dep1", { "foo/bar", dependencies = { "foo/dep1", "foo/dep2" } } }) local spec = Plugin.Spec.new({ "foo/dep1", { "foo/bar", dependencies = { "foo/dep1", "foo/dep2" } } })
Plugin.update_state({ plugins = spec.plugins }) Config.plugins = spec.plugins
Plugin.update_state()
assert.same(3, vim.tbl_count(spec.plugins)) assert.same(3, vim.tbl_count(spec.plugins))
assert(spec.plugins.bar.dep ~= true) assert(spec.plugins.bar.dep ~= true)
assert(spec.plugins.bar.opt == false) assert(spec.plugins.bar.opt == false)
@ -66,7 +68,8 @@ describe("plugin spec opt", function()
it("handles opt from dep", function() it("handles opt from dep", function()
Config.options.opt = false Config.options.opt = false
local spec = Plugin.Spec.new({ "foo/bar", module = "foo" }) local spec = Plugin.Spec.new({ "foo/bar", module = "foo" })
Plugin.update_state({ plugins = spec.plugins }) Config.plugins = spec.plugins
Plugin.update_state()
assert.same(1, vim.tbl_count(spec.plugins)) assert.same(1, vim.tbl_count(spec.plugins))
assert(spec.plugins.bar.dep ~= true) assert(spec.plugins.bar.dep ~= true)
assert(spec.plugins.bar.opt == true) assert(spec.plugins.bar.opt == true)