diff options
| author | kylo252 <[email protected]> | 2022-04-20 13:22:26 +0200 | 
|---|---|---|
| committer | GitHub <[email protected]> | 2022-04-20 13:22:26 +0200 | 
| commit | 0481ec8dddf4bd8ed81c10ae98807fec97f6f872 (patch) | |
| tree | 10a85cd7266f288a9b49e9658a377413854622ba /lua/lvim/impatient.lua | |
| parent | 3d841425aed288091068bbb8d15376c9908c6cec (diff) | |
refactor: update impatient (#2477)
Diffstat (limited to 'lua/lvim/impatient.lua')
| -rw-r--r-- | lua/lvim/impatient.lua | 518 | 
1 files changed, 259 insertions, 259 deletions
| diff --git a/lua/lvim/impatient.lua b/lua/lvim/impatient.lua index 230e5195..fe26b940 100644 --- a/lua/lvim/impatient.lua +++ b/lua/lvim/impatient.lua @@ -1,112 +1,46 @@  -- modified version from https://github.com/lewis6991/impatient.nvim  local vim = vim -local uv = vim.loop -local impatient_load_start = uv.hrtime()  local api = vim.api -local ffi = require "ffi" - -local get_option, set_option = api.nvim_get_option, api.nvim_set_option -local get_runtime_file = api.nvim_get_runtime_file +local uv = vim.loop +local _loadfile = loadfile +local get_runtime = api.nvim__get_runtime +local fs_stat = uv.fs_stat +local mpack = vim.mpack -local impatient_dur +local appdir = os.getenv "APPDIR"  local M = { -  cache = {}, -  profile = nil, -  dirty = false, -  path = nil, +  chunks = { +    cache = {}, +    profile = nil, +    dirty = false, +    path = vim.fn.stdpath "cache" .. "/luacache_chunks", +  }, +  modpaths = { +    cache = {}, +    profile = nil, +    dirty = false, +    path = vim.fn.stdpath "cache" .. "/luacache_modpaths", +  },    log = {},  }  _G.__luacache = M ---{{{ -local cachepack = {} - --- using double for packing/unpacking numbers has no conversion overhead --- 32-bit ARM causes a bus error when casting to double, so use int there -local number_t = jit.arch ~= "arm" and "double" or "int" -ffi.cdef("typedef " .. number_t .. " number_t;") - -local c_number_t = ffi.typeof "number_t[1]" -local c_sizeof_number_t = ffi.sizeof "number_t" - -local out_buf = {} - -function out_buf.write_number(buf, num) -  buf[#buf + 1] = ffi.string(c_number_t(num), c_sizeof_number_t) -end - -function out_buf.write_string(buf, str) -  out_buf.write_number(buf, #str) -  buf[#buf + 1] = str -end - -function out_buf.to_string(buf) -  return table.concat(buf) -end - -local in_buf = {} - -function in_buf.read_number(buf) -  if buf.size < buf.pos then -    error "buffer access violation" -  end -  local res = ffi.cast("number_t*", buf.ptr + buf.pos)[0] -  buf.pos = buf.pos + c_sizeof_number_t -  return res -end - -function in_buf.read_string(buf) -  local len = in_buf.read_number(buf) -  local res = ffi.string(buf.ptr + buf.pos, len) -  buf.pos = buf.pos + len - -  return res -end - -function cachepack.pack(cache) -  local total_keys = vim.tbl_count(cache) -  local buf = {} - -  out_buf.write_number(buf, total_keys) -  for k, v in pairs(cache) do -    out_buf.write_string(buf, k) -    out_buf.write_string(buf, v[1] or "") -    out_buf.write_number(buf, v[2] or 0) -    out_buf.write_string(buf, v[3] or "") -  end - -  return out_buf.to_string(buf) -end - -function cachepack.unpack(str, raw_buf_size) -  if raw_buf_size == 0 or str == nil or (raw_buf_size == nil and #str == 0) then -    return {} -  end - -  local buf = { -    ptr = raw_buf_size and str or ffi.new("const char[?]", #str, str), -    pos = 0, -    size = raw_buf_size or #str, -  } -  local cache = {} - -  local total_keys = in_buf.read_number(buf) -  for _ = 1, total_keys do -    local k = in_buf.read_string(buf) -    local v = { -      in_buf.read_string(buf), -      in_buf.read_number(buf), -      in_buf.read_string(buf), -    } -    cache[k] = v +if not get_runtime then +  -- nvim 0.5 compat +  get_runtime = function(paths, all, _) +    local r = {} +    for _, path in ipairs(paths) do +      local found = api.nvim_get_runtime_file(path, all) +      for i = 1, #found do +        r[#r + 1] = found[i] +      end +    end +    return r    end - -  return cache  end ---}}}  local function log(...)    M.log[#M.log + 1] = table.concat({ string.format(...) }, " ") @@ -119,246 +53,312 @@ function M.print_log()  end  function M.enable_profile() -  M.profile = {} +  local P = require "lvim.impatient.profile" + +  M.chunks.profile = {} +  M.modpaths.profile = {} + +  P.setup(M.modpaths.profile) +    M.print_profile = function() -    M.profile["lvim.impatient"] = { -      resolve = 0, -      load = impatient_dur, -      loader = "standard", -    } -    require("lvim.impatient.profile").print_profile(M.profile) +    P.print_profile(M)    end -  vim.cmd [[command! LuaCacheProfile lua _G.__luacache.print_profile()]] -end -local function is_cacheable(path) -  -- Don't cache files in /tmp since they are not likely to persist. -  -- Note: Appimage versions of Neovim mount $VIMRUNTIME in /tmp in a unique -  -- directory on each launch. -  return not vim.startswith(path, "/tmp/") +  vim.cmd [[command! LuaCacheProfile lua _G.__luacache.print_profile()]]  end  local function hash(modpath) -  local stat = uv.fs_stat(modpath) +  local stat = fs_stat(modpath)    if stat then -    return stat.mtime.sec +    return stat.mtime.sec .. stat.mtime.nsec .. stat.size    end +  error("Could not hash " .. modpath)  end -local function hrtime() -  if M.profile then -    return uv.hrtime() +local function modpath_mangle(modpath) +  if appdir then +    modpath = modpath:gsub(appdir, "/$APPDIR")    end +  return modpath  end -local function load_package_with_cache(name, loader) -  local resolve_start = hrtime() +local function modpath_unmangle(modpath) +  if appdir then +    modpath = modpath:gsub("/$APPDIR", appdir) +  end +  return modpath +end -  local basename = name:gsub("%.", "/") -  local paths = { "lua/" .. basename .. ".lua", "lua/" .. basename .. "/init.lua" } +local function profile(m, entry, name, loader) +  if m.profile then +    local mp = m.profile +    mp[entry] = mp[entry] or {} +    if not mp[entry].loader and loader then +      mp[entry].loader = loader +    end +    if not mp[entry][name] then +      mp[entry][name] = uv.hrtime() +    end +  end +end -  for _, path in ipairs(paths) do -    local modpath = get_runtime_file(path, false)[1] -    if modpath then -      local load_start = hrtime() -      local chunk, err = loadfile(modpath) - -      if M.profile then -        M.profile[name] = { -          resolve = load_start - resolve_start, -          load = hrtime() - load_start, -          loader = loader or "standard", -        } -      end +local function mprofile(mod, name, loader) +  profile(M.modpaths, mod, name, loader) +end -      if chunk == nil then -        return err -      end +local function cprofile(path, name, loader) +  profile(M.chunks, path, name, loader) +end -      if is_cacheable(modpath) then -        log("Creating cache for module %s", name) -        M.cache[name] = { modpath, hash(modpath), string.dump(chunk) } -        M.dirty = true +local function get_runtime_file(basename, paths) +  -- Look in the cache to see if we have already loaded a parent module. +  -- If we have then try looking in the parents directory first. +  local parents = vim.split(basename, "/") +  for i = #parents, 1, -1 do +    local parent = table.concat(vim.list_slice(parents, 1, i), "/") +    local ppath = M.modpaths.cache[parent] +    if ppath then +      if ppath:sub(-9) == "/init.lua" then +        ppath = ppath:sub(1, -10) -- a/b/init.lua -> a/b        else -        log("Unable to cache module %s", name) +        ppath = ppath:sub(1, -5) -- a/b.lua -> a/b        end -      return chunk +      for _, path in ipairs(paths) do +        -- path should be of form 'a/b/c.lua' or 'a/b/c/init.lua' +        local modpath = ppath .. "/" .. path:sub(#("lua/" .. parent) + 2) +        if fs_stat(modpath) then +          return modpath, "cache(p)" +        end +      end      end    end -  return nil + +  -- What Neovim does by default; slowest +  local modpath = get_runtime(paths, false, { is_lua = true })[1] +  return modpath, "standard"  end -local reduced_rtp +local function get_runtime_file_cached(basename, paths) +  local mp = M.modpaths +  if mp.cache[basename] then +    local modpath = mp.cache[basename] +    if fs_stat(modpath) then +      mprofile(basename, "resolve_end", "cache") +      return modpath +    end +    mp.cache[basename] = nil +    mp.dirty = true +  end --- Speed up non-cached loads by reducing the rtp path during requires -function M.update_reduced_rtp() -  local luadirs = get_runtime_file("lua/", true) +  local modpath, loader = get_runtime_file(basename, paths) +  if modpath then +    mprofile(basename, "resolve_end", loader) +    log("Creating cache for module %s", basename) +    mp.cache[basename] = modpath_mangle(modpath) +    mp.dirty = true +  end +  return modpath +end -  for i = 1, #luadirs do -    luadirs[i] = luadirs[i]:sub(1, -6) +local function extract_basename(pats) +  local basename + +  -- Deconstruct basename from pats +  for _, pat in ipairs(pats) do +    for i, npat in ipairs { +      -- Ordered by most specific +      "lua/(.*)/init%.lua", +      "lua/(.*)%.lua", +    } do +      local m = pat:match(npat) +      if i == 2 and m and m:sub(-4) == "init" then +        m = m:sub(0, -6) +      end +      if not basename then +        if m then +          basename = m +        end +      elseif m and m ~= basename then +        -- matches are inconsistent +        return +      end +    end    end -  reduced_rtp = table.concat(luadirs, ",") +  return basename  end -local function load_package_with_cache_reduced_rtp(name) -  if vim.in_fast_event() then -    -- Can't set/get options in the fast handler -    return load_package_with_cache(name, "fast") +local function get_runtime_cached(pats, all, opts) +  local fallback = false +  if all or not opts or not opts.is_lua then +    -- Fallback +    fallback = true    end -  local orig_rtp = get_option "runtimepath" -  local orig_ei = get_option "eventignore" -  if not reduced_rtp then -    M.update_reduced_rtp() +  local basename + +  if not fallback then +    basename = extract_basename(pats)    end -  set_option("eventignore", "all") -  set_option("rtp", reduced_rtp) +  if fallback or not basename then +    return get_runtime(pats, all, opts) +  end -  local found = load_package_with_cache(name, "reduced") +  return { get_runtime_file_cached(basename, pats) } +end -  set_option("rtp", orig_rtp) -  set_option("eventignore", orig_ei) +-- Copied from neovim/src/nvim/lua/vim.lua with two lines changed +local function load_package(name) +  local basename = name:gsub("%.", "/") +  local paths = { "lua/" .. basename .. ".lua", "lua/" .. basename .. "/init.lua" } -  return found -end +  -- Original line: +  -- local found = vim.api.nvim__get_runtime(paths, false, {is_lua=true}) +  local found = { get_runtime_file_cached(basename, paths) } +  if #found > 0 then +    local f, err = loadfile(found[1]) +    return f or error(err) +  end -local function load_from_cache(name) -  local resolve_start = hrtime() -  if M.cache[name] == nil then -    log("No cache for module %s", name) -    return "No cache entry" +  local so_paths = {} +  for _, trail in ipairs(vim._so_trails) do +    local path = "lua" .. trail:gsub("?", basename) -- so_trails contains a leading slash +    table.insert(so_paths, path)    end -  local modpath, mhash, codes = unpack(M.cache[name]) +  -- Original line: +  -- found = vim.api.nvim__get_runtime(so_paths, false, {is_lua=true}) +  found = { get_runtime_file_cached(basename, so_paths) } +  if #found > 0 then +    -- Making function name in Lua 5.1 (see src/loadlib.c:mkfuncname) is +    -- a) strip prefix up to and including the first dash, if any +    -- b) replace all dots by underscores +    -- c) prepend "luaopen_" +    -- So "foo-bar.baz" should result in "luaopen_bar_baz" +    local dash = name:find("-", 1, true) +    local modname = dash and name:sub(dash + 1) or name +    local f, err = package.loadlib(found[1], "luaopen_" .. modname:gsub("%.", "_")) +    return f or error(err) +  end +  return nil +end -  if mhash ~= hash(modpath) then -    log("Stale cache for module %s", name) -    M.cache[name] = nil -    M.dirty = true -    return "Stale cache" +local function load_from_cache(path) +  local mc = M.chunks + +  if not mc.cache[path] then +    return nil, string.format("No cache for path %s", path)    end -  local load_start = hrtime() -  local chunk = loadstring(codes) +  local mhash, codes = unpack(mc.cache[path]) -  if M.profile then -    M.profile[name] = { -      resolve = load_start - resolve_start, -      load = hrtime() - load_start, -      loader = "cache", -    } +  if mhash ~= hash(modpath_unmangle(path)) then +    mc.cache[path] = nil +    mc.dirty = true +    return nil, string.format("Stale cache for path %s", path)    end +  local chunk = loadstring(codes) +    if not chunk then -    M.cache[name] = nil -    M.dirty = true -    log("Error loading cache for module. Invalidating", name) -    return "Cache error" +    mc.cache[path] = nil +    mc.dirty = true +    return nil, string.format("Cache error for path %s", path)    end    return chunk  end -function M.save_cache() -  if M.dirty then -    log("Updating cache file: %s", M.path) -    local f = io.open(M.path, "w+b") -    f:write(cachepack.pack(M.cache)) -    f:flush() -    M.dirty = false +local function loadfile_cached(path) +  cprofile(path, "load_start") + +  local chunk, err = load_from_cache(path) +  if chunk and not err then +    log("Loaded cache for path %s", path) +    cprofile(path, "load_end", "cache") +    return chunk    end -end +  log(err) -function M.clear_cache() -  M.cache = {} -  os.remove(M.path) -end +  chunk, err = _loadfile(path) -impatient_dur = uv.hrtime() - impatient_load_start +  if not err then +    log("Creating cache for path %s", path) +    M.chunks.cache[modpath_mangle(path)] = { hash(path), string.dump(chunk) } +    M.chunks.dirty = true +  end -function M.setup(opts) -  opts = opts or {} -  M.path = opts.path or vim.fn.stdpath "cache" .. "/lvim_cache" +  cprofile(path, "load_end", "standard") +  return chunk, err +end -  if opts.enable_profiling then -    M.enable_profile() +function M.save_cache() +  local function _save_cache(t) +    if t.dirty then +      log("Updating chunk cache file: %s", t.path) +      local f = io.open(t.path, "w+b") +      f:write(mpack.encode(t.cache)) +      f:flush() +      t.dirty = false +    end    end +  _save_cache(M.chunks) +  _save_cache(M.modpaths) +end -  local impatient_setup_start = uv.hrtime() -  local stat = uv.fs_stat(M.path) -  if stat then -    log("Loading cache file %s", M.path) -    local ok -    -- Linux/macOS lets us easily mmap the cache file for faster reads without passing to Lua -    if jit.os == "Linux" or jit.os == "OSX" then -      local size = stat.size - -      local C = ffi.C -      local O_RDONLY = 0x00 -      local PROT_READ = 0x01 -      local MAP_PRIVATE = 0x02 - -      ffi.cdef [[ -      int open(const char *pathname, int flags); -      int close(int fd); -      void *mmap(void *addr, size_t length, int prot, int flags, int fd, long int offset); -      int munmap(void *addr, size_t length); -      ]] -      local f = C.open(M.path, O_RDONLY) - -      local addr = C.mmap(nil, size, PROT_READ, MAP_PRIVATE, f, 0) -      ok = ffi.cast("intptr_t", addr) ~= -1 - -      if ok then -        M.cache = cachepack.unpack(ffi.cast("char *", addr), size) -        C.munmap(addr, size) -      end +function M.clear_cache() +  local function _clear_cache(t) +    t.cache = {} +    os.remove(t.path) +  end +  _clear_cache(M.chunks) +  _clear_cache(M.modpaths) +end -      C.close(f) -    else -      local f = io.open(M.path, "rb") -      ok, M.cache = pcall(function() -        return cachepack.unpack(f:read "*a") +local function init_cache() +  local function _init_cache(t) +    if fs_stat(t.path) then +      log("Loading cache file %s", t.path) +      local f = io.open(t.path, "rb") +      local ok +      ok, t.cache = pcall(function() +        return mpack.decode(f:read "*a")        end) -    end -    if not ok then -      log("Corrupted cache file, %s. Invalidating...", M.path) -      os.remove(M.path) -      M.cache = {} +      if not ok then +        log("Corrupted cache file, %s. Invalidating...", t.path) +        os.remove(t.path) +        t.cache = {} +      end +      t.dirty = not ok      end -    M.dirty = not ok    end -  local insert = table.insert -  local package = package +  _init_cache(M.chunks) +  _init_cache(M.modpaths) +end -  -- Fix the position of the preloader. This also makes loading modules like 'ffi' -  -- and 'bit' quicker -  if package.loaders[1] == vim._load_package then -    -- Move vim._load_package to the second position -    local vim_load = table.remove(package.loaders, 1) -    insert(package.loaders, 2, vim_load) -  end +local function setup() +  init_cache() -  insert(package.loaders, 2, load_from_cache) -  insert(package.loaders, 3, load_package_with_cache_reduced_rtp) -  insert(package.loaders, 4, load_package_with_cache) +  -- Override default functions +  vim._load_package = load_package +  vim.api.nvim__get_runtime = get_runtime_cached +  -- luacheck: ignore 121 +  loadfile = loadfile_cached    vim.cmd [[      augroup impatient        autocmd VimEnter,VimLeave * lua _G.__luacache.save_cache() -      autocmd OptionSet runtimepath lua _G.__luacache.update_reduced_rtp(true)      augroup END      command! LuaCacheClear lua _G.__luacache.clear_cache()      command! LuaCacheLog   lua _G.__luacache.print_log()    ]] - -  impatient_dur = impatient_dur + (uv.hrtime() - impatient_setup_start)  end +setup() +  return M | 
