summaryrefslogtreecommitdiff
path: root/.config/nvim/lua/impatient.lua
blob: 7d924d194b1cc43d29ff179b2dd4de80de0ca720 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
-- modified version from https://github.com/lewis6991/impatient.nvim

local vim = vim
local uv = vim.loop
local impatient_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 home_dir = uv.os_homedir()

local impatient_dur

local M = {
  cache = {},
  profile = nil,
  dirty = false,
  path = home_dir .. "/.local/share/nvim/cache",
  log = {},
}

_G.__luacache = M

--{{{
local cachepack = {}

-- using double for packing/unpacking numbers has no conversion overhead
local c_double = ffi.typeof "double[1]"
local sizeof_c_double = ffi.sizeof "double"

local out_buf = {}

function out_buf.write_number(buf, num)
  buf[#buf + 1] = ffi.string(c_double(num), sizeof_c_double)
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("double*", buf.ptr + buf.pos)[0]
  buf.pos = buf.pos + sizeof_c_double
  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
  end

  return cache
end
--}}}

local function log(...)
  M.log[#M.log + 1] = table.concat({ string.format(...) }, " ")
end

function M.print_log()
  for _, l in ipairs(M.log) do
    print(l)
  end
end

function M.enable_profile()
  M.profile = {}
  M.print_profile = function()
    M.profile["impatient"] = {
      resolve = 0,
      load = impatient_dur,
      loader = "standard",
    }
    require("impatient.profile").print_profile(M.profile)
  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/")
end

local function hash(modpath)
  local stat = uv.fs_stat(modpath)
  if stat then
    return stat.mtime.sec
  end
end

local function hrtime()
  if M.profile then
    return uv.hrtime()
  end
end

local function load_package_with_cache(name, loader)
  local resolve_start = hrtime()

  local basename = name:gsub("%.", "/")
  local paths = { "lua/" .. basename .. ".lua", "lua/" .. basename .. "/init.lua" }

  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

      if chunk == nil then
        return err
      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
      else
        log("Unable to cache module %s", name)
      end

      return chunk
    end
  end
  return nil
end

local reduced_rtp

-- Speed up non-cached loads by reducing the rtp path during requires
function M.update_reduced_rtp()
  local luadirs = get_runtime_file("lua/", true)

  for i = 1, #luadirs do
    luadirs[i] = luadirs[i]:sub(1, -6)
  end

  reduced_rtp = table.concat(luadirs, ",")
end

local function load_package_with_cache_reduced_rtp(name)
  local orig_rtp = get_option "runtimepath"
  local orig_ei = get_option "eventignore"

  if not reduced_rtp then
    M.update_reduced_rtp()
  end

  set_option("eventignore", "all")
  set_option("rtp", reduced_rtp)

  local found = load_package_with_cache(name, "reduced")

  set_option("rtp", orig_rtp)
  set_option("eventignore", orig_ei)

  return found
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"
  end

  local modpath, mhash, codes = unpack(M.cache[name])

  if mhash ~= hash(modpath) then
    log("Stale cache for module %s", name)
    M.cache[name] = nil
    M.dirty = true
    return "Stale cache"
  end

  local load_start = hrtime()
  local chunk = loadstring(codes)

  if M.profile then
    M.profile[name] = {
      resolve = load_start - resolve_start,
      load = hrtime() - load_start,
      loader = "cache",
    }
  end

  if not chunk then
    M.cache[name] = nil
    M.dirty = true
    log("Error loading cache for module. Invalidating", name)
    return "Cache error"
  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
  end
end

function M.clear_cache()
  M.cache = {}
  os.remove(M.path)
end

local function setup()
  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

      C.close(f)
    else
      local f = io.open(M.path, "rb")
      ok, M.cache = pcall(function()
        return cachepack.unpack(f:read "*a")
      end)
    end

    if not ok then
      log("Corrupted cache file, %s. Invalidating...", M.path)
      os.remove(M.path)
      M.cache = {}
    end
    M.dirty = not ok
  end

  local insert = table.insert
  local package = package

  -- 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

  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)

  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()
  ]]
end

setup()

impatient_dur = uv.hrtime() - impatient_start

return M