ap / vim-css-color

Preview colours in source code while editing
http://www.vim.org/scripts/script.php?script_id=5056
MIT License
1.77k stars 78 forks source link

Slow performance with cursorline on long lines #112

Closed bryclee closed 5 years ago

bryclee commented 5 years ago

Sometimes I'll open a file with long lines (for example, a minified file) and find that vim basically freezes. My profiling shows the following:

count  total (s)   self (s)
    8   0.000103   0.000044     call s:clear_matches()
    8              0.000025     if ! &l:cursorline | return | endif
                                " adds matches based that duplicate the highlighted colors on the current line
    8              0.000018     let lnr = line('.')
    8              0.000008     let group = ''
    8              0.000009     let groupstart = 0
    8              0.000100     let endcol = col('$')
2069130              1.092930   for col in range( 1, endcol )
2069123             17.750037       let nextgroup = col < endcol ? synIDattr( synID( lnr, col, 1 ), 'name' ) : ''
4137701              2.761382       if group == nextgroup | continue | endif
  544              0.001379         if group =~ '^BG\x\{6}$'
                                        let regex = '\%'.lnr.'l\%'.groupstart.'c'.repeat( '.', col - groupstart )
                                        let w:css_color_match_id += [ matchadd( group, regex, -1 ) ]
                                    endif
  544              0.000469         let group = nextgroup
  544              0.000375         let groupstart = col
  544              0.000253     endfor

Which points to this line being slow https://github.com/ap/vim-css-color/blob/2411b84298eb6db034001f35ce7cc32c36f9b43b/autoload/css_color.vim#L196

I read through the comments of https://github.com/ap/vim-css-color/issues/90 and saw that this is necessary behavior to highlight matches on the current if cursorline is set with a background. But I'm wondering if there's any way to improve this.

Some thoughts on how to improve this:

bryclee commented 5 years ago

Just to note, I'd be happy to submit a PR to add a synmaxcol or nvim check, if that makes sense. Thanks!

ap commented 5 years ago

Thanks for alerting me to the change in NeoVim. Disabling the cursorline hack when superfluous is a no-brainer. Turns out this was introduced in 0.3.1 (with a cleverly simple modification: neovim/neovim@12481781a0215c2ba0ab250d9cfdb9a082ab495a ā€“ Iā€™m impressed) so the check needs to be conditional on that.

Of course checking synmaxcol is a no-brainer as well.

ap commented 5 years ago

Iā€™m going to add the NeoVim check as well, but in the meantime, can you take a look at how much 6aa147b604c57979cdbc6b80b3d0613b47941f40 helps?

bryclee commented 5 years ago

šŸ‘ that helps a lot. Thanks!

ap commented 5 years ago

And that should help even more. šŸ™‚

Share and enjoy!