aboutsummaryrefslogtreecommitdiff
path: root/runtime/lua/vim/loader.lua
diff options
context:
space:
mode:
Diffstat (limited to 'runtime/lua/vim/loader.lua')
-rw-r--r--runtime/lua/vim/loader.lua197
1 files changed, 95 insertions, 102 deletions
diff --git a/runtime/lua/vim/loader.lua b/runtime/lua/vim/loader.lua
index 41d5579664..201de18497 100644
--- a/runtime/lua/vim/loader.lua
+++ b/runtime/lua/vim/loader.lua
@@ -1,8 +1,11 @@
local uv = vim.loop
+--- @type (fun(modename: string): fun()|string)[]
+local loaders = package.loaders
+
local M = {}
----@alias CacheHash {mtime: {sec:number, nsec:number}, size:number}
+---@alias CacheHash {mtime: {sec:number, nsec:number}, size:number, type: string}
---@alias CacheEntry {hash:CacheHash, chunk:string}
---@class ModuleFindOpts
@@ -29,6 +32,8 @@ local Loader = {
VERSION = 3,
---@type table<string, table<string,ModuleInfo>>
_indexed = {},
+ ---@type table<string, CacheHash>
+ _hashes = {},
---@type table<string, string[]>
_topmods = {},
_loadfile = loadfile,
@@ -38,27 +43,21 @@ local Loader = {
},
}
---- Tracks the time spent in a function
----@private
-function Loader.track(stat, start)
- Loader._stats[stat] = Loader._stats[stat] or { total = 0, time = 0 }
- Loader._stats[stat].total = Loader._stats[stat].total + 1
- Loader._stats[stat].time = Loader._stats[stat].time + uv.hrtime() - start
+--- @param path string
+--- @return uv.fs_stat.result
+--- @private
+function Loader.get_hash(path)
+ if not Loader._hashes[path] then
+ -- Note we must never save a stat for a non-existent path.
+ -- For non-existent paths fs_stat() will return nil.
+ Loader._hashes[path] = uv.fs_stat(path)
+ end
+ return Loader._hashes[path]
end
---- slightly faster/different version than vim.fs.normalize
---- we also need to have it here, since the loader will load vim.fs
---@private
-function Loader.normalize(path)
- if path:sub(1, 1) == '~' then
- local home = vim.loop.os_homedir() or '~'
- if home:sub(-1) == '\\' or home:sub(-1) == '/' then
- home = home:sub(1, -2)
- end
- path = home .. path:sub(2)
- end
- path = path:gsub('\\', '/'):gsub('/+', '/')
- return path:sub(-1) == '/' and path:sub(1, -2) or path
+local function normalize(path)
+ return vim.fs.normalize(path, { expand_env = false })
end
--- Gets the rtp excluding after directories.
@@ -67,9 +66,7 @@ end
--- @return string[] rtp, boolean updated
---@private
function Loader.get_rtp()
- local start = uv.hrtime()
if vim.in_fast_event() then
- Loader.track('get_rtp', start)
return (Loader._rtp or {}), false
end
local updated = false
@@ -77,7 +74,7 @@ function Loader.get_rtp()
if key ~= Loader._rtp_key then
Loader._rtp = {}
for _, path in ipairs(vim.api.nvim_get_runtime_file('', true)) do
- path = Loader.normalize(path)
+ path = normalize(path)
-- skip after directories
if
path:sub(-6, -1) ~= '/after'
@@ -89,7 +86,6 @@ function Loader.get_rtp()
updated = true
Loader._rtp_key = key
end
- Loader.track('get_rtp', start)
return Loader._rtp, updated
end
@@ -120,19 +116,28 @@ function Loader.write(name, entry)
uv.fs_close(f)
end
+--- @param path string
+--- @param mode integer
+--- @return string? data
+--- @private
+local function readfile(path, mode)
+ local f = uv.fs_open(path, 'r', mode)
+ if f then
+ local hash = assert(uv.fs_fstat(f))
+ local data = uv.fs_read(f, hash.size, 0) --[[@as string?]]
+ uv.fs_close(f)
+ return data
+ end
+end
+
--- Loads the cache entry for a given module or file
---@param name string module name or filename
---@return CacheEntry?
---@private
function Loader.read(name)
- local start = uv.hrtime()
local cname = Loader.cache_file(name)
- local f = uv.fs_open(cname, 'r', 438)
- if f then
- local hash = uv.fs_fstat(f) --[[@as CacheHash]]
- local data = uv.fs_read(f, hash.size, 0) --[[@as string]]
- uv.fs_close(f)
-
+ local data = readfile(cname, 438)
+ if data then
local zero = data:find('\0', 1, true)
if not zero then
return
@@ -143,7 +148,6 @@ function Loader.read(name)
if tonumber(header[1]) ~= Loader.VERSION then
return
end
- Loader.track('read', start)
return {
hash = {
size = tonumber(header[2]),
@@ -152,7 +156,6 @@ function Loader.read(name)
chunk = data:sub(zero + 1),
}
end
- Loader.track('read', start)
end
--- The `package.loaders` loader for lua files using the cache.
@@ -160,14 +163,13 @@ end
---@return string|function
---@private
function Loader.loader(modname)
- local start = uv.hrtime()
local ret = M.find(modname)[1]
if ret then
- local chunk, err = Loader.load(ret.modpath, { hash = ret.stat })
- Loader.track('loader', start)
+ -- Make sure to call the global loadfile so we respect any augmentations done elsewhere.
+ -- E.g. profiling
+ local chunk, err = loadfile(ret.modpath)
return chunk or error(err)
end
- Loader.track('loader', start)
return '\ncache_loader: module ' .. modname .. ' not found'
end
@@ -176,7 +178,6 @@ end
---@return string|function
---@private
function Loader.loader_lib(modname)
- local start = uv.hrtime()
local sysname = uv.os_uname().sysname:lower() or ''
local is_win = sysname:find('win', 1, true) and not sysname:find('darwin', 1, true)
local ret = M.find(modname, { patterns = is_win and { '.dll' } or { '.so' } })[1]
@@ -190,28 +191,23 @@ function Loader.loader_lib(modname)
local dash = modname:find('-', 1, true)
local funcname = dash and modname:sub(dash + 1) or modname
local chunk, err = package.loadlib(ret.modpath, 'luaopen_' .. funcname:gsub('%.', '_'))
- Loader.track('loader_lib', start)
return chunk or error(err)
end
- Loader.track('loader_lib', start)
return '\ncache_loader_lib: module ' .. modname .. ' not found'
end
--- `loadfile` using the cache
+--- Note this has the mode and env arguments which is supported by LuaJIT and is 5.1 compatible.
---@param filename? string
---@param mode? "b"|"t"|"bt"
---@param env? table
----@param hash? CacheHash
---@return function?, string? error_message
---@private
-- luacheck: ignore 312
-function Loader.loadfile(filename, mode, env, hash)
- local start = uv.hrtime()
- filename = Loader.normalize(filename)
- mode = nil -- ignore mode, since we byte-compile the lua source files
- local chunk, err = Loader.load(filename, { mode = mode, env = env, hash = hash })
- Loader.track('loadfile', start)
- return chunk, err
+function Loader.loadfile(filename, mode, env)
+ -- ignore mode, since we byte-compile the lua source files
+ mode = nil
+ return Loader.load(normalize(filename), { mode = mode, env = env })
end
--- Checks whether two cache hashes are the same based on:
@@ -231,26 +227,21 @@ end
--- Loads the given module path using the cache
---@param modpath string
----@param opts? {hash?: CacheHash, mode?: "b"|"t"|"bt", env?:table} (table|nil) Options for loading the module:
---- - hash: (table) the hash of the file to load if it is already known. (defaults to `vim.loop.fs_stat({modpath})`)
+---@param opts? {mode?: "b"|"t"|"bt", env?:table} (table|nil) Options for loading the module:
--- - mode: (string) the mode to load the module with. "b"|"t"|"bt" (defaults to `nil`)
--- - env: (table) the environment to load the module in. (defaults to `nil`)
---@see |luaL_loadfile()|
---@return function?, string? error_message
---@private
function Loader.load(modpath, opts)
- local start = uv.hrtime()
-
opts = opts or {}
- local hash = opts.hash or uv.fs_stat(modpath)
+ local hash = Loader.get_hash(modpath)
---@type function?, string?
local chunk, err
if not hash then
-- trigger correct error
- chunk, err = Loader._loadfile(modpath, opts.mode, opts.env)
- Loader.track('load', start)
- return chunk, err
+ return Loader._loadfile(modpath, opts.mode, opts.env)
end
local entry = Loader.read(modpath)
@@ -258,7 +249,6 @@ function Loader.load(modpath, opts)
-- found in cache and up to date
chunk, err = load(entry.chunk --[[@as string]], '@' .. modpath, opts.mode, opts.env)
if not (err and err:find('cannot load incompatible bytecode', 1, true)) then
- Loader.track('load', start)
return chunk, err
end
end
@@ -269,7 +259,6 @@ function Loader.load(modpath, opts)
entry.chunk = string.dump(chunk)
Loader.write(modpath, entry)
end
- Loader.track('load', start)
return chunk, err
end
@@ -287,7 +276,6 @@ end
--- - modname: (string) the name of the module
--- - stat: (table|nil) the fs_stat of the module path. Won't be returned for `modname="*"`
function M.find(modname, opts)
- local start = uv.hrtime()
opts = opts or {}
modname = modname:gsub('/', '.')
@@ -338,7 +326,7 @@ function M.find(modname, opts)
for _, pattern in ipairs(patterns) do
local modpath = path .. pattern
Loader._stats.find.stat = (Loader._stats.find.stat or 0) + 1
- local hash = uv.fs_stat(modpath)
+ local hash = Loader.get_hash(modpath)
if hash then
results[#results + 1] = { modpath = modpath, stat = hash, modname = modname }
if not continue() then
@@ -366,7 +354,6 @@ function M.find(modname, opts)
_find(opts.paths)
end
- Loader.track('find', start)
if #results == 0 then
-- module not found
Loader._stats.find.not_found = Loader._stats.find.not_found + 1
@@ -375,15 +362,18 @@ function M.find(modname, opts)
return results
end
---- Resets the topmods cache for the path, or all the paths
+--- Resets the cache for the path, or all the paths
--- if path is nil.
---@param path string? path to reset
function M.reset(path)
if path then
- Loader._indexed[Loader.normalize(path)] = nil
+ Loader._indexed[normalize(path)] = nil
else
Loader._indexed = {}
end
+
+ -- Path could be a directory so just clear all the hashes.
+ Loader._hashes = {}
end
--- Enables the experimental Lua module loader:
@@ -399,29 +389,16 @@ function M.enable()
vim.fn.mkdir(vim.fn.fnamemodify(M.path, ':p'), 'p')
_G.loadfile = Loader.loadfile
-- add lua loader
- table.insert(package.loaders, 2, Loader.loader)
+ table.insert(loaders, 2, Loader.loader)
-- add libs loader
- table.insert(package.loaders, 3, Loader.loader_lib)
+ table.insert(loaders, 3, Loader.loader_lib)
-- remove Neovim loader
- for l, loader in ipairs(package.loaders) do
+ for l, loader in ipairs(loaders) do
if loader == vim._load_package then
- table.remove(package.loaders, l)
+ table.remove(loaders, l)
break
end
end
-
- -- this will reset the top-mods in case someone adds a new
- -- top-level lua module to a path already on the rtp
- vim.api.nvim_create_autocmd('BufWritePost', {
- group = vim.api.nvim_create_augroup('cache_topmods_reset', { clear = true }),
- callback = function(event)
- local bufname = event.match ---@type string
- local idx = bufname:find('/lua/', 1, true)
- if idx then
- M.reset(bufname:sub(1, idx - 1))
- end
- end,
- })
end
--- Disables the experimental Lua module loader:
@@ -433,14 +410,12 @@ function M.disable()
end
M.enabled = false
_G.loadfile = Loader._loadfile
- ---@diagnostic disable-next-line: no-unknown
- for l, loader in ipairs(package.loaders) do
+ for l, loader in ipairs(loaders) do
if loader == Loader.loader or loader == Loader.loader_lib then
- table.remove(package.loaders, l)
+ table.remove(loaders, l)
end
end
- table.insert(package.loaders, 2, vim._load_package)
- vim.api.nvim_del_augroup_by_name('cache_topmods_reset')
+ table.insert(loaders, 2, vim._load_package)
end
--- Return the top-level `/lua/*` modules for this path
@@ -448,17 +423,11 @@ end
---@private
function Loader.lsmod(path)
if not Loader._indexed[path] then
- local start = uv.hrtime()
Loader._indexed[path] = {}
- local handle = vim.loop.fs_scandir(path .. '/lua')
- while handle do
- local name, t = vim.loop.fs_scandir_next(handle)
- if not name then
- break
- end
+ for name, t in vim.fs.dir(path .. '/lua') do
local modpath = path .. '/lua/' .. name
-- HACK: type is not always returned due to a bug in luv
- t = t or uv.fs_stat(modpath).type
+ t = t or Loader.get_hash(modpath).type
---@type string
local topname
local ext = name:sub(-4)
@@ -477,22 +446,46 @@ function Loader.lsmod(path)
end
end
end
- Loader.track('lsmod', start)
end
return Loader._indexed[path]
end
+--- Tracks the time spent in a function
+--- @generic F: function
+--- @param f F
+--- @return F
+--- @private
+function Loader.track(stat, f)
+ return function(...)
+ local start = vim.loop.hrtime()
+ local r = { f(...) }
+ Loader._stats[stat] = Loader._stats[stat] or { total = 0, time = 0 }
+ Loader._stats[stat].total = Loader._stats[stat].total + 1
+ Loader._stats[stat].time = Loader._stats[stat].time + uv.hrtime() - start
+ return unpack(r, 1, table.maxn(r))
+ end
+end
+
+---@class ProfileOpts
+---@field loaders? boolean Add profiling to the loaders
+
--- Debug function that wrapps all loaders and tracks stats
---@private
-function M._profile_loaders()
- for l, loader in pairs(package.loaders) do
- local loc = debug.getinfo(loader, 'Sn').source:sub(2)
- package.loaders[l] = function(modname)
- local start = vim.loop.hrtime()
- local ret = loader(modname)
- Loader.track('loader ' .. l .. ': ' .. loc, start)
- Loader.track('loader_all', start)
- return ret
+---@param opts ProfileOpts?
+function M._profile(opts)
+ Loader.get_rtp = Loader.track('get_rtp', Loader.get_rtp)
+ Loader.read = Loader.track('read', Loader.read)
+ Loader.loader = Loader.track('loader', Loader.loader)
+ Loader.loader_lib = Loader.track('loader_lib', Loader.loader_lib)
+ Loader.loadfile = Loader.track('loadfile', Loader.loadfile)
+ Loader.load = Loader.track('load', Loader.load)
+ M.find = Loader.track('find', M.find)
+ Loader.lsmod = Loader.track('lsmod', Loader.lsmod)
+
+ if opts and opts.loaders then
+ for l, loader in pairs(loaders) do
+ local loc = debug.getinfo(loader, 'Sn').source:sub(2)
+ loaders[l] = Loader.track('loader ' .. l .. ': ' .. loc, loader)
end
end
end