perf(loader): reduce calls to Loader.cache_file

This commit is contained in:
Lewis Russell
2024-10-31 14:16:46 +00:00
parent 03118c46ab
commit f8fc6cb157

View File

@ -125,11 +125,10 @@ function Loader.cache_file(name)
end end
--- Saves the cache entry for a given module or file --- Saves the cache entry for a given module or file
---@param name string module name or filename ---@param cname string cache filename
---@param entry CacheEntry ---@param entry CacheEntry
---@private ---@private
function Loader.write(name, entry) function Loader.write(cname, entry)
local cname = Loader.cache_file(name)
local f = assert(uv.fs_open(cname, 'w', 438)) local f = assert(uv.fs_open(cname, 'w', 438))
local header = { local header = {
Loader.VERSION, Loader.VERSION,
@ -156,11 +155,10 @@ local function readfile(path, mode)
end end
--- Loads the cache entry for a given module or file --- Loads the cache entry for a given module or file
---@param name string module name or filename ---@param cname string cache filename
---@return CacheEntry? ---@return CacheEntry?
---@private ---@private
function Loader.read(name) function Loader.read(cname)
local cname = Loader.cache_file(name)
local data = readfile(cname, 438) local data = readfile(cname, 438)
if data then if data then
local zero = data:find('\0', 1, true) local zero = data:find('\0', 1, true)
@ -268,7 +266,9 @@ function Loader.load(modpath, opts)
return Loader._loadfile(modpath, opts.mode, opts.env) return Loader._loadfile(modpath, opts.mode, opts.env)
end end
local entry = Loader.read(modpath) local cname = Loader.cache_file(modpath)
local entry = Loader.read(cname)
if entry and Loader.eq(entry.hash, hash) then if entry and Loader.eq(entry.hash, hash) then
-- found in cache and up to date -- found in cache and up to date
chunk, err = load(entry.chunk --[[@as string]], '@' .. modpath, opts.mode, opts.env) chunk, err = load(entry.chunk --[[@as string]], '@' .. modpath, opts.mode, opts.env)
@ -281,7 +281,7 @@ function Loader.load(modpath, opts)
chunk, err = Loader._loadfile(modpath, opts.mode, opts.env) chunk, err = Loader._loadfile(modpath, opts.mode, opts.env)
if chunk then if chunk then
entry.chunk = string.dump(chunk) entry.chunk = string.dump(chunk)
Loader.write(modpath, entry) Loader.write(cname, entry)
end end
return chunk, err return chunk, err
end end