From 329a6d82cbd28104fa80c6139a04b8b1151d869a Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Sat, 30 Nov 2024 17:58:05 +0000 Subject: [PATCH] feat(vim.loader): defer writing cache files --- runtime/lua/vim/loader.lua | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/runtime/lua/vim/loader.lua b/runtime/lua/vim/loader.lua index 71d0188128..415d27f2ae 100644 --- a/runtime/lua/vim/loader.lua +++ b/runtime/lua/vim/loader.lua @@ -120,21 +120,33 @@ local function cache_filename(name) return ret:sub(-4) == '.lua' and (ret .. 'c') or (ret .. '.luac') end +local writing = {} --- @type table + --- Saves the cache entry for a given module or file --- @param cname string cache filename --- @param hash vim.loader.CacheHash --- @param chunk function local function write_cachefile(cname, hash, chunk) - local f = assert(uv.fs_open(cname, 'w', 438)) - local header = { - VERSION, - hash.size, - hash.mtime.sec, - hash.mtime.nsec, - } - uv.fs_write(f, table.concat(header, ',') .. '\0') - uv.fs_write(f, string.dump(chunk)) - uv.fs_close(f) + if writing[cname] then + return + end + + writing[cname] = chunk + uv.fs_open(cname, 'w', 438, function(err, f) + if err then + error(err) + end + local header = { + VERSION, + hash.size, + hash.mtime.sec, + hash.mtime.nsec, + } + uv.fs_write(f, table.concat(header, ',') .. '\0') + uv.fs_write(f, string.dump(chunk)) + uv.fs_close(f) + writing[cname] = nil + end) end --- @param path string @@ -255,6 +267,10 @@ local function loadfile_cached(filename, mode, env) end end + if writing[cname] then + return writing[cname] + end + local chunk, err = _loadfile(modpath, mode, env) if chunk and stat then write_cachefile(cname, stat, chunk)