Skip to content

Commit

Permalink
perf: disable cache by default on VimEnter or on BufReadPre
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Dec 3, 2022
1 parent c1e44cb commit b2727d9
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 34 deletions.
33 changes: 9 additions & 24 deletions lua/lazy/core/cache.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ M.dirty = false
M.config = {
enabled = true,
path = vim.fn.stdpath("state") .. "/lazy.state",
-- choose what should be cached
-- * lazy: cache all lazy.nvim core modules and your config files
-- * init: all of the above and any module needed to init your plugins
-- * VimEnter: any module till VimEnter
-- * VeryLazy: any module till VeryLazy
-- * allthethings: all mdules. Not recommended
strategy = "VimEnter", ---@type "lazy"|"init"|"VimEnter"|"allthethings"
-- Once one of the following events triggers, caching will be disabled.
-- To cache all modules, set this to `{}`, but that is not recommended.
-- The default is to disable on:
-- * VimEnter: not useful to cache anything else beyond startup
-- * BufReadPre: this will be triggered early when opening a file from the command line directly
disable_events = { "VimEnter", "BufReadPre" },
}
M.debug = false

Expand All @@ -40,15 +39,10 @@ function M.check_load(modname, modpath)
require("lazy.core.loader").autoload(modname, modpath)
end

---@param step? string
function M.disable(step)
function M.disable()
if not M.enabled then
return
end
if step and M.config.strategy ~= step then
return
end

local idx = M.idx()
if idx then
table.remove(package.loaders, idx)
Expand Down Expand Up @@ -141,17 +135,8 @@ function M.setup(opts)
M.load_cache()
table.insert(package.loaders, M.loader_idx, M.loader)

if M.config.strategy == "VimEnter" then
vim.api.nvim_create_autocmd("VimEnter", {
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,
})
if #M.config.disable_events > 0 then
vim.api.nvim_create_autocmd(M.config.disable_events, { once = true, callback = M.disable })
end
return M
end
Expand Down
11 changes: 1 addition & 10 deletions lua/lazy/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ function M.setup(spec, opts)
end
local start = vim.loop.hrtime()

local Cache
if not (opts and opts.performance and opts.performance.cache and opts.performance.cache.enabled == false) then
-- load module cache before anything else
Cache = require("lazy.core.cache").setup(opts)
require("lazy.core.cache").setup(opts)
end

local Util = require("lazy.core.util")
Expand Down Expand Up @@ -40,17 +39,9 @@ function M.setup(spec, opts)
Config.plugins["lazy.nvim"]._.loaded = { time = delta, source = "init.lua" }
end

if Cache then
Cache.disable("lazy")
end

-- load plugins with lazy=false or Plugin.init
Loader.init_plugins()

if Cache then
Cache.disable("init")
end

-- all done!
vim.cmd("do User LazyDone")
end
Expand Down

0 comments on commit b2727d9

Please sign in to comment.