perf: disable cache by default on VimEnter or on BufReadPre

This commit is contained in:
Folke Lemaitre 2022-12-03 18:59:28 +01:00
parent c1e44cbc3f
commit b2727d98a3
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
2 changed files with 10 additions and 34 deletions

View File

@ -9,13 +9,12 @@ M.dirty = false
M.config = { M.config = {
enabled = true, enabled = true,
path = vim.fn.stdpath("state") .. "/lazy.state", path = vim.fn.stdpath("state") .. "/lazy.state",
-- choose what should be cached -- Once one of the following events triggers, caching will be disabled.
-- * lazy: cache all lazy.nvim core modules and your config files -- To cache all modules, set this to `{}`, but that is not recommended.
-- * init: all of the above and any module needed to init your plugins -- The default is to disable on:
-- * VimEnter: any module till VimEnter -- * VimEnter: not useful to cache anything else beyond startup
-- * VeryLazy: any module till VeryLazy -- * BufReadPre: this will be triggered early when opening a file from the command line directly
-- * allthethings: all mdules. Not recommended disable_events = { "VimEnter", "BufReadPre" },
strategy = "VimEnter", ---@type "lazy"|"init"|"VimEnter"|"allthethings"
} }
M.debug = false M.debug = false
@ -40,15 +39,10 @@ function M.check_load(modname, modpath)
require("lazy.core.loader").autoload(modname, modpath) require("lazy.core.loader").autoload(modname, modpath)
end end
---@param step? string function M.disable()
function M.disable(step)
if not M.enabled then if not M.enabled then
return return
end end
if step and M.config.strategy ~= step then
return
end
local idx = M.idx() local idx = M.idx()
if idx then if idx then
table.remove(package.loaders, idx) table.remove(package.loaders, idx)
@ -141,17 +135,8 @@ function M.setup(opts)
M.load_cache() M.load_cache()
table.insert(package.loaders, M.loader_idx, M.loader) table.insert(package.loaders, M.loader_idx, M.loader)
if M.config.strategy == "VimEnter" then if #M.config.disable_events > 0 then
vim.api.nvim_create_autocmd("VimEnter", { vim.api.nvim_create_autocmd(M.config.disable_events, { once = true, callback = M.disable })
once = true,
callback = function()
-- use schedule so all other VimEnter handlers will have run
vim.schedule(function()
-- startup done, so stop caching
M.disable()
end)
end,
})
end end
return M return M
end end

View File

@ -8,10 +8,9 @@ function M.setup(spec, opts)
end end
local start = vim.loop.hrtime() local start = vim.loop.hrtime()
local Cache
if not (opts and opts.performance and opts.performance.cache and opts.performance.cache.enabled == false) then if not (opts and opts.performance and opts.performance.cache and opts.performance.cache.enabled == false) then
-- load module cache before anything else -- load module cache before anything else
Cache = require("lazy.core.cache").setup(opts) require("lazy.core.cache").setup(opts)
end end
local Util = require("lazy.core.util") local Util = require("lazy.core.util")
@ -40,17 +39,9 @@ function M.setup(spec, opts)
Config.plugins["lazy.nvim"]._.loaded = { time = delta, source = "init.lua" } Config.plugins["lazy.nvim"]._.loaded = { time = delta, source = "init.lua" }
end end
if Cache then
Cache.disable("lazy")
end
-- load plugins with lazy=false or Plugin.init -- load plugins with lazy=false or Plugin.init
Loader.init_plugins() Loader.init_plugins()
if Cache then
Cache.disable("init")
end
-- all done! -- all done!
vim.cmd("do User LazyDone") vim.cmd("do User LazyDone")
end end