Skip to content

Commit

Permalink
refactor: use nvim_set_hl to speed up startup time
Browse files Browse the repository at this point in the history
On my machine, prior to this change, the colorscheme adds about 8ms to
the startup time of neovim. Using the newly introduced API
`nvim_set_hl`, it’s a little under 3ms, so that’s more than a two times
speedup!

Special care is taken to handle the `fmt` argument, as that’s the least
friendly to `nvim_set_hl`.

This change strives to be backward compatible. Once neovim version 0.7.0
will be widely spread enough, further performance gain are within reach
by:
* removing the fallback that calls `string.format`
* defining the `group_settings` so that those can be passed to `nvim_set_hl` directly
  • Loading branch information
cljoly committed Oct 2, 2022
1 parent 6c72a9c commit 4e6f0e9
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion lua/onedark/highlights.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,23 @@ local util = require("onedark.util")
local M = {}
local hl = {langs = {}, plugins = {}}

local function vim_highlights(highlights)
local function vim_highlights_nvim070(highlights)
for group_name, group_settings in pairs(highlights) do
local settings = {
fg = group_settings.fg or "none",
bg = group_settings.bg or "none",
sp = group_settings.sp or "none",
}
if not group_settings.fmt == nil then
for setting in vim.split(group_settings, ",") do
settings[setting] = 1
end
end
vim.api.nvim_set_hl(0, group_name, settings)
end
end

local function vim_highlights_prior_to_nvim070(highlights)
for group_name, group_settings in pairs(highlights) do
vim.api.nvim_command(string.format("highlight %s guifg=%s guibg=%s guisp=%s gui=%s", group_name,
group_settings.fg or "none",
Expand All @@ -15,6 +31,11 @@ local function vim_highlights(highlights)
end
end

local vim_highlights = vim_highlights_prior_to_nvim070
if vim.fn.has('nvim-0.7.0') then
vim_highlights = vim_highlights_nvim070
end

local colors = {
Fg = {fg = c.fg},
LightGrey = {fg = c.light_grey},
Expand Down

0 comments on commit 4e6f0e9

Please sign in to comment.