perf: new file-based cache that ensures correct rtp order (#532)

* perf: new file-based cache that ensures rtp is alweays correct and will cache all files, including those after startup

* refactor: new cache

* test: fix tests

* fix(cache): cache file names on Windows

* feat(cache): allow to disable the cache

* docs: updated cache settings
This commit is contained in:
Folke Lemaitre 2023-02-13 12:01:56 +01:00 committed by GitHub
parent 06f835d0b4
commit 462633bae1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 354 additions and 549 deletions

View file

@ -1,4 +1,3 @@
local Cache = require("lazy.core.cache")
local Config = require("lazy.core.config")
local Util = require("lazy.util")
local Plugin = require("lazy.core.plugin")
@ -6,12 +5,11 @@ local Loader = require("lazy.core.loader")
local M = {}
---@type table<string, CacheHash>
---@type table<string, vim.loop.Stat>
M.files = {}
---@type vim.loop.Timer
M.timer = nil
M.root = nil
function M.enable()
if M.timer then
@ -19,7 +17,6 @@ function M.enable()
end
if #Config.spec.modules > 0 then
M.timer = vim.loop.new_timer()
M.root = vim.fn.stdpath("config") .. "/lua"
M.check(true)
M.timer:start(2000, 2000, M.check)
end
@ -32,6 +29,12 @@ function M.disable()
end
end
---@param h1 vim.loop.Stat
---@param h2 vim.loop.Stat
function M.eq(h1, h2)
return h1 and h2 and h1.size == h2.size and h1.mtime.sec == h2.mtime.sec and h1.mtime.nsec == h2.mtime.nsec
end
function M.check(start)
---@type table<string,true>
local checked = {}
@ -41,10 +44,10 @@ function M.check(start)
-- spec is a module
local function check(_, modpath)
checked[modpath] = true
local hash = Cache.hash(modpath)
local hash = vim.loop.fs_stat(modpath)
if hash then
if M.files[modpath] then
if not Cache.eq(M.files[modpath], hash) then
if not M.eq(M.files[modpath], hash) then
M.files[modpath] = hash
table.insert(changes, { file = modpath, what = "changed" })
end