perf: tons of performance improvements. Lazy should now load in about 1.5ms for 97 plugins

This commit is contained in:
Folke Lemaitre 2022-11-24 22:04:23 +01:00
parent 711834f17c
commit 2507fd5790
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
11 changed files with 220 additions and 278 deletions

View file

@ -3,9 +3,43 @@ local Module = require("lazy.core.module")
local M = {}
M.functions = { "init", "config", "run" }
M.dirty = true
function M.update_state(check_clean)
local Util = require("lazy.core.util")
local Config = require("lazy.core.config")
---@type table<"opt"|"start", table<string,boolean>>
local installed = { opt = {}, start = {} }
for opt, packs in pairs(installed) do
Util.scandir(Config.options.package_path .. "/" .. opt, function(_, name, type)
if type == "directory" or type == "link" then
packs[name] = true
end
end)
end
for _, plugin in pairs(Config.plugins) do
local opt = plugin.opt and "opt" or "start"
plugin.installed = installed[opt][plugin.pack] == true
installed[opt][plugin.pack] = nil
end
if check_clean then
Config.to_clean = {}
for opt, packs in pairs(installed) do
for pack in pairs(packs) do
table.insert(Config.to_clean, {
name = pack,
pack = pack,
dir = Config.options.package_path .. "/" .. opt .. "/" .. pack,
opt = opt == "opt",
installed = true,
})
end
end
end
end
function M.save()
if not M.dirty then
return
@ -14,29 +48,30 @@ function M.save()
---@class LazyState
local state = {
---@type LazyPlugin[]
---@type CachedPlugin[]
plugins = {},
loaders = require("lazy.core.loader").loaders,
config = Config.options,
}
---@alias CachedPlugin LazyPlugin | {_funcs: table<string, number|boolean>}
local skip = { installed = true, loaded = true, tasks = true, dirty = true, dir = true }
local funcount = 0
for _, plugin in pairs(Config.plugins) do
---@type LazyPlugin | {_chunks: string[] | table<string, number>}
---@type CachedPlugin
local save = {}
table.insert(state.plugins, save)
---@diagnostic disable-next-line: no-unknown
for k, v in pairs(plugin) do
if type(v) == "function" then
if vim.tbl_contains(M.functions, k) then
if plugin.modname then
save[k] = true
else
funcount = funcount + 1
Cache.set("cache.state.fun." .. funcount, string.dump(v))
save[k] = funcount
end
save._funcs = save._funcs or {}
if plugin.modname then
save._funcs[k] = true
else
funcount = funcount + 1
Cache.set("cache.state.fun." .. funcount, string.dump(v))
save._funcs[k] = funcount
end
elseif not skip[k] then
save[k] = v
@ -46,16 +81,6 @@ function M.save()
Cache.set("cache.state", vim.json.encode(state))
end
local function load_plugin(plugin, fun, ...)
local mod = Module.load(plugin.modname)
for k, v in pairs(mod) do
if type(v) == "function" then
plugin[k] = v
end
end
return mod[fun](...)
end
function M.load()
---@type boolean, LazyState
local ok, state = pcall(vim.json.decode, Cache.get("cache.state"))
@ -64,7 +89,6 @@ function M.load()
return false
end
local Util = require("lazy.core.util")
local Config = require("lazy.core.config")
if not vim.deep_equal(Config.options, state.config) then
@ -72,50 +96,45 @@ function M.load()
return false
end
-- Check for installed plugins
---@type table<"opt"|"start", table<string,boolean>>
local installed = { opt = {}, start = {} }
for opt, packs in pairs(installed) do
for _, entry in ipairs(Util.scandir(Config.options.package_path .. "/" .. opt)) do
if entry.type == "directory" or entry.type == "link" then
packs[entry.name] = true
end
end
if Module.is_dirty(Config.options.plugins, Config.paths.main) then
return false
end
-- plugins
for _, plugin in ipairs(state.plugins) do
---@cast plugin LazyPlugin|{_chunks:table}
Config.plugins[plugin.name] = plugin
plugin.loaded = nil
plugin.dir = Config.options.package_path .. "/" .. (plugin.opt and "opt" or "start") .. "/" .. plugin.pack
plugin.installed = installed[plugin.opt and "opt" or "start"][plugin.pack]
if plugin.modname then
-- mark module as used
assert(Cache.get(plugin.modname))
for _, fun in ipairs(M.functions) do
if plugin[fun] == true then
plugin[fun] = function(...)
return load_plugin(plugin, fun, ...)
if Module.is_dirty(plugin.modname, plugin.modpath) then
return false
end
for fun in pairs(plugin._funcs or {}) do
---@diagnostic disable-next-line: assign-type-mismatch
plugin[fun] = function(...)
local mod = Module.load(plugin.modname, plugin.modpath)
for k in pairs(plugin._funcs) do
plugin[k] = mod[k]
end
return plugin[fun](...)
end
end
else
for _, fun in ipairs(M.functions) do
if type(plugin[fun]) == "number" then
local chunk = assert(Cache.get("cache.state.fun." .. plugin[fun]))
plugin[fun] = function(...)
plugin[fun] = loadstring(chunk)
return plugin[fun](...)
end
elseif plugin._funcs then
for fun, id in pairs(plugin._funcs) do
local chunk = assert(Cache.get("cache.state.fun." .. id))
---@diagnostic disable-next-line: assign-type-mismatch
plugin[fun] = function(...)
---@diagnostic disable-next-line: assign-type-mismatch
plugin[fun] = loadstring(chunk)
return plugin[fun](...)
end
end
end
end
M.update_state()
-- loaders
local Loader = require("lazy.core.loader")
Loader.loaders = state.loaders
require("lazy.core.loader").loaders = state.loaders
M.dirty = false