NeoVim 光标移动卡顿原因分析

2019-07-15  本文已影响0人  zzdts

    最近打开一个html文件时JK移动光标直接卡死或者1秒后才有反应处理过程如下。
ENV: macOS Mojave: 10.14.5; NeoVim: v0.3.1 , 使用SpaceVim配置。

  1. 升级NeoVim 和SpaceVim到最新版本,Github上下载最新0.3.8 编译好的macos版本。

  2. VIM 在Normal模式下每次移动光标都会触发自动命令:CursorMoved。通过开启:set verbose=9追踪发现:

Executing CursorMoved Autocommands for "*"
autocommand call s:matchparen.highlight_deferred()
Press ENTER or type command to continue

每次JK移动都会执行一个call s:matchparen.highlight_deferred()函数,搜索确定函数是在vim-matchup 插件中定义的,这个插件主要是语法匹配 并且会高亮匹配符号比如{} [] () 。

  1. 测试语法匹配时间:打开html文件输入 vim命令:
    :syntime on
    移动光标 再输入:
    :syntime off , :syntime report
    查看 report:
  TOTAL      COUNT  MATCH   SLOWEST     AVERAGE   NAME               PATTERN
  0.644858   308    254     0.004609    0.002094  javaScriptLineComment \/\/.*
  0.075428   500    418     0.004837    0.000151  htmlTagError       [^>]<
  0.062363   204    0       0.001224    0.000306  htmlCssStyleComment \(<!--\|-->\)
  0.049274   31086  31080   0.000265    0.000002  javaScriptParens   [()]
  0.047984   264    4       0.000533    0.000182  htmlArg            \<\(http-equiv\|href\|title\)=
  0.045648   260    0       0.000664    0.000176  htmlArg            \<\(accept-charset\|label\)\>
  0.044403   200    0       0.000465    0.000222  javaScriptRegexpString [,(=+]\s*/[^/*]
  0.035824   3887   3887    0.000407    0.000009  javaScriptNumber   -\=\<\d\+L\=\>\|0[xX][0-9a-fA-F]\+\>
  0.028142   10514  10454   0.002904    0.000003  javaScriptBraces   [{}\[\]]
  0.017400   288    224     0.000470    0.000060  htmlTagName        \<[a-z][-.0-9_a-z]*-[-.0-9_a-z]*\>
  0.016333   288    224     0.000355    0.000057  htmlTagName        [.0-9_a-z]\@<=-[-.0-9_a-z]*\>
  0.007602   310    228     0.000142    0.000025  htmlCssDefinition  style="
  ...
  ...
  1.329802   93025

可以看到 javaScriptLineComment 居然花了640多ms时间,操作完成花费 1.329802 Sec,Why?
文件是html文件 为什么会是javascript sync异常呢?
再去看看html中的js代码,我去 一行代码居然没玩没了了,使用vim命令移动光标到js代码行末,这一行js居然有32001列,也就是32001个字符。

导致Nvim卡顿的原因确认,一行js代码太长导致的,格式化后测试JK移动正常。

Vim debug 还可以使用 profile:

:profile start profile.log
:profile func *
:profile file *
" At this point do slow actions
:profile pause
:noautocmd qall!
上一篇下一篇

猜你喜欢

热点阅读