impatient.lua 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. local orig_rtp = get_option "runtimepath"
  166. local orig_ei = get_option "eventignore"
  167. if not reduced_rtp then
  168. M.update_reduced_rtp()
  169. end
  170. set_option("eventignore", "all")
  171. set_option("rtp", reduced_rtp)
  172. local found = load_package_with_cache(name, "reduced")
  173. set_option("rtp", orig_rtp)
  174. set_option("eventignore", orig_ei)
  175. return found
  176. end
  177. local function load_from_cache(name)
  178. local resolve_start = hrtime()
  179. if M.cache[name] == nil then
  180. log("No cache for module %s", name)
  181. return "No cache entry"
  182. end
  183. local modpath, mhash, codes = unpack(M.cache[name])
  184. if mhash ~= hash(modpath) then
  185. log("Stale cache for module %s", name)
  186. M.cache[name] = nil
  187. M.dirty = true
  188. return "Stale cache"
  189. end
  190. local load_start = hrtime()
  191. local chunk = loadstring(codes)
  192. if M.profile then
  193. M.profile[name] = {
  194. resolve = load_start - resolve_start,
  195. load = hrtime() - load_start,
  196. loader = "cache",
  197. }
  198. end
  199. if not chunk then
  200. M.cache[name] = nil
  201. M.dirty = true
  202. log("Error loading cache for module. Invalidating", name)
  203. return "Cache error"
  204. end
  205. return chunk
  206. end
  207. function M.save_cache()
  208. if M.dirty then
  209. log("Updating cache file: %s", M.path)
  210. local f = io.open(M.path, "w+b")
  211. f:write(cachepack.pack(M.cache))
  212. f:flush()
  213. M.dirty = false
  214. end
  215. end
  216. function M.clear_cache()
  217. M.cache = {}
  218. os.remove(M.path)
  219. end
  220. impatient_dur = uv.hrtime() - impatient_load_start
  221. function M.setup(opts)
  222. opts = opts or {}
  223. M.path = opts.path or vim.fn.stdpath "cache" .. "/lvim_cache"
  224. if opts.enable_profiling then
  225. M.enable_profile()
  226. end
  227. local impatient_setup_start = uv.hrtime()
  228. local stat = uv.fs_stat(M.path)
  229. if stat then
  230. log("Loading cache file %s", M.path)
  231. local ok
  232. -- Linux/macOS lets us easily mmap the cache file for faster reads without passing to Lua
  233. if jit.os == "Linux" or jit.os == "OSX" then
  234. local size = stat.size
  235. local C = ffi.C
  236. local O_RDONLY = 0x00
  237. local PROT_READ = 0x01
  238. local MAP_PRIVATE = 0x02
  239. ffi.cdef [[
  240. int open(const char *pathname, int flags);
  241. int close(int fd);
  242. void *mmap(void *addr, size_t length, int prot, int flags, int fd, long int offset);
  243. int munmap(void *addr, size_t length);
  244. ]]
  245. local f = C.open(M.path, O_RDONLY)
  246. local addr = C.mmap(nil, size, PROT_READ, MAP_PRIVATE, f, 0)
  247. ok = ffi.cast("intptr_t", addr) ~= -1
  248. if ok then
  249. M.cache = cachepack.unpack(ffi.cast("char *", addr), size)
  250. C.munmap(addr, size)
  251. end
  252. C.close(f)
  253. else
  254. local f = io.open(M.path, "rb")
  255. ok, M.cache = pcall(function()
  256. return cachepack.unpack(f:read "*a")
  257. end)
  258. end
  259. if not ok then
  260. log("Corrupted cache file, %s. Invalidating...", M.path)
  261. os.remove(M.path)
  262. M.cache = {}
  263. end
  264. M.dirty = not ok
  265. end
  266. local insert = table.insert
  267. local package = package
  268. -- Fix the position of the preloader. This also makes loading modules like 'ffi'
  269. -- and 'bit' quicker
  270. if package.loaders[1] == vim._load_package then
  271. -- Move vim._load_package to the second position
  272. local vim_load = table.remove(package.loaders, 1)
  273. insert(package.loaders, 2, vim_load)
  274. end
  275. insert(package.loaders, 2, load_from_cache)
  276. insert(package.loaders, 3, load_package_with_cache_reduced_rtp)
  277. insert(package.loaders, 4, load_package_with_cache)
  278. vim.cmd [[
  279. augroup impatient
  280. autocmd VimEnter,VimLeave * lua _G.__luacache.save_cache()
  281. autocmd OptionSet runtimepath lua _G.__luacache.update_reduced_rtp(true)
  282. augroup END
  283. command! LuaCacheClear lua _G.__luacache.clear_cache()
  284. command! LuaCacheLog lua _G.__luacache.print_log()
  285. ]]
  286. impatient_dur = impatient_dur + (uv.hrtime() - impatient_setup_start)
  287. end
  288. return M