diff --git a/.github/workflows/install.yaml b/.github/workflows/install.yaml index 6e3685dd4ba..54da94bcd0c 100644 --- a/.github/workflows/install.yaml +++ b/.github/workflows/install.yaml @@ -23,7 +23,6 @@ jobs: - runner: macos-10.15 os: osx runs-on: ${{ matrix.runner }} - if: github.event.pull_request.draft == false steps: - uses: actions/checkout@v2 diff --git a/lua/lvim/bootstrap.lua b/lua/lvim/bootstrap.lua index c324055a783..d409e732373 100644 --- a/lua/lvim/bootstrap.lua +++ b/lua/lvim/bootstrap.lua @@ -96,7 +96,8 @@ function M:init(base_dir) if not in_headless then _G.PLENARY_DEBUG = false require("lvim.impatient").setup { - path = join_paths(self.cache_dir, "lvim_cache"), + chunks = { path = join_paths(get_cache_dir(), "lvim_luacache_chunks") }, + modpaths = { path = join_paths(get_cache_dir(), "lvim_luacache_modpaths") }, enable_profiling = true, } end diff --git a/lua/lvim/impatient.lua b/lua/lvim/impatient.lua index 230e5195026..6e91cfb1d4d 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,320 @@ 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 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 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" + -- 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 - local modpath, mhash, codes = unpack(M.cache[name]) +local function load_from_cache(path) + local mc = M.chunks - if mhash ~= hash(modpath) then - log("Stale cache for module %s", name) - M.cache[name] = nil - M.dirty = true - return "Stale cache" + 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 + +function M.setup(opts) + opts = opts or {} + opts.chunks = opts.chunks or {} + opts.modpaths = opts.modpaths or {} + M.chunks.path = opts.chunks.path or vim.fn.stdpath "cache" .. "/luacache_chunks" + M.modpaths.path = opts.modpaths.path or vim.fn.stdpath "cache" .. "/luacache_modpaths" - -- 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) + if opts.enable_profiling then + M.enable_profile() 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) + init_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 return M diff --git a/lua/lvim/impatient/profile.lua b/lua/lvim/impatient/profile.lua index 0f4f82363d5..814388542f9 100644 --- a/lua/lvim/impatient/profile.lua +++ b/lua/lvim/impatient/profile.lua @@ -1,145 +1,237 @@ local M = {} -local api = vim.api +local api, uv = vim.api, vim.loop -function M.print_profile(profile) - if not profile then +local std_data = get_runtime_dir() -- vim.fn.stdpath "data" +local std_config = get_config_dir() -- vim.fn.stdpath "config" +local vimruntime = os.getenv "VIMRUNTIME" + +local function load_buffer(title, lines) + local bufnr = api.nvim_create_buf(false, false) + api.nvim_buf_set_lines(bufnr, 0, 0, false, lines) + api.nvim_buf_set_option(bufnr, "bufhidden", "wipe") + api.nvim_buf_set_option(bufnr, "buftype", "nofile") + api.nvim_buf_set_option(bufnr, "swapfile", false) + api.nvim_buf_set_option(bufnr, "modifiable", false) + api.nvim_buf_set_name(bufnr, title) + api.nvim_set_current_buf(bufnr) +end + +local function mod_path(path) + if not path then + return "?" + end + path = path:gsub(std_data .. "/site/pack/packer/", "/") + path = path:gsub(std_data .. "/", "/") + path = path:gsub(std_config .. "/", "/") + path = path:gsub(vimruntime .. "/", "/") + return path +end + +local function time_tostr(x) + if x == 0 then + return "?" + end + return string.format("%8.3fms", x) +end + +local function mem_tostr(x) + local unit = "" + for _, u in ipairs { "K", "M", "G" } do + if x < 1000 then + break + end + x = x / 1000 + unit = u + end + return string.format("%1.1f%s", x, unit) +end + +function M.print_profile(I) + local mod_profile = I.modpaths.profile + local chunk_profile = I.chunks.profile + + if not mod_profile and not chunk_profile then print "Error: profiling was not enabled" return end local total_resolve = 0 local total_load = 0 - local name_pad = 0 local modules = {} - local plugins = {} - - for module, p in pairs(profile) do - p.resolve = p.resolve / 1000000 - p.load = p.load / 1000000 - p.total = p.resolve + p.load - p.module = module:gsub("/", ".") - - local plugin = p.module:match "([^.]+)" - if plugin then - if not plugins[plugin] then - plugins[plugin] = { - module = plugin, - resolve = 0, - load = 0, - total = 0, - } - end - local r = plugins[plugin] - r.resolve = r.resolve + p.resolve - r.load = r.load + p.load - r.total = r.total + p.total + for path, m in pairs(chunk_profile) do + m.load = m.load_end - m.load_start + m.load = m.load / 1000000 + m.path = mod_path(path) + end - if not r.loader then - r.loader = p.loader - elseif r.loader ~= p.loader then - r.loader = "mixed" - end + local module_content_width = 0 + + for module, m in pairs(mod_profile) do + m.resolve = 0 + if m.resolve_end then + m.resolve = m.resolve_end - m.resolve_start + m.resolve = m.resolve / 1000000 end - total_resolve = total_resolve + p.resolve - total_load = total_load + p.load + m.module = module:gsub("/", ".") + m.loader = m.loader or m.loader_guess + + local path = I.modpaths.cache[module] + local path_prof = chunk_profile[path] + m.path = mod_path(path) - if #module > name_pad then - name_pad = #module + if path_prof then + chunk_profile[path] = nil + m.load = path_prof.load + m.ploader = path_prof.loader + else + m.load = 0 + m.ploader = "NA" end - modules[#modules + 1] = p + total_resolve = total_resolve + m.resolve + total_load = total_load + m.load + + if #module > module_content_width then + module_content_width = #module + end + + modules[#modules + 1] = m end table.sort(modules, function(a, b) - return a.module > b.module + return (a.resolve + a.load) > (b.resolve + b.load) end) - do - local plugins_a = {} - for _, v in pairs(plugins) do - plugins_a[#plugins_a + 1] = v - end - plugins = plugins_a + local paths = {} + + local total_paths_load = 0 + for _, m in pairs(chunk_profile) do + paths[#paths + 1] = m + total_paths_load = total_paths_load + m.load end - table.sort(plugins, function(a, b) - return a.total > b.total + table.sort(paths, function(a, b) + return a.load > b.load end) local lines = {} - local function add(...) - lines[#lines + 1] = string.format(...) - end + local function add(fmt, ...) + local args = { ... } + for i, a in ipairs(args) do + if type(a) == "number" then + args[i] = time_tostr(a) + end + end - local l = string.rep("─", name_pad + 1) + lines[#lines + 1] = string.format(fmt, unpack(args)) + end - add( - "%s┬───────────┬────────────┬────────────┬────────────┐", - l - ) - add("%-" .. name_pad .. "s │ Loader │ Resolve │ Load │ Total │", "") - add( - "%s┼───────────┼────────────┼────────────┼────────────┤", - l - ) - add( - "%-" .. name_pad .. "s │ │ %8.4fms │ %8.4fms │ %8.4fms │", - "Total", - total_resolve, - total_load, - total_resolve + total_load - ) - add( - "%s┴───────────┴────────────┴────────────┴────────────┤", - l + local time_cell_width = 12 + local loader_cell_width = 11 + local time_content_width = time_cell_width - 2 + local loader_content_width = loader_cell_width - 2 + local module_cell_width = module_content_width + 2 + + local tcwl = string.rep("─", time_cell_width) + local lcwl = string.rep("─", loader_cell_width) + local mcwl = string.rep("─", module_cell_width + 2) + + local n = string.rep("─", 200) + + local module_cell_format = "%-" .. module_cell_width .. "s" + local loader_format = "%-" .. loader_content_width .. "s" + local line_format = "%s │ %s │ %s │ %s │ %s │ %s" + + local row_fmt = line_format:format( + " %" .. time_content_width .. "s", + loader_format, + "%" .. time_content_width .. "s", + loader_format, + module_cell_format, + "%s" ) - add("%-" .. name_pad .. "s │", "By Plugin") - add( - "%s┬───────────┬────────────┬────────────┬────────────┤", - l + + local title_fmt = line_format:format( + " %-" .. time_content_width .. "s", + loader_format, + "%-" .. time_content_width .. "s", + loader_format, + module_cell_format, + "%s" ) - for _, p in ipairs(plugins) do - add( - "%-" .. name_pad .. "s │ %9s │ %8.4fms │ %8.4fms │ %8.4fms │", - p.module, - p.loader, - p.resolve, - p.load, - p.total - ) + + local title1_width = time_cell_width + loader_cell_width - 1 + local title1_fmt = ("%s │ %s │"):format(" %-" .. title1_width .. "s", "%-" .. title1_width .. "s") + + add "Note: this report is not a measure of startup time. Only use this for comparing" + add "between cached and uncached loads of Lua modules" + add "" + + add "Cache files:" + for _, f in ipairs { I.chunks.path, I.modpaths.path } do + local size = vim.loop.fs_stat(f).size + add(" %s %s", f, mem_tostr(size)) end - add( - "%s┴───────────┴────────────┴────────────┴────────────┤", - l - ) - add("%-" .. name_pad .. "s │", "By Module") - add( - "%s┬───────────┬────────────┬────────────┬────────────┤", - l - ) - for _, p in pairs(modules) do - add( - "%-" .. name_pad .. "s │ %9s │ %8.4fms │ %8.4fms │ %8.4fms │", - p.module, - p.loader, - p.resolve, - p.load, - p.total - ) + add "" + + add("%s─%s┬%s─%s┐", tcwl, lcwl, tcwl, lcwl) + add(title1_fmt, "Resolve", "Load") + add("%s┬%s┼%s┬%s┼%s┬%s", tcwl, lcwl, tcwl, lcwl, mcwl, n) + add(title_fmt, "Time", "Method", "Time", "Method", "Module", "Path") + add("%s┼%s┼%s┼%s┼%s┼%s", tcwl, lcwl, tcwl, lcwl, mcwl, n) + add(row_fmt, total_resolve, "", total_load, "", "Total", "") + add("%s┼%s┼%s┼%s┼%s┼%s", tcwl, lcwl, tcwl, lcwl, mcwl, n) + for _, p in ipairs(modules) do + add(row_fmt, p.resolve, p.loader, p.load, p.ploader, p.module, p.path) + end + add("%s┴%s┴%s┴%s┴%s┴%s", tcwl, lcwl, tcwl, lcwl, mcwl, n) + + if #paths > 0 then + add "" + add(n) + local f3 = " %" .. time_content_width .. "s │ %" .. loader_content_width .. "s │ %s" + add "Files loaded with no associated module" + add("%s┬%s┬%s", tcwl, lcwl, n) + add(f3, "Time", "Loader", "Path") + add("%s┼%s┼%s", tcwl, lcwl, n) + add(f3, total_paths_load, "", "Total") + add("%s┼%s┼%s", tcwl, lcwl, n) + for _, p in ipairs(paths) do + add(f3, p.load, p.loader, p.path) + end + add("%s┴%s┴%s", tcwl, lcwl, n) + add "" end - add( - "%s┴───────────┴────────────┴────────────┴────────────┘", - l - ) - local bufnr = api.nvim_create_buf(false, false) - api.nvim_buf_set_lines(bufnr, 0, 0, false, lines) - api.nvim_buf_set_option(bufnr, "buftype", "nofile") - api.nvim_buf_set_name(bufnr, "Impatient Profile Report") - api.nvim_set_current_buf(bufnr) + load_buffer("Impatient Profile Report", lines) +end + +M.setup = function(profile) + local _require = require + + require = function(mod) + local basename = mod:gsub("%.", "/") + if not profile[basename] then + profile[basename] = {} + profile[basename].resolve_start = uv.hrtime() + profile[basename].loader_guess = "C" + end + return _require(mod) + end + + -- Add profiling around all the loaders + local pl = package.loaders + for i = 1, #pl do + local l = pl[i] + pl[i] = function(mod) + local basename = mod:gsub("%.", "/") + profile[basename].loader_guess = i == 1 and "preloader" or "loader #" .. i + return l(mod) + end + end end return M