impatient.lua 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. -- modified version from https://github.com/lewis6991/impatient.nvim
  2. local vim = vim
  3. local uv = vim.loop
  4. local impatient_load_start = uv.hrtime()
  5. local api = vim.api
  6. local ffi = require "ffi"
  7. local get_option, set_option = api.nvim_get_option, api.nvim_set_option
  8. local get_runtime_file = api.nvim_get_runtime_file
  9. local impatient_dur
  10. local M = {
  11. cache = {},
  12. profile = nil,
  13. dirty = false,
  14. path = nil,
  15. log = {},
  16. }
  17. _G.__luacache = M
  18. --{{{
  19. local cachepack = {}
  20. -- using double for packing/unpacking numbers has no conversion overhead
  21. -- 32-bit ARM causes a bus error when casting to double, so use int there
  22. local number_t = jit.arch ~= "arm" and "double" or "int"
  23. ffi.cdef("typedef " .. number_t .. " number_t;")
  24. local c_number_t = ffi.typeof "number_t[1]"
  25. local c_sizeof_number_t = ffi.sizeof "number_t"
  26. local out_buf = {}
  27. function out_buf.write_number(buf, num)
  28. buf[#buf + 1] = ffi.string(c_number_t(num), c_sizeof_number_t)
  29. end
  30. function out_buf.write_string(buf, str)
  31. out_buf.write_number(buf, #str)
  32. buf[#buf + 1] = str
  33. end
  34. function out_buf.to_string(buf)
  35. return table.concat(buf)
  36. end
  37. local in_buf = {}
  38. function in_buf.read_number(buf)
  39. if buf.size < buf.pos then
  40. error "buffer access violation"
  41. end
  42. local res = ffi.cast("number_t*", buf.ptr + buf.pos)[0]
  43. buf.pos = buf.pos + c_sizeof_number_t
  44. return res
  45. end
  46. function in_buf.read_string(buf)
  47. local len = in_buf.read_number(buf)
  48. local res = ffi.string(buf.ptr + buf.pos, len)
  49. buf.pos = buf.pos + len
  50. return res
  51. end
  52. function cachepack.pack(cache)
  53. local total_keys = vim.tbl_count(cache)
  54. local buf = {}
  55. out_buf.write_number(buf, total_keys)
  56. for k, v in pairs(cache) do
  57. out_buf.write_string(buf, k)
  58. out_buf.write_string(buf, v[1] or "")
  59. out_buf.write_number(buf, v[2] or 0)
  60. out_buf.write_string(buf, v[3] or "")
  61. end
  62. return out_buf.to_string(buf)
  63. end
  64. function cachepack.unpack(str, raw_buf_size)
  65. if raw_buf_size == 0 or str == nil or (raw_buf_size == nil and #str == 0) then
  66. return {}
  67. end
  68. local buf = {
  69. ptr = raw_buf_size and str or ffi.new("const char[?]", #str, str),
  70. pos = 0,
  71. size = raw_buf_size or #str,
  72. }
  73. local cache = {}
  74. local total_keys = in_buf.read_number(buf)
  75. for _ = 1, total_keys do
  76. local k = in_buf.read_string(buf)
  77. local v = {
  78. in_buf.read_string(buf),
  79. in_buf.read_number(buf),
  80. in_buf.read_string(buf),
  81. }
  82. cache[k] = v
  83. end
  84. return cache
  85. end
  86. --}}}
  87. local function log(...)
  88. M.log[#M.log + 1] = table.concat({ string.format(...) }, " ")
  89. end
  90. function M.print_log()
  91. for _, l in ipairs(M.log) do
  92. print(l)
  93. end
  94. end
  95. function M.enable_profile()
  96. M.profile = {}
  97. M.print_profile = function()
  98. M.profile["lvim.impatient"] = {
  99. resolve = 0,
  100. load = impatient_dur,
  101. loader = "standard",
  102. }
  103. require("lvim.impatient.profile").print_profile(M.profile)
  104. end
  105. vim.cmd [[command! LuaCacheProfile lua _G.__luacache.print_profile()]]
  106. end
  107. local function is_cacheable(path)
  108. -- Don't cache files in /tmp since they are not likely to persist.
  109. -- Note: Appimage versions of Neovim mount $VIMRUNTIME in /tmp in a unique
  110. -- directory on each launch.
  111. return not vim.startswith(path, "/tmp/")
  112. end
  113. local function hash(modpath)
  114. local stat = uv.fs_stat(modpath)
  115. if stat then
  116. return stat.mtime.sec
  117. end
  118. end
  119. local function hrtime()
  120. if M.profile then
  121. return uv.hrtime()
  122. end
  123. end
  124. local function load_package_with_cache(name, loader)
  125. local resolve_start = hrtime()
  126. local basename = name:gsub("%.", "/")
  127. local paths = { "lua/" .. basename .. ".lua", "lua/" .. basename .. "/init.lua" }
  128. for _, path in ipairs(paths) do
  129. local modpath = get_runtime_file(path, false)[1]
  130. if modpath then
  131. local load_start = hrtime()
  132. local chunk, err = loadfile(modpath)
  133. if M.profile then
  134. M.profile[name] = {
  135. resolve = load_start - resolve_start,
  136. load = hrtime() - load_start,
  137. loader = loader or "standard",
  138. }
  139. end
  140. if chunk == nil then
  141. return err
  142. end
  143. if is_cacheable(modpath) then
  144. log("Creating cache for module %s", name)
  145. M.cache[name] = { modpath, hash(modpath), string.dump(chunk) }
  146. M.dirty = true
  147. else
  148. log("Unable to cache module %s", name)
  149. end
  150. return chunk
  151. end
  152. end
  153. return nil
  154. end
  155. local reduced_rtp
  156. -- Speed up non-cached loads by reducing the rtp path during requires
  157. function M.update_reduced_rtp()
  158. local luadirs = get_runtime_file("lua/", true)
  159. for i = 1, #luadirs do
  160. luadirs[i] = luadirs[i]:sub(1, -6)
  161. end
  162. reduced_rtp = table.concat(luadirs, ",")
  163. end
  164. local function load_package_with_cache_reduced_rtp(name)
  165. if vim.in_fast_event() then
  166. -- Can't set/get options in the fast handler
  167. return load_package_with_cache(name, "fast")
  168. end
  169. local orig_rtp = get_option "runtimepath"
  170. local orig_ei = get_option "eventignore"
  171. if not reduced_rtp then
  172. M.update_reduced_rtp()
  173. end
  174. set_option("eventignore", "all")
  175. set_option("rtp", reduced_rtp)
  176. local found = load_package_with_cache(name, "reduced")
  177. set_option("rtp", orig_rtp)
  178. set_option("eventignore", orig_ei)
  179. return found
  180. end
  181. local function load_from_cache(name)
  182. local resolve_start = hrtime()
  183. if M.cache[name] == nil then
  184. log("No cache for module %s", name)
  185. return "No cache entry"
  186. end
  187. local modpath, mhash, codes = unpack(M.cache[name])
  188. if mhash ~= hash(modpath) then
  189. log("Stale cache for module %s", name)
  190. M.cache[name] = nil
  191. M.dirty = true
  192. return "Stale cache"
  193. end
  194. local load_start = hrtime()
  195. local chunk = loadstring(codes)
  196. if M.profile then
  197. M.profile[name] = {
  198. resolve = load_start - resolve_start,
  199. load = hrtime() - load_start,
  200. loader = "cache",
  201. }
  202. end
  203. if not chunk then
  204. M.cache[name] = nil
  205. M.dirty = true
  206. log("Error loading cache for module. Invalidating", name)
  207. return "Cache error"
  208. end
  209. return chunk
  210. end
  211. function M.save_cache()
  212. if M.dirty then
  213. log("Updating cache file: %s", M.path)
  214. local f = io.open(M.path, "w+b")
  215. f:write(cachepack.pack(M.cache))
  216. f:flush()
  217. M.dirty = false
  218. end
  219. end
  220. function M.clear_cache()
  221. M.cache = {}
  222. os.remove(M.path)
  223. end
  224. impatient_dur = uv.hrtime() - impatient_load_start
  225. function M.setup(opts)
  226. opts = opts or {}
  227. M.path = opts.path or vim.fn.stdpath "cache" .. "/lvim_cache"
  228. if opts.enable_profiling then
  229. M.enable_profile()
  230. end
  231. local impatient_setup_start = uv.hrtime()
  232. local stat = uv.fs_stat(M.path)
  233. if stat then
  234. log("Loading cache file %s", M.path)
  235. local ok
  236. -- Linux/macOS lets us easily mmap the cache file for faster reads without passing to Lua
  237. if jit.os == "Linux" or jit.os == "OSX" then
  238. local size = stat.size
  239. local C = ffi.C
  240. local O_RDONLY = 0x00
  241. local PROT_READ = 0x01
  242. local MAP_PRIVATE = 0x02
  243. ffi.cdef [[
  244. int open(const char *pathname, int flags);
  245. int close(int fd);
  246. void *mmap(void *addr, size_t length, int prot, int flags, int fd, long int offset);
  247. int munmap(void *addr, size_t length);
  248. ]]
  249. local f = C.open(M.path, O_RDONLY)
  250. local addr = C.mmap(nil, size, PROT_READ, MAP_PRIVATE, f, 0)
  251. ok = ffi.cast("intptr_t", addr) ~= -1
  252. if ok then
  253. M.cache = cachepack.unpack(ffi.cast("char *", addr), size)
  254. C.munmap(addr, size)
  255. end
  256. C.close(f)
  257. else
  258. local f = io.open(M.path, "rb")
  259. ok, M.cache = pcall(function()
  260. return cachepack.unpack(f:read "*a")
  261. end)
  262. end
  263. if not ok then
  264. log("Corrupted cache file, %s. Invalidating...", M.path)
  265. os.remove(M.path)
  266. M.cache = {}
  267. end
  268. M.dirty = not ok
  269. end
  270. local insert = table.insert
  271. local package = package
  272. -- Fix the position of the preloader. This also makes loading modules like 'ffi'
  273. -- and 'bit' quicker
  274. if package.loaders[1] == vim._load_package then
  275. -- Move vim._load_package to the second position
  276. local vim_load = table.remove(package.loaders, 1)
  277. insert(package.loaders, 2, vim_load)
  278. end
  279. insert(package.loaders, 2, load_from_cache)
  280. insert(package.loaders, 3, load_package_with_cache_reduced_rtp)
  281. insert(package.loaders, 4, load_package_with_cache)
  282. vim.cmd [[
  283. augroup impatient
  284. autocmd VimEnter,VimLeave * lua _G.__luacache.save_cache()
  285. autocmd OptionSet runtimepath lua _G.__luacache.update_reduced_rtp(true)
  286. augroup END
  287. command! LuaCacheClear lua _G.__luacache.clear_cache()
  288. command! LuaCacheLog lua _G.__luacache.print_log()
  289. ]]
  290. impatient_dur = impatient_dur + (uv.hrtime() - impatient_setup_start)
  291. end
  292. return M