rakr / vim-one

Adaptation of one-light and one-dark colorschemes for Vim
MIT License
1.98k stars 196 forks source link

Improve startup time by hard-coding the 256 color #93

Closed laggardkernel closed 5 years ago

laggardkernel commented 5 years ago

vim-one converts the color hex code to 256 color code in highlight command wrapper function called <sid>X(group, fg, bg, attr). The unnecessary calculation in all of its 380+ highlight definitions makes vim-one one of the slowest vim colorschemes.

I realized this from the explanation made by @geoffharcourt at issue #74 . Someone reported the slow startup caused by vim-one in issue #84 as well.

In my experiment, I removed the conversion, hard-coded the closest 256 color code, defined a new hi wrapper for highlight definition. It turns out that this improves the startup time hugely.

At the same time, the original hi wrapper function <sid>X(group, fg, bg, attr) is kept for user customization.

vim-one loading time
Original 170.621 ms
Hard code 256 color 33.659 ms
Use hi statements directly 12.178 ms
# the original one
❯ vim-profiler.py -r 10 nvim
=====================================
Top 10 plugins slowing nvim's startup
=====================================
1       170.621   vim-one
2         6.274   vim-airline
3         4.038   vim-polyglot
4         3.496   coc.nvim
5         2.645   colorizer
6         2.413   vim-textobj-xmlattr
7         2.313   vim-textobj-comment
8         1.795   vim-sensible
9         1.664   vim-textobj-line
10        1.341   ctrlp.vim
=====================================

# hard-code the 256 color
vim-one  new-hi-wrapper
❯ vim-profiler.py -r 10 nvim
=====================================
Top 10 plugins slowing nvim's startup
=====================================
1        33.659   vim-one
2         6.735   vim-airline
3         4.161   vim-polyglot
4         3.652   coc.nvim
5         2.729   colorizer
6         2.443   vim-textobj-xmlattr
7         2.440   vim-textobj-comment
8         1.753   vim-textobj-line
9         1.749   vim-sensible
10        1.495   ctrlp.vim
=====================================

# using `hi` command directly
vim-one  master [⇡]
❯ vim-profiler.py -r 10 nvim
=====================================
Top 10 plugins slowing nvim's startup
=====================================
1        12.178   vim-one
2         6.434   vim-airline
3         3.923   vim-polyglot
4         3.519   coc.nvim
5         2.720   colorizer
6         2.542   vim-textobj-comment
7         2.391   vim-textobj-xmlattr
8         1.797   vim-textobj-line
9         1.724   vim-sensible
10        1.363   ctrlp.vim
=====================================

The final result 34 ms is acceptable, considering vim-one uses so many highlight definitions.

Credit

geoffharcourt commented 5 years ago

This is an awesome compromise between the flexible original approach and your idea in #74 that retains the ongoing maintainability of the current repo. Nice!

rakr commented 5 years ago

Thanks a lot for your contribution and sorry this has been taken into account so late, life has been pretty busy and I must admit that I did not spend much time on this repo. As there was a merge conflict with other pull request I merged the code manually.

kevinhwang91 commented 4 years ago

@laggardkernel IncSearch doesn't work properly.

Fixed by

-  hi! link IncSearch OneHue6
+  call <sid>X('IncSearch',    s:hue_6,         '',               '')
laggardkernel commented 3 years ago

@kevinhwang91 Sorry, haven't used vim for a long time. After some reading, I found the cause

Only a quick fix removing problematic hi! link. Maybe we should drop hi! link on the default groups (listed in :highlight-default).

hi clear

hi clear clears user defined highlight groups and load some default groups,

IncSearch      xxx term=reverse cterm=reverse gui=revers

hi! link

hi! link IncSearch OneHue6

check :hi IncSearch

IncSearch      xxx cterm=reverse gui=reverse 
                   links to OneHue6

The default def is ignored, only linked group is used, so the text (foreground) is OneHue6.

hi

If we use call <sid>X('IncSearch', s:hue_6, '', '')

IncSearch      xxx cterm=reverse ctermfg=94 gui=reverse guifg=#986801

Group def is merged with the default one, the background color is OneHue6.