Skip to content

Commit b2727d9

Browse files
committed
perf: disable cache by default on VimEnter or on BufReadPre
1 parent c1e44cb commit b2727d9

File tree

2 files changed

+10
-34
lines changed

2 files changed

+10
-34
lines changed

lua/lazy/core/cache.lua

+9-24
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@ M.dirty = false
99
M.config = {
1010
enabled = true,
1111
path = vim.fn.stdpath("state") .. "/lazy.state",
12-
-- choose what should be cached
13-
-- * lazy: cache all lazy.nvim core modules and your config files
14-
-- * init: all of the above and any module needed to init your plugins
15-
-- * VimEnter: any module till VimEnter
16-
-- * VeryLazy: any module till VeryLazy
17-
-- * allthethings: all mdules. Not recommended
18-
strategy = "VimEnter", ---@type "lazy"|"init"|"VimEnter"|"allthethings"
12+
-- Once one of the following events triggers, caching will be disabled.
13+
-- To cache all modules, set this to `{}`, but that is not recommended.
14+
-- The default is to disable on:
15+
-- * VimEnter: not useful to cache anything else beyond startup
16+
-- * BufReadPre: this will be triggered early when opening a file from the command line directly
17+
disable_events = { "VimEnter", "BufReadPre" },
1918
}
2019
M.debug = false
2120

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

43-
---@param step? string
44-
function M.disable(step)
42+
function M.disable()
4543
if not M.enabled then
4644
return
4745
end
48-
if step and M.config.strategy ~= step then
49-
return
50-
end
51-
5246
local idx = M.idx()
5347
if idx then
5448
table.remove(package.loaders, idx)
@@ -141,17 +135,8 @@ function M.setup(opts)
141135
M.load_cache()
142136
table.insert(package.loaders, M.loader_idx, M.loader)
143137

144-
if M.config.strategy == "VimEnter" then
145-
vim.api.nvim_create_autocmd("VimEnter", {
146-
once = true,
147-
callback = function()
148-
-- use schedule so all other VimEnter handlers will have run
149-
vim.schedule(function()
150-
-- startup done, so stop caching
151-
M.disable()
152-
end)
153-
end,
154-
})
138+
if #M.config.disable_events > 0 then
139+
vim.api.nvim_create_autocmd(M.config.disable_events, { once = true, callback = M.disable })
155140
end
156141
return M
157142
end

lua/lazy/init.lua

+1-10
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ function M.setup(spec, opts)
88
end
99
local start = vim.loop.hrtime()
1010

11-
local Cache
1211
if not (opts and opts.performance and opts.performance.cache and opts.performance.cache.enabled == false) then
1312
-- load module cache before anything else
14-
Cache = require("lazy.core.cache").setup(opts)
13+
require("lazy.core.cache").setup(opts)
1514
end
1615

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

43-
if Cache then
44-
Cache.disable("lazy")
45-
end
46-
4742
-- load plugins with lazy=false or Plugin.init
4843
Loader.init_plugins()
4944

50-
if Cache then
51-
Cache.disable("init")
52-
end
53-
5445
-- all done!
5546
vim.cmd("do User LazyDone")
5647
end

0 commit comments

Comments
 (0)