andymass / vim-matchup

vim match-up: even better % :facepunch: navigate and highlight matching words :facepunch: modern matchit and matchparen. Supports both vim and neovim + tree-sitter.
https://www.vim.org/scripts/script.php?script_id=5624
MIT License
1.7k stars 73 forks source link

High CPU usage in macvim when using ignorecase and hlsearch #33

Closed idoa01 closed 6 years ago

idoa01 commented 6 years ago

Explain the issue

when using vim-matchup and ignorecase and hlsearch set in the .vimrc (both must be set), I see high cpu usage (up to 100% cpu) when editing a ruby file (see attached example file) (might also happen in other formats, didn't check, but didn't happen in a small vimscript file)

  1. vim version:

    vim --version
    VIM - Vi IMproved 8.1 (2018 May 17, compiled May 31 2018 18:29:18)
    macOS version
    Included patches: 1-22
    Compiled by Homebrew
    Huge version with MacVim GUI.  Features included (+) or not (-):
    +acl               +farsi             +mouse_netterm     +tag_binary
    +arabic            +file_in_path      +mouse_sgr         +tag_old_static
    +autocmd           +find_in_path      -mouse_sysmouse    -tag_any_white
    -autoservername    +float             +mouse_urxvt       +tcl
    +balloon_eval      +folding           +mouse_xterm       +termguicolors
    +balloon_eval_term -footer            +multi_byte        +terminal
    +browse            +fork()            +multi_lang        +terminfo
    ++builtin_terms    +fullscreen        -mzscheme          +termresponse
    +byte_offset       -gettext           +netbeans_intg     +textobjects
    +channel           -hangul_input      +num64             +timers
    +cindent           +iconv             +odbeditor         +title
    +clientserver      +insert_expand     +packages          +toolbar
    +clipboard         +job               +path_extra        +transparency
    +cmdline_compl     +jumplist          +perl              +user_commands
    +cmdline_hist      +keymap            +persistent_undo   +vertsplit
    +cmdline_info      +lambda            +postscript        +virtualedit
    +comments          +langmap           +printer           +visual
    +conceal           +libcall           +profile           +visualextra
    +cryptv            +linebreak         -python            +viminfo
    +cscope            +lispindent        +python3           +vreplace
    +cursorbind        +listcmds          +quickfix          +wildignore
    +cursorshape       +localmap          +reltime           +wildmenu
    +dialog_con_gui    -lua               +rightleft         +windows
    +diff              +menu              +ruby              +writebackup
    +digraphs          +mksession         +scrollbind        -X11
    +dnd               +modify_fname      +signs             -xfontset
    -ebcdic            +mouse             +smartindent       +xim
    +emacs_tags        +mouseshape        +startuptime       -xpm
    +eval              +mouse_dec         +statusline        -xsmp
    +ex_extra          -mouse_gpm         -sun_workshop      -xterm_clipboard
    +extra_search      -mouse_jsbterm     +syntax            -xterm_save
    system vimrc file: "$VIM/vimrc"
     user vimrc file: "$HOME/.vimrc"
    2nd user vimrc file: "~/.vim/vimrc"
      user exrc file: "$HOME/.exrc"
    system gvimrc file: "$VIM/gvimrc"
    user gvimrc file: "$HOME/.gvimrc"
    2nd user gvimrc file: "~/.vim/gvimrc"
       defaults file: "$VIMRUNTIME/defaults.vim"
    system menu file: "$VIMRUNTIME/menu.vim"
    fall-back for $VIM: "/Applications/MacVim.app/Contents/Resources/vim"
    Compilation: clang -c -I. -Iproto -DHAVE_CONFIG_H -DFEAT_GUI_MACVIM -Wall -Wno-unknown-pragmas -pipe  -DMACOS_X -DMACOS_X_DARWIN  -g -O2 -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=1       
    Linking: clang   -L.             -L /BuildRoot/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.Internal.sdk/usr/local/libressl/lib -L/BuildRoot/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.Internal.sdk/usr/local/lib -L.             -L /BuildRoot/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.Internal.sdk/usr/local/libressl/lib -L/BuildRoot/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.Internal.sdk/usr/local/lib  -L/usr/local/lib -o Vim -framework Cocoa -framework Carbon       -lm  -lncurses -liconv -framework AppKit   -fstack-protector  -L/System/Library/Perl/5.18/darwin-thread-multi-2level/CORE -lperl  -L/usr/local/opt/python/Frameworks/Python.framework/Versions/3.6/lib/python3.6/config-3.6m-darwin -lpython3.6m -framework CoreFoundation -F/System/Library/Frameworks -framework Tcl -framework CoreFoundation -framework Ruby 
  2. steps to reproduce. using the attached minimal .vimrc and the attached test.rb, use keyboard to run up and down the file and see the CPU skyrocket.

  3. Expected behavior smooth cursor behavior and low CPU usage.

  4. Observed behavior choppy cursor, high CPU

Minimal working example

(Tried to cut more of the code, but it seems that the length of the code is a factor in the CPU usage)

require 'yaml'
require 'aws-sdk'

class Time
  COMMON_YEAR_DAYS_IN_MONTH = [nil, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31].freeze

  class << self
    def days_in_month(month, year)
      if month == 2 && ::Date.gregorian_leap?(year)
        29
      else
        COMMON_YEAR_DAYS_IN_MONTH[month]
      end
    end
  end
end

module LoremIpsum 
  class Lorem < Ipsum 
    def test 
      lorem_ipsum = Aws::Credentials.new(@config[:lorem_ipsum][:dolor_sit], @config[:amet][:consectetur])
      @adipiscing = @config[:elit][:sed]
      @do = Aws::S3::Client.new(:region => @config[:eiusmod][:tempor], :credentials => lorem_ipsum)
      @incididunt = Aws::S3::Bucket.new(@adipiscing , client: @do)
      @ut = @config[:labore][:et]
      @dolore = /#{@config[:magna]}/
      @aliqua = /#{@config[:ut]}/
      @enim = @config[:ad] || false
      @minim = @config[:veniam].nil? ? :quis: @config[:nostrud].to_sym
      @exercitation = @config[:ullamco]
      @laboris = (@config[:nisi] || 0) * 60
      @ut = @config[:aliquip] || false
      @ex = @config[:ea] || false

      dolor = {:region =>            @config[:ex][:ea],
               :access_key_id =>     @config[:commodo][:consequat],
               :secret_access_key => @config[:duis][:aute]}
      @irure = Aws::S3::Client.new(dolor)

      @reprehenderit = @config[:in][:voluptate]
      @velit = Aws::S3::Bucket.new(@esse, client: @cillum)
      @dolore = @config[:eu][:fugiat]
      @nulla = @config[:pariatur]

      @excepteur = @config[:velit][:esse]
      @cillum = Aws::S3::Bucket.new(@dolore, client: @eu)
      @fugiat = @config[:nulla][:pariatur]

      if (@config[:excepteur])
        sint(@config[:occaecat])
      elsif (@config[:cupidatat])
        non(@config[:proident])
      else
        throw "sunt"
      end

      @in = Aws::EMR::Client.new(culpa)

      @qui = @config[:officia][:deserunt]

      @mollit = @config[:anim]

      @id = @config[:est][:laborum]
    end
  end
end

LoremIpsum::Lorem.test if __FILE__ == $0

Minimal vimrc file

Please provide a minimal vimrc file that reproduces the issue. The following should often suffice:

set nocompatible

" load match-up
let &rtp  = '~/.vim/bundle/vim-matchup,' . &rtp
let &rtp .= ',~/.vim/bundle/vim-matchup/after'

" load other plugins, if necessary
" let &rtp = '~/path/to/other/plugin,' . &rtp

set ignorecase " Case insensitive search
set hlsearch   " Highlight search results

filetype plugin indent on
syntax enable

" match-up options go here
andymass commented 6 years ago

Thanks, you are right; there does seem to be a large difference in CPU usage between ignorecase and noignorecase. Apparently, when ignorecase is toggled (internal to the plugin) vim redraws the screen, which happens to be slower for large ruby files.

I think I can fix this.. but in any case, have you tried the deferred matching?

let g:matchup_matchparen_deferred = 1
" let g:matchup_matchparen_deferred_show_delay = 50    {may configure milliseconds}
idoa01 commented 6 years ago

tried it now, display is still choppy and CPU is high

andymass commented 6 years ago

Could you try the most recent version? For me this brings the CPU down a lot.

idoa01 commented 6 years ago

looks a lot better, thanks!

andymass commented 6 years ago

excellent, closing then.