Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vim slows down because of parenthesis (etc.) matching #163

Open
felixjung opened this issue Mar 11, 2014 · 30 comments
Open

Vim slows down because of parenthesis (etc.) matching #163

felixjung opened this issue Mar 11, 2014 · 30 comments

Comments

@felixjung
Copy link

Hi,

I usually edit latex code with lots of equation environments and therefore many open and close patterns. LaTeX-Box tries to detect and highlight these patterns, which is a really helpful feature. From the help I know that the patterns are set using the g:LatexBox_open_pats and g:LatexBox_close_pats variables.

With the default setting my vim slows down a lot when moving between lines or within a line in inline math or an equation environment using the h, j, k, l keys. When holding h or l for a longer time this may even cause Vim to become unresponsive for a number of seconds.

Setting the above variables to empty lists (i.e. '[]') fixes any performance issues, but obviously also disables the matching feature.

Is there a way to improve the performance? Maybe only check for matching opening and closing patterns once the cursor stood still for half a second or so?

Thanks!

@lervag
Copy link

lervag commented Mar 11, 2014

I also usually work with documents containing a lot of equations, and don't really experience these lags. It would be much easier to investigate this if you could define a minimal working example. That is, if you could give a simple tex file, something like this:

\documentclass{minimal}
\begin{document}
... [content that leads to you problem]
\end{document}

It would also help if you could further explain exactly how to reproduce the slowdown (keypresses and settings).

Since I'm also working on a "fork" of LaTeX-Box, I would be very happy if you could check if this problem also happens there: vim-latex. If the problem does not happen with vim-latex, then I might fix it for LaTeX-Box by copying the modified pattern matching functions back to LaTeX-Box.

@felixjung
Copy link
Author

Sorry for not sticking with bug reporting 101 and providing a working example. Below is the LaTeX file causing me problems (I include this in my main file using \input{}, but I notice the bug when opening this file by itself as well). The problem is reproducible on my end by just navigating horizontally through one of the align environments using the h, j, k, or l key or by navigating vertically through the file using the same keys (making sure you hit a lot of "matched" strings, i.e. \left[ ... \right]). The cursor will always pause once it reaches a matched string, then the opening and closing parts are highlighted, and only after the highlight is the cursor allowed to continue travel.

Also, I should note I use SPF13, but have tried to disable any of the included bundles I don't need. In particular the one that looks like it's related to matching backets etc.

I'll try vim-latex next.

Thanks!

UPDATE:
I just noticed the problem is almost not noticeable in a small window (I usually work in fullscreen). I guess it must be related to screen redrawing then?

UPDATE 2: I have the same problem with your vim-latex.

\section{Introduction}
\label{sec:introduction}
We consider the forward intensity approach of \citet{Duan:2012bh} where a company might exit the market because of default or for other reasons (i.e. a merger).
The authors model survival probabilities via a doubly stochastic Poisson process with an intensity $g_t(\tau)$.
However, this intensity is itself comprised of a default intensity $f_t(\tau)$ and an \emph{other exit} (i.e. due to a merger) intensity $h_t(\tau)$, and is therefore called \emph{combined exit} intensity.
Moreover, these intensities are \emph{forward} intensities assumed to be exponentially affine in a vector $X_t$ of state variables.
This approach alleviates the problem of having to compute the survival probability as an expectancy, which is necessary when employing spot intensities.
Overall, forward intensities for some stopping time $\tau$ in the \citeauthor{Duan:2012bh} framework can be expressed formally as
\begin{align}
  \label{eq:def_intensities_duan}
  f_t(\tau) &= \exp[\alpha_0(\tau) + \alpha_1(\tau) x_{t,1} + \alpha_2(\tau) x_{t,2} + \cdots + \alpha_K(\tau) x_{t,K}]\\
  \label{eq:oth_intensities_duan}
  h_t(\tau) &= \exp[\beta_0(\tau) + \beta_1(\tau) x_{t,1} + \beta_2(\tau) x_{t,2} + \cdots + \beta_K(\tau) x_{t,K}]\\
  \label{eq:comb_intensities_duan}
  g_t(\tau) &= f_t(\tau) + h_t(\tau),
\end{align}
where $\alpha(\tau) = (\alpha_0, \alpha_1, \cdots, \alpha_K)^\top$ and $\beta(\tau) = (\beta_0, \beta_1, \cdots, \beta_K)^\top$ are constant coefficient vectors for the specific stopping time $\tau$.
The resulting forward intensities' dynamics are driven by the covariate vector $X_t$.

Consider a prediction horizon $\tau$, measured in months $\Delta t = 1 / 12$.
Denote the time of default by $\tau_D$ and combined exit time by $\tau_C$.
Obviously, we have $\tau_C \leq \tau_D$.
In this setting the forward survival, default, and combined exit probabilities are given by
\begin{align}
  \label{eq:fwd_prob_surv}
  &P_t(\tau_C > t + \tau) = \exp\left[-\sum_{s = 0}^{\tau - 1} g_t(s) \, \Delta t\right]\\[0.5em]
  \label{eq:fwd_prob_def}
  &P_t(\tau_C; \tau_D = \tau_C \leq t + \tau) \notag\\
  &= 
    \begin{cases}
      1 - \exp\left[-f_t(0) \, \Delta t\right]  & , \quad \text{if $\tau_C = t + 1$}\\
      \!\begin{aligned}
        & \exp\left[-\sum_{s = 0}^{\tau_C - t - 2} g_t(s) \, \Delta t\right] \times \\
        & \left\{1 - \exp\left[-f_t(\tau_C - t - 1) \, \Delta t\right]\right\}
      \end{aligned}                             & , \quad \text{if $t + 1 < \tau_C \leq t + \tau$}
    \end{cases}
\end{align}
and
\begin{align}
\label{eq:fwd_prob_oth}
  &P_t(\tau_C; \tau_D \neq \tau_C \land \tau_C \leq t + \tau) \nonumber\\
  &= \begin{cases}
      \exp\left[-f_t(0) \, \Delta t\right] - \exp\left[-g_t(0) \, \Delta t\right] & , \quad \text{if $\tau_C = t + 1$}\\
      \!\begin{aligned}
        & \exp\left[-\sum_{s = 0}^{\tau_C - t - 2} g_t(s) \, \Delta t\right] \times \\
        & \{\exp\left[-f_t(\tau_C - t - 1) \, \Delta t\right]\\
        &~- \exp\left[-g_t(\tau_C - t - 1) \, \Delta t\right] \}
      \end{aligned}                             & , \quad \text{if $t + 1 < \tau_C \leq t + \tau$}
  \end{cases}
\end{align}
respectively.
As is evident from the above equations, intensities are assumed to be piecewise constant for any given month. 

\citeauthor{Duan:2012bh} assume the state vector $X_t$ to consist of firm specific variables and common factors.
For a sample of $N$ companies containing information regarding the companies' survival/default/other exit and the corresponding firm specific and common covariates of $X_t$ during a sample period of $T$ months, the authors construct the following pseudo-likelihood function for the parameter set comprised of $\alpha = \left\{\alpha(0), \alpha(1), \cdots, \alpha(\tau - 1)\right\}$ and $\beta = \left\{\beta(0), \beta(1), \cdots, \beta(\tau - 1)\right\}$, given a fixed $\tau$ for the maximum prediction horizon.
\begin{align}
\label{eq:likelihood_function_duan}
  \mathcal{L}_t(\alpha, \beta; \tau_C, \tau_D, X) = \prod_{i = 1}^N \prod_{t = 0}^{T - 1} \mathcal{L}_{\tau, i, t}(\alpha, \beta)
\end{align}
where
\begin{align*}
\label{eq:likelihood_function_duan_part}
  \mathcal{L}_{\tau, i, t}(\alpha, \beta) &= \mathbf{I}(t_{0,i} \leq t, \tau_{C,i} > t + \tau) \, P_t(\tau_{C, i} > t + \tau)\\
  &+ \mathbf{I}(t_{0,i} \leq t, \tau_{D, i} = \tau_{C,i} \leq t + \tau) \, P_t(\tau_{C,i}; \tau_{D,i} = \tau_{C,i} \leq t + \tau)\\
  &+ \mathbf{I}(t_{0,i} \leq t, \tau_{D, i} \neq \tau_{C,i}, \tau_{C,i} \leq t + \tau) \, P_t(\tau_{C,i}; \tau_{D,i} \neq \tau_{C,i} \land \tau_{C,i} \leq t + \tau)\\
  &+ \mathbf{I}(t_{0,i} > t) + \mathbf{I}(\tau_{C,i} \leq t).
\end{align*}
The above probabilities are given by \eqref{eq:fwd_prob_surv} through \eqref{eq:fwd_prob_oth}, with the intensities depending on firm $i$'s state vector.
\citeauthor{Duan:2012bh} show that \eqref{eq:likelihood_function_duan} can be split into numerous pseudo-likelihoods; one per $\alpha(\tau)$, $\beta(\tau)$, and horizon
\begin{align}
  \label{eq:likelihood_function_duan_split_alpha}
  \mathcal{L}(\alpha(s)) &= \prod_{i = 1}^N \prod_{t = 0}^{T - s - 1} \mathcal{L}_{i,t}(\alpha(s)), & s = 0, 1, \cdots, \tau - 1\\
  \label{eq:likelihood_function_duan_split_beta}
  \mathcal{L}(\beta(s)) &= \prod_{i = 1}^N \prod_{t = 0}^{T - s - 1} \mathcal{L}_{i,t}(\beta(s)), & s = 0, 1, \cdots, \tau - 1,
\end{align}
where
\begin{align*}
  \mathcal{L}_{i, t}(\alpha(s)) &= \mathbf{I}(t_{0,i} \leq t, \tau_{C,i} > t + s + 1) \, \exp\left[-f_{i,t}(s) \, \Delta t\right]\\
  &+ \mathbf{I}(t_{0,i} \leq t, \tau_{D, i} = \tau_{C,i} = t + s + 1) \, \{1 - \exp[-f_{i,t}(s) \, \Delta t]\}\\
  &+ \mathbf{i}(t_{0,i} \leq t, \tau_{d, i} \neq \tau_{c,i}, \tau_{c,i} = t + s + 1) \, \exp[-f_{i,t}(s) \, \delta t]\\
  &+ \mathbf{i}(t_{0,i} > t) + \mathbf{i}(\tau_{c,i} < t + s + 1)\\[0.5em]
  \mathcal{L}_{i, t}(\beta(s)) &= \mathbf{I}(t_{0,i} \leq t, \tau_{C,i} > t + s + 1) \, \exp\left\{-\left[g_{i,t}(s) - f_{i,t}(s)\right] \, \Delta t\right\}\\
  &+ \mathbf{I}(t_{0,i} \leq t, \tau_{D, i} = \tau_{C,i} = t + s + 1)  + \mathbf{I}(t_{0,i} \leq t, \tau_{D, i} \neq \tau_{C,i} = t + s + 1) \\
  &\times \left\{1 - \exp\left[-(g_{i,t}(s) - f_{i,t}(s)) \, \Delta t\right]\right\}\\
  &+ \mathbf{I}(t_{0,i} > t) + \mathbf{I}(\tau_{c,i} < t + s + 1).
\end{align*}

@darkfeline
Copy link

I'm also experiencing this problem. Profiling vim shows that the FindMatchingPair function is indeed the culprit.

The content which is encountering this problem, for me, is just a whole lot of text between begin and end document. I also used many fold markers ({{{, }}}) in between, however; would that be an issue?

@lervag
Copy link

lervag commented Mar 12, 2014

@feju

Strange. I saved your test file and tested in full screen (I have a full screen resolution of 2560x1440): I don't get any [noticable] delays. I tried both using vertical and horisontal movements with h, j, k, and l. Note that I tested with my own plugin, since you stated that the same problem occurs there.

Could you please test the same with a minimal vimrc file. You could use something like what I have pasted below, which is based on my own minimalvimrc file. You might prefer to remove or change some/all of the options I have used, but you need the rtp += ... to source the latex plugin. Then start vim with vim -u /path/to/minimal/vimrc [tex-file] or, if you prefer, gvim ....

set nocompatible

set rtp+=~/.vim/bundle/vim-latex " Or LaTeX-Box

filetype plugin indent on
syntax enable
if has("gui_running")
  set lines=56
  set guioptions=aeci
else
  set t_Co=256
endif
set keywordprg=":help"
set autoindent
set backspace=indent,eol,start
set ttimeout
set expandtab
set ttimeoutlen=50
set laststatus=2

@lervag
Copy link

lervag commented Mar 12, 2014

@darkfeline

Yes, I would suspect that this problem can be due to FindMatchingPair, but I suspect that there might be a vim option that you have activated that makes the function run slower. It is difficult for me to investigate until I can reproduce.

If you could also test if you experience the same problem with vim-latex, I would be happy. This is a fork that is designed to be slightly easier to debug, and so if the problem is there as well, then I can investigate it there and then apply the fix to LaTeX-Box afterwards.

I can confirm that if I add surrounding parantheses to the entire content of the test file provided by @feju, then there is a slight delay (still talking ~100 ms) when I move past these parantheses. I also tested with both vim-latex and LaTeX-Box, and there seems to be no difference: Both plugins seems to be fast for the example provided, but have a slight delay when passing delimiters such as parantheses that contains a LOT of text (~100 lines).

@felixjung
Copy link
Author

@lervag Did you do the movements by pressing the movement keys repeatedly or by pressing and holding them? I do the latter.
I've run profile for my vimrc and the one you provided above (using LaTeX-Box). As you can see, the problem persists.

I am running MacVim and Vim (both have the problem) installed from Homebrew on Mac OS X 10.9.

Profile from my own vimrc:

FUNCTIONS SORTED ON TOTAL TIME
count  total (s)   self (s)  function
  353   0.613290   0.169002  <SNR>111_FindMatchingPair()
  508   0.444169             LatexBox_InComment()

Profile from the vimrc you provided above:

FUNCTIONS SORTED ON TOTAL TIME
count  total (s)   self (s)  function
  546   0.694637   0.182908  <SNR>28_FindMatchingPair()
  770   0.511320             LatexBox_InComment()
    1   0.000409             <SNR>28_HasSyntax()

@lervag
Copy link

lervag commented Mar 12, 2014

I've tried both repeated pressing and continuous pressing. Perhaps it works well with me because I'm on a fast computer?

Could you explain how you do the profiling? Both how to start profiling (is it a vim functionality?) and exactly what you profile. Do you for instance only profile like 10 seconds of navigation?

@lervag
Copy link

lervag commented Mar 12, 2014

Just an example of what I mean when I say I am not affected.
cast

@nasenatmer
Copy link

I'm also using vim-latex and I can't confirm any lags when keeping l or j pressed. My computer isn't the freshest anymore either… @lervag how did you make this gif shot?

@lervag
Copy link

lervag commented Mar 12, 2014

@nasenatmer I used a utility called ffcast, see here or here. To create the gif, I also needed to have byzanz-record installed. Then I used sleep 3; ffcast byzanz-record -- cast.gif to capture the entire full screen. The sleep part was to be able to switch the windows so that I only captured the full screen vim session.

@samuelecioni
Copy link

I'm having the same issue too with vim on os x (installed with homebrew).

@darkfeline
Copy link

@lervag I am not seeing this problem with vim-latex. I switched from vim-latex to latex-box to avoid the keybindings, and that was when the performance problems emerged. I was using vim's built-in profiler (:h profile), and specifically, profiled about 10 seconds of navigation (hjkl), pressing and holding down. There is a tangible cursor movement lag, and when I hold down a motion key, the cursor disappears instead of smoothly moving like usual.

@felixjung
Copy link
Author

@lervag I'm on a 2011 Mac Pro at work (that's where I did the testing). There should be enough performance...
Profiling was done in vim using:

:profile start foo.log
:profile func*
:profile file*
" Do something here
:profile pause
:qall!

UPDATE: Just tested on my 2011 iMac (Core i7 3.4Ghz). Same issue.

@lervag
Copy link

lervag commented Mar 13, 2014

What version of vim are you all using? I am using vim 7.4.135, so perhaps I need to try an older version? If I understand correctly, the issue is confirmed by @darkfeline @feju and @Stelgard, where at least two of you use Mac.

I can confirm that the cursor disappears when I hold a motion key, but I can still not reproduce the delays. That is, I can not confirm any substantial delays.

Btw, @darkfeline, could you explain what you mean with "to avoid the keybindings"? In vim-latex you can turn off all keybindings through g:latex_mappings_enabled, and then define the keybindings yourself.

Update: Here is timing from me.

FUNCTIONS SORTED ON TOTAL TIME
count  total (s)   self (s)  function
  300   0.232271   0.060135  <SNR>154_FindMatchingPair()
  368   0.172053             LatexBox_InComment()

Update 2: I tested with a minimal vimrc, and I found that the cursor no longer disappear. This indicates that the disappearance of the cursor is not because of LaTeX-Box (or vim-latex). Here is a timing from vim-latex as well:

FUNCTIONS SORTED ON TOTAL TIME
count  total (s)   self (s)  function
  328   0.296947   0.080593  <SNR>26_highlight_matching_pair()
  488   0.216354             latex#util#in_comment()

Update 3: Here's a new gif showing how it looks for me with a minimal vimrc file, fullscreen, with vim-latex.
cast

@nasenatmer
Copy link

FWIW, heres my profile output though I don't quite understand it:

FUNCTIONS SORTED ON TOTAL TIME
count  total (s)   self (s)  function
  347   0.584649   0.164885  <SNR>45_highlight_matching_pair()
  396   0.460601   0.016977  airline#extensions#branch#get_head()
  396   0.443624   0.040567  airline#extensions#branch#head()
  426   0.419136             latex#util#in_comment()
  396   0.396545   0.034635  fugitive#detect()
  396   0.361910   0.260288  fugitive#extract_git_dir()
 1584   0.062293   0.037774  fugitive#is_git_dir()
  396   0.058396             airline#check_mode()

Needless to say, I'm not using LaTeX-Box, but vim-latex.

@lervag
Copy link

lervag commented Mar 13, 2014

This is sort of strange, because based on the profiling results posted here, it seems that we get relatively similar results. That is, the result from @nasenatmer is very similar to that of @feju (and me).

The result implies that it takes vim 0.58 seconds to run highlight_matching_pair 347 times (although much of the time is spent in the in_comment() function). To me, this seems to imply that a single call to the function is very fast. That is, it seems to me that the profiling results presented here do not really reveal that there is an issue here.

@feju Could you try to create a screencast (see my above comment for a tip on how to do this) where you could show me what is really happening?

@darkfeline
Copy link

@lervag I meant expansions like for {{ and [[. I think I read somewhere that even using that option doesn't work. But in any case I only really used vim-latex for syntax highlighting, and I wouldn't have stumbled on this bug if I didn't try out latex-box, would I?

I'm using the default vim for Arch (here's all the version and compile option dump).

I can confirm that the cursor lag and disappearing is caused by swapping out vim-latex for latex-box.

Profile log for latex-box

FUNCTIONS SORTED ON TOTAL TIME
count  total (s)   self (s)  function
  647   0.710861   0.157660  <SNR>138_FindMatchingPair()
  668   0.553201             LatexBox_InComment()
  631   0.209019   0.014876  airline#extensions#branch#get_head()
  631   0.194143   0.026437  airline#extensions#branch#head()
    1   0.179105   0.000094  <SNR>113_finalize()
    1   0.178900   0.000289  <SNR>113_vp_dlclose()
    1   0.178611   0.149217  <SNR>113_libcall()
  631   0.116868   0.009598  fugitive#head()
  631   0.086186   0.021748  <SNR>65_repo_head()
  631   0.051391   0.006777  airline#extensions#syntastic#get_warnings()
  631   0.050838             <SNR>154_check_in_path()
  631   0.047203             airline#check_mode()
  631   0.044614   0.004905  SyntasticStatuslineFlag()
  631   0.039130   0.034814  <SNR>65_repo_head_ref()
 1262   0.036890             <SNR>65_repo()
  631   0.034305             45()
    1   0.029394             <SNR>113_split()
  631   0.024486             airline#extensions#whitespace#check()
 4417   0.022697             airline#util#wrap()
    1   0.016161   0.016018  <SNR>46_QuitPreHook()

FUNCTIONS SORTED ON SELF TIME
count  total (s)   self (s)  function
  668              0.553201  LatexBox_InComment()
  647   0.710861   0.157660  <SNR>138_FindMatchingPair()
    1   0.178611   0.149217  <SNR>113_libcall()
  631              0.050838  <SNR>154_check_in_path()
  631              0.047203  airline#check_mode()
 1262              0.036890  <SNR>65_repo()
  631   0.039130   0.034814  <SNR>65_repo_head_ref()
  631              0.034305  45()
    1              0.029394  <SNR>113_split()
  631   0.194143   0.026437  airline#extensions#branch#head()
  631              0.024486  airline#extensions#whitespace#check()
 4417              0.022697  airline#util#wrap()
  631   0.086186   0.021748  <SNR>65_repo_head()
    1   0.016161   0.016018  <SNR>46_QuitPreHook()
  631              0.015235  airline#parts#ffenc()
  631   0.209019   0.014876  airline#extensions#branch#get_head()
 1262              0.014556  airline#util#append()
  631              0.012850  airline#statusline()
  631   0.116868   0.009598  fugitive#head()
  631              0.009502  <SNR>65_sub()

Same config, without latex-box

FUNCTIONS SORTED ON TOTAL TIME
count  total (s)   self (s)  function
 1125   0.369848   0.025588  airline#extensions#branch#get_head()
 1125   0.344260   0.046797  airline#extensions#branch#head()
 1125   0.206314   0.017249  fugitive#head()
 1125   0.153714   0.038210  <SNR>65_repo_head()
 1119   0.135490             <SNR>76_Highlight_Matching_Pair()
 1125   0.092283   0.012118  airline#extensions#syntastic#get_warnings()
 1125   0.091149             <SNR>144_check_in_path()
 1125   0.082619             airline#check_mode()
 1125   0.080165   0.009016  SyntasticStatuslineFlag()
 1125   0.070058   0.062470  <SNR>65_repo_head_ref()
 2250   0.063365             <SNR>65_repo()
 1125   0.061690             45()
 1125   0.043475             airline#extensions#whitespace#check()
 7875   0.040323             airline#util#wrap()
 1125   0.026854             airline#parts#ffenc()
 2250   0.025930             airline#util#append()
 1125   0.023139             airline#statusline()
 1125   0.017432             <SNR>65_sub()
 1119   0.011654             <SNR>140_sync_active_winnr()
 1125   0.009459             40()

FUNCTIONS SORTED ON SELF TIME
count  total (s)   self (s)  function
 1119              0.135490  <SNR>76_Highlight_Matching_Pair()
 1125              0.091149  <SNR>144_check_in_path()
 1125              0.082619  airline#check_mode()
 2250              0.063365  <SNR>65_repo()
 1125   0.070058   0.062470  <SNR>65_repo_head_ref()
 1125              0.061690  45()
 1125   0.344260   0.046797  airline#extensions#branch#head()
 1125              0.043475  airline#extensions#whitespace#check()
 7875              0.040323  airline#util#wrap()
 1125   0.153714   0.038210  <SNR>65_repo_head()
 1125              0.026854  airline#parts#ffenc()
 2250              0.025930  airline#util#append()
 1125   0.369848   0.025588  airline#extensions#branch#get_head()
 1125              0.023139  airline#statusline()
 1125              0.017432  <SNR>65_sub()
 1125   0.206314   0.017249  fugitive#head()
 1125   0.092283   0.012118  airline#extensions#syntastic#get_warnings()
 1119              0.011654  <SNR>140_sync_active_winnr()
 1125              0.009459  40()
 1125   0.080165   0.009016  SyntasticStatuslineFlag()

@achilleas-k
Copy link

NOTE: Just as I was about to hit the comment button, I ran a few more tests to narrow down the cause. First, I overwrote the offending function by inserting

function! s:Highlight_Matching_Pair()
endfunction

in ~/.vim/ftplugin/latex.vim (I later discovered that :NoMatchParen has the same effect). This took care of most of the lag, but there was still some jumping around of the cursor. Disabling relativenumber and cursorline resolved the issue completely (set norelativenumber, set nocursorline).
I'm curious as to why Highlight_Matching_Pair() causes this much lag in LaTeX specifically. On the positive side, even after disabling it, matching symbols ([:], {:}, (:), etc) are still highlighted and % works for jumping to corresponding symbols. It would seem that LaTeX-Box handles matching in a much more efficient way and the built-in matching has some issue with symbol pair matching in LaTeX documents, which are heavy in this type of formatting.


Original comment follows


I've been trying to hunt down the cause of this slowdown myself. I get heavy cursor lag when editing large LaTeX files. As another commenter mentioned, this is not noticeable or at least less pronounced when the editor window isn't maximised.

The latency is certainly there, though. If I hold a movement key long enough, then let go, there are occasions where the cursor will keep scrolling for a few (2-3) seconds before it stops.

I have had a very hard time pinpointing the exact cause. This only happens when editing LaTeX files and it didn't seem to occur when I was using vim-latex. However, (this is where it gets interesting) if I remove LaTeX-Box and my personal LaTeX ftplugin file, the problem persists. I am forced to conclude that this is not a LaTeX-Box issue, but something else that is very specific to LaTeX. The fact that the cursor seems to "jump around" when scrolling points to something that requires drawing in other parts of the file. For example, I recall that having the cursorcolumn option enabled, would cause similar behaviour in most files. Perhaps it is related to parenthesis/bracket matching and similar behaviour.

Regardless, the fact that this issue persists when LaTeX-Box is removed, means that this issue shouldn't be here. I assume I am having the same issue as the other commenters --- I get similar results as the others mentioned when I run profiling, i.e., Highlight_Matching_Pair() jumps up to the top of the list.

@lervag
Copy link

lervag commented May 5, 2014

Hi, @achilleas-k. Thanks for your comments, they are interesting! I still do not see any good way to fix this globally from within LaTeX-Box, but I think your comment might help some people to resolve the issue locally.

@stanislas-brossette
Copy link

I ran into the same problem but with using vimtex with neovim.
For me, setting the option let g:vimtex_motion_matchparen = 0 fixed it. No more lag.

I profiled in neovim with the 2 different setups.
During the profiling, all I did was going from the top to the bottom of a file that contains a lot of equations with j.
And the difference is clear
Here is the profiling output with let g:vimtex_motion_matchparen = 1
profileJ.txt

And here is the profiling output with let g:vimtex_motion_matchparen = 0
profileJNoMotionMatchParen.txt

I paste the summary here for clarity:
with let g:vimtex_motion_matchparen = 1

FUNCTIONS SORTED ON TOTAL TIME
count  total (s)   self (s)  function
 1142   2.201501   0.074201  <SNR>91_highlight_matching_pair()
 1580   1.224766   0.650188  <SNR>82_get_delim()
 1142   0.927146   0.019770  vimtex#util#in_comment()
 1145   0.913424             vimtex#util#in_syntax()
 1133   0.846561   0.017493  vimtex#delim#get_current()
  138   0.567404   0.005865  vimtex#cmd#get_at()
  138   0.561539   0.008019  vimtex#cmd#get_current()
  138   0.553519   0.026014  <SNR>79_get_cmd()
  447   0.512244   0.038404  <SNR>79_get_cmd_part()
  447   0.401353   0.005655  vimtex#delim#get_next()
 1312   0.394973   0.069011  vimtex#delim#get_matching()
 1142   0.318419   0.030762  neomake#CursorMoved()
  138   0.298860   0.020624  <SNR>82_parser_env()
  450   0.245348   0.097678  <SNR>82_parser_delim()
 1142   0.154485             <SNR>40_Highlight_Matching_Pair()
 1142   0.145271             neomake#signs#PlaceVisibleSigns()
 1142   0.142385             neomake#EchoCurrentError()
  453   0.088049             <SNR>82_parser_delim_get_corr()
  906   0.059621             <SNR>82_parser_delim_get_regexp()
  634   0.039183             <SNR>79_text_between()

with let g:vimtex_motion_matchparen = 0

FUNCTIONS SORTED ON TOTAL TIME
count  total (s)   self (s)  function
 1140   0.339115   0.035153  neomake#CursorMoved()
 1140   0.163684             <SNR>40_Highlight_Matching_Pair()
 1140   0.158171             neomake#signs#PlaceVisibleSigns()
 1140   0.145791             neomake#EchoCurrentError()

@junhyeokahn
Copy link

Hi,
I read through this.
Is it still unsolved problem?
I would like to keep cursor line and match parenthesis
Is there anyone who resolve this?

@lervag
Copy link

lervag commented May 20, 2017

IMHO, LaTeX-Box is deprecated. I recommend you try vimtex.

@junhyeokahn
Copy link

@lervag Thank you for reply.
I would definitely like to use Vimtex but the problem was my vim (I downloaded it from home-brew) does not support client server feature.
So when I installed it, it caused some compile error at .vimrc
Do you have any recommendation on it?

@lervag
Copy link

lervag commented May 20, 2017

If you don't have clientserver, then you can use an option to disable the feature that requires it. For instance, this:

let g:vimtex_compiler_latexmk = {'callback' : 0}

@junhyeokahn
Copy link

@lervag Thank you so much.
I moved to Vimtex and it becomes much better.
But when I go to equations which include lot's of parenthesis it has little delay.
Is it natural to be so?
I feel like much better though.
Thank you!

@lervag
Copy link

lervag commented May 20, 2017

No problem. I'm sorry, but it is very difficult for me to say whether it is natural or not with the delay based on such a vague description. If you feel it is a problem, don't hesitate to open a new issue on the vimtex repo. If so, please adhere to the issue template and provide a reproducible and minimal example.

@goudan-er
Copy link

I got same problem and my vim profiling output was the same as @felixjung . So it is probably cause by sync settings. When I removed syntax on or set foldmethod=syntax in .vimrc, the lag was gone. I hope this can help and you can check http://vim.wikia.com/wiki/Fix_syntax_highlighting for more.

@arjunkc
Copy link

arjunkc commented Sep 26, 2017

I had the same problem as everyone, and here is what worked for me. I tried migrating to vimtex, but that brought along a bunch of bugs: I could not for example hit enter in the jump list to go to the errors I was used to seeing in latex-box. Perhaps these can all be fixed, but I chose to fix the small annoyance in latex-box by disabling the feature and moving on with my life.

After inspecting the code in LaTeX-Box/ftplugin/latex-box/motion.vim you find a function that says that you can disable the FindMatchingParen() functionality by setting the variable:

let g:LatexBox_loaded_matchparen=1

The variable let g:loaded_matchparen=1 disables the code in latexbox that runs vims internal matchparen. This does not appear to affect anything significantly though, so I've chosen not to set it.

@madnight
Copy link

madnight commented Oct 8, 2017

Same problem over here:

FUNCTIONS SORTED ON SELF TIME
count  total (s)   self (s)  function
  551   3.568197   3.546848  <SNR>104_FindMatchingPair()
43979              0.969997  ale#util#LocItemCompare()
  104   0.574618   0.487038  <SNR>159_InvokeCompletion()
 3870              0.345074  ale#sign#GetSignName()
   32   0.332203   0.322068  ale#engine#FixLocList()
   42              0.252454  ale#util#Writefile()
   32   0.319261   0.223209  ale#highlight#UpdateHighlights()
  369              0.208523  ale#util#BinarySearch()
  566              0.205637  <SNR>159_Pyeval()
   32              0.194491  ale#sign#ParseSigns()
   42   0.198781   0.190825  ale#job#Start()
   32   0.532885   0.187811  <SNR>153_BuildSignMap()
  394              0.180917  <SNR>217_Move()
 3486              0.175307  <SNR>129_get_syn()
  117              0.171076  <SNR>160_wordcount()
 1253   0.563912   0.167230  airline#highlighter#exec()
  814   0.691991   0.161052  airline#check_mode()
   32   0.161177   0.160934  ale#statusline#Update()
 1743   0.358051   0.155127  airline#highlighter#get_highlight()
   32              0.152405  ale#util#GetMatches()

@lervag
Copy link

lervag commented Oct 8, 2017

Just mentioning: I don't think anyone is maintaining this repo. You should consider trying vimtex.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests