lervag / vimtex

VimTeX: A modern Vim and neovim filetype plugin for LaTeX files.
MIT License
5.53k stars 391 forks source link

Constant (low) CPU Usage even when out of focus #1154

Closed tmainiero closed 6 years ago

tmainiero commented 6 years ago

Thanks for the wonderful plugin and all the hard work you continue to put into it.

I'm experiencing constant low-level CPU usage on .tex files. The usage is the same for large .tex files as well as empty ones and is persists when the (n)vim instance is out of focus. It is exacerbated with certain status-line plugins (especially nvim in combination with airline)---although this might be a separate but related issue.

My amateur diagnosis based off of some log files (see below) makes me suspect it is related to checking for parenthesis matching (calls to vimtex#matchparen#popup_check() ).

Apologies ahead of time if this is not really an issue, but some stupidity/unhealthy obsession on my part.


Minimal .vimrc:

set nocompatible

filetype plugin on

filetype plugin indent on           

set laststatus=2            

"vimtex (using my personal directories)
let &rtp  = '~/.vim/pack/my_plugins/automanage/vimtex,' . &rtp
let &rtp .= ',~/.vim/pack/my_plugins/automanage/vimtex/after'

"Statuslines---uncomment one for relevant issue
"let &rtp  = '~/.vim/pack/my_plugins/automanage/lightline.vim,' . &rtp
"let &rtp  = '~/.vim/pack/my_plugins/automanage/vim-airline,' . &rtp

"Default to latex even on empty .tex files
let g:tex_flavor='latex'        

Other : vim version: vim 8 ; behavior is independent of the terminal emulator (or use of gvim); running Ubuntu 14.04.

Observed Behavior: I'll give an idea of what I see on my system: using with the minimal .vimrc below (uncommenting zero or one of the statusline plugins), I reliably get the following constant background CPU usages when opening an empty .tex file; the usage is present regardless of whether the (n)vim instance is in focus or defocused/minimized.

vim:

nvim:

Any non-.tex file: No measurable background CPU usage for either vim or nvim.

Expected Behavior: No measurable (or only occasional) CPU usage when window is defocused and no interaction with (n)vim is occurring.

Yes I'm a crazy person and those numbers are low, but (among other reasons) I don't like it because I work with many .tex files constantly open in the background. (My laptop has an old battery and I always find myself squeezing out every drop of power.) When running nvim with a bunch of bells-and-whistles plugins called for other file types I don't see CPU usage when nvim is not in focus.


Some log files: I'm a vim newbie, but I attempted to profile vim to figure out what's causing this.

Profile log files: profile_nvim_noplug.log profile_vim_noplug.log profile_nvim_airline.log profile_vim_airline.log

As the filenames suggest, these were produced using vim or nvim and either no statusline plugins, or with airline.

To get these log files I used the following procedure:

Open nvim (or vim) on an empty .tex file using the above .vimrc :profile start profile.log :profile func * defocus (n)vim and wait ~9-11 seconds :profile pause :q!

The usage with no plugins seems to be due to the calling of vimtex#matchparen#popup_check() . (both vim and nvim call this ~300 times in the ~10 second period). With airline this function is still being called about the same number of times, but airline seems to be calling a lot more things in nvim.

andymass commented 6 years ago

You should mention which version of neovim you are using. It might not be worth trying to fix this on previous versions of neovim (0.3.0 is current). Also, there are a few very similar issues on the neovim tracker tracker (even down to mentioning airline).

tmainiero commented 6 years ago

Ah thanks. I'm using neovim 0.3.0 (nvim --version prints out "0.3.0-dev"). I'll also look for similar neovim specific issues. I switched to lightline to cut down on the CPU usage, but there's still something (albeit much more minor) happening.

andymass commented 6 years ago

Okay, just checking.. there must be some lingering neovim issues with timers.

@lervag, maybe TextChangedP would work instead of the timer, when supported? This could also work around #1143.

lervag commented 6 years ago

Yes, this is most likely due to the popup check that you found. I'll look into @andymass's suggestions.

@tmainiero See #493 for the background of implementing these popup checks.

lervag commented 6 years ago

Ok, it seems TextChangedP might fix the problem. Please update and try now. Note, this only works if you have a recent version if Vim (8.0.1494) or neovim.

tmainiero commented 6 years ago

Beautiful! Indeed, the issues I described are gone. Thank you both. The plugin is great and the speedy maintenance is a definite bonus.

lervag commented 6 years ago

Thanks, I appreciate the kind feedback! :)