Skip to content

Commit 690f9e8

Browse files
committed
refactor: prepping for vim.loader
1 parent 887eb75 commit 690f9e8

File tree

2 files changed

+62
-57
lines changed

2 files changed

+62
-57
lines changed

lua/lazy/core/cache.lua

+61-56
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,34 @@ local M = {}
2626
---@field modname string Name of the module
2727
---@field stat? uv_fs_t File stat of the module path
2828

29-
M.VERSION = 3
29+
---@alias LoaderStats table<string, {total:number, time:number, [string]:number?}?>
30+
3031
M.path = vim.fn.stdpath("cache") .. "/luac"
3132
M.enabled = false
32-
---@type table<string, {total:number, time:number, [string]:number?}?>
33-
M.stats = {
34-
find = { total = 0, time = 0, not_found = 0 },
35-
}
3633

3734
---@class Loader
3835
---@field _rtp string[]
3936
---@field _rtp_pure string[]
4037
---@field _rtp_key string
4138
local Loader = {
39+
VERSION = 3,
4240
---@type table<string, table<string,ModuleInfo>>
4341
_indexed = {},
4442
---@type table<string, string[]>
4543
_topmods = {},
4644
_loadfile = loadfile,
45+
---@type LoaderStats
46+
_stats = {
47+
find = { total = 0, time = 0, not_found = 0 },
48+
},
4749
}
4850

4951
--- Tracks the time spent in a function
5052
---@private
51-
function M._track(stat, start)
52-
M.stats[stat] = M.stats[stat] or { total = 0, time = 0 }
53-
M.stats[stat].total = M.stats[stat].total + 1
54-
M.stats[stat].time = M.stats[stat].time + uv.hrtime() - start
53+
function Loader.track(stat, start)
54+
Loader._stats[stat] = Loader._stats[stat] or { total = 0, time = 0 }
55+
Loader._stats[stat].total = Loader._stats[stat].total + 1
56+
Loader._stats[stat].time = Loader._stats[stat].time + uv.hrtime() - start
5557
end
5658

5759
--- slightly faster/different version than vim.fs.normalize
@@ -77,7 +79,7 @@ end
7779
function Loader.get_rtp()
7880
local start = uv.hrtime()
7981
if vim.in_fast_event() then
80-
M._track("get_rtp", start)
82+
Loader.track("get_rtp", start)
8183
return (Loader._rtp or {}), false
8284
end
8385
local updated = false
@@ -94,7 +96,7 @@ function Loader.get_rtp()
9496
updated = true
9597
Loader._rtp_key = key
9698
end
97-
M._track("get_rtp", start)
99+
Loader.track("get_rtp", start)
98100
return Loader._rtp, updated
99101
end
100102

@@ -115,7 +117,7 @@ function Loader.write(name, entry)
115117
local cname = Loader.cache_file(name)
116118
local f = assert(uv.fs_open(cname, "w", 438))
117119
local header = {
118-
M.VERSION,
120+
Loader.VERSION,
119121
entry.hash.size,
120122
entry.hash.mtime.sec,
121123
entry.hash.mtime.nsec,
@@ -142,16 +144,16 @@ function Loader.read(name)
142144

143145
---@type integer[]|{[0]:integer}
144146
local header = vim.split(data:sub(1, zero - 1), ",")
145-
if tonumber(header[1]) ~= M.VERSION then
147+
if tonumber(header[1]) ~= Loader.VERSION then
146148
return
147149
end
148-
M._track("read", start)
150+
Loader.track("read", start)
149151
return {
150152
hash = { size = tonumber(header[2]), mtime = { sec = tonumber(header[3]), nsec = tonumber(header[4]) } },
151153
chunk = data:sub(zero + 1),
152154
}
153155
end
154-
M._track("read", start)
156+
Loader.track("read", start)
155157
end
156158

157159
--- The `package.loaders` loader for lua files using the cache.
@@ -163,10 +165,10 @@ function Loader.loader(modname)
163165
local ret = M.find(modname)[1]
164166
if ret then
165167
local chunk, err = Loader.load(ret.modpath, { hash = ret.stat })
166-
M._track("loader", start)
168+
Loader.track("loader", start)
167169
return chunk or error(err)
168170
end
169-
M._track("loader", start)
171+
Loader.track("loader", start)
170172
return "\ncache_loader: module " .. modname .. " not found"
171173
end
172174

@@ -189,10 +191,10 @@ function Loader.loader_lib(modname)
189191
local dash = modname:find("-", 1, true)
190192
local funcname = dash and modname:sub(dash + 1) or modname
191193
local chunk, err = package.loadlib(ret.modpath, "luaopen_" .. funcname:gsub("%.", "_"))
192-
M._track("loader_lib", start)
194+
Loader.track("loader_lib", start)
193195
return chunk or error(err)
194196
end
195-
M._track("loader_lib", start)
197+
Loader.track("loader_lib", start)
196198
return "\ncache_loader_lib: module " .. modname .. " not found"
197199
end
198200

@@ -209,7 +211,7 @@ function Loader.loadfile(filename, mode, env, hash)
209211
filename = Loader.normalize(filename)
210212
mode = nil -- ignore mode, since we byte-compile the lua source files
211213
local chunk, err = Loader.load(filename, { mode = mode, env = env, hash = hash })
212-
M._track("loadfile", start)
214+
Loader.track("loadfile", start)
213215
return chunk, err
214216
end
215217

@@ -244,7 +246,7 @@ function Loader.load(modpath, opts)
244246
if not hash then
245247
-- trigger correct error
246248
chunk, err = Loader._loadfile(modpath, opts.mode, opts.env)
247-
M._track("load", start)
249+
Loader.track("load", start)
248250
return chunk, err
249251
end
250252

@@ -254,7 +256,7 @@ function Loader.load(modpath, opts)
254256
-- selene: allow(incorrect_standard_library_use)
255257
chunk, err = load(entry.chunk --[[@as string]], "@" .. modpath, opts.mode, opts.env)
256258
if not (err and err:find("cannot load incompatible bytecode", 1, true)) then
257-
M._track("load", start)
259+
Loader.track("load", start)
258260
return chunk, err
259261
end
260262
end
@@ -265,7 +267,7 @@ function Loader.load(modpath, opts)
265267
entry.chunk = string.dump(chunk)
266268
Loader.write(modpath, entry)
267269
end
268-
M._track("load", start)
270+
Loader.track("load", start)
269271
return chunk, err
270272
end
271273

@@ -332,7 +334,7 @@ function M.find(modname, opts)
332334
elseif Loader.lsmod(path)[topmod] then
333335
for _, pattern in ipairs(patterns) do
334336
local modpath = path .. pattern
335-
M.stats.find.stat = (M.stats.find.stat or 0) + 1
337+
Loader._stats.find.stat = (Loader._stats.find.stat or 0) + 1
336338
local hash = uv.fs_stat(modpath)
337339
if hash then
338340
results[#results + 1] = { modpath = modpath, stat = hash, modname = modname }
@@ -361,10 +363,10 @@ function M.find(modname, opts)
361363
_find(opts.paths)
362364
end
363365

364-
M._track("find", start)
366+
Loader.track("find", start)
365367
if #results == 0 then
366368
-- module not found
367-
M.stats.find.not_found = M.stats.find.not_found + 1
369+
Loader._stats.find.not_found = Loader._stats.find.not_found + 1
368370
end
369371

370372
return results
@@ -474,57 +476,60 @@ function Loader.lsmod(path)
474476
end
475477
end
476478
end
477-
M._track("lsmod", start)
479+
Loader.track("lsmod", start)
478480
end
479481
return Loader._indexed[path]
480482
end
481483

482484
--- Debug function that wrapps all loaders and tracks stats
483485
---@private
484-
function M.profile_loaders()
486+
function M._profile_loaders()
485487
for l, loader in pairs(package.loaders) do
486488
local loc = debug.getinfo(loader, "Sn").source:sub(2)
487489
package.loaders[l] = function(modname)
488490
local start = vim.loop.hrtime()
489491
local ret = loader(modname)
490-
M._track("loader " .. l .. ": " .. loc, start)
491-
M._track("loader_all", start)
492+
Loader.track("loader " .. l .. ": " .. loc, start)
493+
Loader.track("loader_all", start)
492494
return ret
493495
end
494496
end
495497
end
496498

497499
--- Prints all cache stats
500+
---@param opts? {print?:boolean}
501+
---@return LoaderStats
498502
---@private
499-
function M.inspect()
500-
---@private
501-
local function ms(nsec)
502-
return math.floor(nsec / 1e6 * 1000 + 0.5) / 1000 .. "ms"
503-
end
504-
local chunks = {} ---@type string[][]
505-
---@type string[]
506-
local stats = vim.tbl_keys(M.stats)
507-
table.sort(stats)
508-
for _, stat in ipairs(stats) do
509-
vim.list_extend(chunks, {
510-
{ "\n" .. stat .. "\n", "Title" },
511-
{ "* total: " },
512-
{ tostring(M.stats[stat].total) .. "\n", "Number" },
513-
{ "* time: " },
514-
{ ms(M.stats[stat].time) .. "\n", "Bold" },
515-
{ "* avg time: " },
516-
{ ms(M.stats[stat].time / M.stats[stat].total) .. "\n", "Bold" },
517-
})
518-
for k, v in pairs(M.stats[stat]) do
519-
if not vim.tbl_contains({ "time", "total" }, k) then
520-
chunks[#chunks + 1] = { "* " .. k .. ":" .. string.rep(" ", 9 - #k) }
521-
chunks[#chunks + 1] = { tostring(v) .. "\n", "Number" }
503+
function M._inspect(opts)
504+
if opts and opts.print then
505+
---@private
506+
local function ms(nsec)
507+
return math.floor(nsec / 1e6 * 1000 + 0.5) / 1000 .. "ms"
508+
end
509+
local chunks = {} ---@type string[][]
510+
---@type string[]
511+
local stats = vim.tbl_keys(Loader._stats)
512+
table.sort(stats)
513+
for _, stat in ipairs(stats) do
514+
vim.list_extend(chunks, {
515+
{ "\n" .. stat .. "\n", "Title" },
516+
{ "* total: " },
517+
{ tostring(Loader._stats[stat].total) .. "\n", "Number" },
518+
{ "* time: " },
519+
{ ms(Loader._stats[stat].time) .. "\n", "Bold" },
520+
{ "* avg time: " },
521+
{ ms(Loader._stats[stat].time / Loader._stats[stat].total) .. "\n", "Bold" },
522+
})
523+
for k, v in pairs(Loader._stats[stat]) do
524+
if not vim.tbl_contains({ "time", "total" }, k) then
525+
chunks[#chunks + 1] = { "* " .. k .. ":" .. string.rep(" ", 9 - #k) }
526+
chunks[#chunks + 1] = { tostring(v) .. "\n", "Number" }
527+
end
522528
end
523529
end
530+
vim.api.nvim_echo(chunks, true, {})
524531
end
525-
vim.api.nvim_echo(chunks, true, {})
532+
return Loader._stats
526533
end
527534

528-
M._Cache = Loader
529-
530535
return M

lua/lazy/view/render.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,7 @@ function M:debug()
675675
end)
676676
self:nl()
677677

678-
Util.foreach(require("lazy.core.cache").stats, function(name, stats)
678+
Util.foreach(require("lazy.core.cache")._inspect(), function(name, stats)
679679
self:append(name, "LazyH2"):nl()
680680
local props = {
681681
{ "total", stats.total or 0, "Number" },

0 commit comments

Comments
 (0)