Skip to content

Commit

Permalink
perf: removed partial spec caching. not worth the tiny performance boost
Browse files Browse the repository at this point in the history
  • Loading branch information
folke committed Nov 29, 2022
1 parent 9be3d3d commit 4438faf
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 228 deletions.
23 changes: 8 additions & 15 deletions lua/lazy/core/cache.lua
Original file line number Diff line number Diff line change
Expand Up @@ -68,27 +68,20 @@ function M.setup()
end

function M.autosave()
vim.api.nvim_create_autocmd("User", {
pattern = "LazyDone",
once = true,
vim.api.nvim_create_autocmd("VimLeavePre", {
callback = function()
vim.api.nvim_create_autocmd("VimLeavePre", {
callback = function()
if M.dirty then
local hash = M.hash(cache_path)
-- abort when the file was changed in the meantime
if hash == nil or cache_hash == hash then
M.save()
end
end
end,
})
if M.dirty then
local hash = M.hash(cache_path)
-- abort when the file was changed in the meantime
if hash == nil or cache_hash == hash then
M.save()
end
end
end,
})
end

function M.save()
require("lazy.core.plugin").save()
require("lazy.core.module").save()

local f = assert(io.open(cache_path, "wb"))
Expand Down
32 changes: 12 additions & 20 deletions lua/lazy/core/handler.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local Util = require("lazy.core.util")
local Loader = require("lazy.core.loader")
local Config = require("lazy.core.config")

---@class LazyPluginHandlers
---@field event? string|string[]
Expand All @@ -12,34 +13,25 @@ local M = {}

---@alias LazyHandler fun(grouped:table<string, string[]>)

---@type table<string, table<string, string[]>>
M._groups = nil

---@param plugins LazyPlugin[]
---@param rebuild? boolean
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
---@diagnostic disable-next-line: no-unknown
for _, value in pairs(type(plugin[key]) == "table" and plugin[key] or { plugin[key] }) do
M._groups[key][value] = M._groups[key][value] or {}
table.insert(M._groups[key][value], plugin.name)
end
function M.setup()
for key, handler in pairs(M.handlers) do
---@type table<string, string[]>
local group = {}
for _, plugin in pairs(Config.plugins) do
if plugin[key] then
---@diagnostic disable-next-line: no-unknown
for _, value in pairs(type(plugin[key]) == "table" and plugin[key] or { plugin[key] }) do
group[value] = group[value] or {}
table.insert(group[value], plugin.name)
end
end
end
handler(group)
end
return M._groups
end

---@type table<string, LazyHandler>
M.handlers = {}

function M.handlers.event(grouped)
local group = vim.api.nvim_create_augroup("lazy_handler_event", { clear = true })
for event, plugins in pairs(grouped) do
Expand Down
12 changes: 0 additions & 12 deletions lua/lazy/core/loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,6 @@ local M = {}
---@type LazyPlugin[]
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()
Util.track("plugin_init")
for _, plugin in pairs(Config.plugins) do
Expand Down
10 changes: 2 additions & 8 deletions lua/lazy/core/module.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,9 @@ local M = {}
---@type table<string, string>
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 modpath string
---@return any, boolean
---@return any
function M.load(modname, modpath)
local err
---@type (string|fun())?
Expand All @@ -24,9 +20,7 @@ function M.load(modname, modpath)
chunk = nil
end

local cached = false
if chunk then
cached = true
chunk, err = load(chunk --[[@as string]], "@" .. modpath, "b")
else
vim.schedule(function()
Expand All @@ -39,7 +33,7 @@ function M.load(modname, modpath)
end

if chunk then
return chunk(), cached
return chunk()
else
error(err)
end
Expand Down
Loading

0 comments on commit 4438faf

Please sign in to comment.