perf: run cache autosave after loading

This commit is contained in:
Folke Lemaitre 2022-11-29 23:16:21 +01:00
parent e6bbf92c77
commit 3ec5a2ce4c
No known key found for this signature in database
GPG Key ID: 41F8B1FBACAE2040
2 changed files with 26 additions and 25 deletions

View File

@ -44,7 +44,30 @@ function M.hash(file)
end end
function M.setup() function M.setup()
M.load() cache = {}
local f = io.open(cache_path, "rb")
if f then
cache_hash = M.hash(cache_path)
---@type string
local data = f:read("*a")
f:close()
local from = 1
local to = data:find("\0", from, true)
while to do
local key = data:sub(from, to - 1)
from = to + 1
to = data:find("\0", from, true)
local len = tonumber(data:sub(from, to - 1))
from = to + 1
cache[key] = data:sub(from, from + len - 1)
from = from + len
to = data:find("\0", from, true)
end
end
end
function M.autosave()
vim.api.nvim_create_autocmd("User", { vim.api.nvim_create_autocmd("User", {
pattern = "LazyDone", pattern = "LazyDone",
once = true, once = true,
@ -77,28 +100,4 @@ function M.save()
f:close() f:close()
end end
function M.load()
cache = {}
local f = io.open(cache_path, "rb")
if f then
cache_hash = M.hash(cache_path)
---@type string
local data = f:read("*a")
f:close()
local from = 1
local to = data:find("\0", from, true)
while to do
local key = data:sub(from, to - 1)
from = to + 1
to = data:find("\0", from, true)
local len = tonumber(data:sub(from, to - 1))
from = to + 1
cache[key] = data:sub(from, from + len - 1)
from = from + len
to = data:find("\0", from, true)
end
end
end
return M return M

View File

@ -27,6 +27,7 @@ M.defaults = {
task = "", task = "",
}, },
}, },
install_missing = true,
git = { git = {
-- defaults for `Lazy log` -- defaults for `Lazy log`
log = { "-10" }, -- last 10 commits log = { "-10" }, -- last 10 commits
@ -67,6 +68,7 @@ function M.setup(opts)
pattern = "VeryLazy", pattern = "VeryLazy",
once = true, once = true,
callback = function() callback = function()
require("lazy.core.cache").autosave()
require("lazy.view").setup() require("lazy.view").setup()
end, end,
}) })