Skip to content

Commit 4e1a909

Browse files
committed
[Neovim] Improve startup time
When I switched from [lazy.nvim] to `vim.pack`, I lost the ability to lazily load plugins behind events of key bindings. I profiled the behavior with `nvim --startuptime` and it seemed okay. When I loaded up the changes on a computer with a much larger directory with a much larger file history, I could tell that the startup was noticeably slower. According to `nvim --startuptime`, it took 3.5 *seconds* to load. The biggest offender was calculating the stats about plugins for my dashboard. Removing this took off about 3 seconds by itself. While it was nice to have, I honestly never paid any attention to it. After this I began moving plugins behind events. I'd originally used `vim.schedule` because I (wrongly) thought this would run the callback _after_ startup. It turns out it runs the callback more or less after the current event's callbacks are done. There are a few instances where I took advantage of this by using `vim.schedule` instead of callback. This all became enough of a pattern that I added a `load_on` helper. Finally, because I was already well down the rabbit hole, I learned that enabling spellcheck was taking about 10 milliseconds. I decided to drop this, too, behind an event. All of these changes together helped me reduce the 3.5 second startup time to 45 milliseconds. I'm sure I'll discover things that don't work quite right or don't load early enough, but we'll see how it goes as I use it.
1 parent 9ee6ac6 commit 4e1a909

22 files changed

Lines changed: 216 additions & 230 deletions

.config/nvim/init.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
-- Set the leader first so that any plugins providing mappings will pick it up.
22
vim.g.mapleader = [[ ]]
33

4+
-- Load plugin helpers.
5+
require("dirn.plugins")
6+
47
-- Initialize file types.
58
require("dirn.filetypes")
69

.config/nvim/lua/dirn/dashboard.lua

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -92,51 +92,6 @@ local function header()
9292
}
9393
end
9494

95-
local function plugins()
96-
local opts = {
97-
type = "text",
98-
val = "You don't appear to be using any plugins.",
99-
opts = { position = "center", hl = "Comment" },
100-
}
101-
102-
vim.api.nvim_create_autocmd({ "User" }, {
103-
pattern = { "AlphaReady" },
104-
callback = function()
105-
local ok, _ = pcall(require, "mason")
106-
if ok then
107-
vim.keymap.set("n", "M", function()
108-
vim.cmd.Mason()
109-
end, { silent = true, noremap = true, buffer = true })
110-
end
111-
end,
112-
})
113-
114-
local all = vim.pack.get()
115-
if #all > 0 then
116-
local total = #all
117-
118-
local active = vim
119-
.iter(all)
120-
:filter(function(x)
121-
return x.active
122-
end)
123-
:totable()
124-
125-
local loaded = "All"
126-
if total ~= #active then
127-
loaded = tostring(#active)
128-
end
129-
130-
opts.val = "You are using "
131-
.. total
132-
.. " plugins. "
133-
.. loaded
134-
.. " of them are loaded."
135-
end
136-
137-
return { opts }
138-
end
139-
14095
local function recents()
14196
function recent(filename, label, shortcut)
14297
local action = "<cmd>e " .. filename .. "<cr>"
@@ -301,7 +256,6 @@ local sections = {
301256
footer = { type = "group", val = footer },
302257
greeting = { type = "group", val = greeting },
303258
header = { type = "group", val = header },
304-
plugins = { type = "group", val = plugins },
305259
recents = { type = "group", val = recents },
306260
shortcuts = { type = "group", val = shortcuts },
307261
}
@@ -317,8 +271,6 @@ local config = {
317271
padding[1],
318272
sections.recents,
319273
padding[4],
320-
sections.plugins,
321-
padding[1],
322274
sections.footer,
323275
},
324276
opts = { margin = 3, redraw_on_resize = false },

.config/nvim/lua/dirn/plugins.lua

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---@param event string|string[]
2+
---@param callback fun(args: table)
3+
---@param opts? {schedule?: boolean}
4+
function load_on(event, callback, opts)
5+
opts = opts or {}
6+
7+
vim.api.nvim_create_autocmd(event, {
8+
once = true,
9+
callback = opts.schedule and vim.schedule_wrap(callback) or callback,
10+
})
11+
end

.config/nvim/lua/dirn/settings.lua

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,12 @@ vim.opt.number = true
8181
vim.opt.title = true
8282

8383
-- Use spell check.
84-
vim.opt.spell = true
84+
vim.api.nvim_create_autocmd({ "BufReadPost", "BufNewFile" }, {
85+
once = true,
86+
callback = function()
87+
vim.opt.spell = true
88+
end,
89+
})
8590

8691
-- Make horizontal scrolling smoother.
8792
vim.opt.sidescroll = 1

.config/nvim/plugin/cmp.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
vim.schedule(function()
1+
load_on("InsertEnter", function()
22
vim.pack.add({
33
"https://github.com/hrsh7th/nvim-cmp",
44

.config/nvim/plugin/comment.lua

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
vim.pack.add({
2-
"https://github.com/numToStr/Comment.nvim",
3-
})
1+
load_on({ "BufReadPost", "BufNewFile" }, function()
2+
vim.pack.add({
3+
"https://github.com/numToStr/Comment.nvim",
4+
})
45

5-
require("comment").setup()
6+
require("comment").setup()
7+
end)

.config/nvim/plugin/committia.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
-- NOTE: If this plugin is deferred, it doesn't load in time to take over the commit
2+
-- message editor view.
13
vim.pack.add({
24
"https://github.com/rhysd/committia.vim",
35
})

.config/nvim/plugin/conform.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
vim.schedule(function()
1+
load_on("BufWritePre", function()
22
vim.pack.add({
33
"https://github.com/stevearc/conform.nvim",
44
})

.config/nvim/plugin/diminactive.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
vim.schedule(function()
1+
load_on("UIEnter", function()
22
vim.pack.add({
33
"https://github.com/blueyed/vim-diminactive",
44
})

.config/nvim/plugin/exchange.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
vim.schedule(function()
1+
load_on({ "BufReadPost", "BufNewFile" }, function()
22
vim.pack.add({
33
"https://github.com/tommcdo/vim-exchange",
44
})

0 commit comments

Comments
 (0)