From 4e6f0e917cfbe5e043c19a9527431a093102dfff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Joly?= Date: Sun, 8 May 2022 12:16:08 +0000 Subject: [PATCH 1/2] refactor: use nvim_set_hl to speed up startup time MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- lua/onedark/highlights.lua | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/lua/onedark/highlights.lua b/lua/onedark/highlights.lua index a43a9332..2f204148 100644 --- a/lua/onedark/highlights.lua +++ b/lua/onedark/highlights.lua @@ -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", @@ -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}, From e589ba1af0abc725d04c3064da687fafb755edd0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Joly?= Date: Tue, 10 May 2022 08:16:16 +0100 Subject: [PATCH 2/2] fixup! refactor: use nvim_set_hl to speed up startup time --- lua/onedark/highlights.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/onedark/highlights.lua b/lua/onedark/highlights.lua index 2f204148..2594135d 100644 --- a/lua/onedark/highlights.lua +++ b/lua/onedark/highlights.lua @@ -12,8 +12,8 @@ local function vim_highlights_nvim070(highlights) 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 + if group_settings.fmt and group_settings.fmt ~= "none" then + for _, setting in pairs(vim.split(group_settings.fmt, ",")) do settings[setting] = 1 end end