jeffreytse / zsh-vi-mode

💻 A better and friendly vi(vim) mode plugin for ZSH.
MIT License
3.29k stars 112 forks source link

Terminal slows down after enabling this plugin #96

Open sahinakkaya opened 3 years ago

sahinakkaya commented 3 years ago

General infomation

Terminal: Konsole OS: Ubuntu 20.04 ZSH framework: oh-my-zsh ZSH version 5.8 ZVM version 0.8.3

Basic examination

Problem description

Enabling this plugin makes prompt 2.5 times slow. Am I doing something wrong?

Without any plugins

********************************************************************
                      Prompt Benchmark Results
********************************************************************
Warmup duration      4s
Benchmark duration   2.011s
Benchmarked prompts  112
Time per prompt      17.95ms  <-- prompt latency (lower is better)
********************************************************************

Nearly 15 plugins

********************************************************************
                      Prompt Benchmark Results
********************************************************************
Warmup duration      4s
Benchmark duration   2.001s
Benchmarked prompts  101
Time per prompt      19.81ms  <-- prompt latency (lower is better)
********************************************************************

15 other plugins and zsh-vi-mode

********************************************************************
                      Prompt Benchmark Results
********************************************************************
Warmup duration      4s
Benchmark duration   2.044s
Benchmarked prompts  41
Time per prompt      49.84ms  <-- prompt latency (lower is better)
********************************************************************

Here is all my settings related with this plugin:

ZVM_LAZY_KEYBINDINGS=false
ZVM_VI_INSERT_ESCAPE_BINDKEY=kj
plugins=(... zsh-vi-mode)
source $ZSH/oh-my-zsh.sh

# https://github.com/jeffreytse/zsh-vi-mode#execute-extra-commands
function my_bindings() {
    # easy bindings instead of ctrl + arrow keys
    bindkey '^j' backward-word
    bindkey '^f' forward-word
    bindkey '^g' autosuggest-accept
    bindkey "\e\e" sudo-command-line
}

zvm_after_init_commands+=('[ -f ~/.fzf.zsh ] && source ~/.fzf.zsh')
zvm_after_init_commands+=(my_bindings)
# Always starting with insert mode for each command line
ZVM_LINE_INIT_MODE=$ZVM_MODE_INSERT
ZVM_INSERT_MODE_CURSOR=$ZVM_CURSOR_BLOCK

Reproduction steps

  1. Install and enable zsh-prompt-bencmark
  2. Benchmark with zsh-vi-mode enabled
  3. Benchmark with zsh-vi-mode not enabled
  4. Compare the results and see this plugin takes too much time

Expected behavior

I expect this plugin to be not take too much time like the other ones.

jeffreytse commented 3 years ago

Hi @Asocia

Firstly, thanks for the detail experience on this plugin :+1:. In fact, this plugin is initialized during the first precmd, it's different from most of other plugins that are initialized during souring the plugin. So the zsh-prompt-bencmark could not detect the prompt time of those plugins. Actually, the initialization time is short now. It will spend no more time on the following precmd, you can refer to the key source code.

Thanks and regards

sahinakkaya commented 3 years ago

Yes, as you said it is initialized only once. I disabled precmd hook for this plugin and initialized it myself but result didn't change. I'm curious what makes this plugin slow and if it is possible to make it faster. I really like this plugin and it would be great if this issue can be solved.

Some other examinations:

Thanks!

jeffreytse commented 3 years ago

Hi @Asocia

There are lots of stuffs need to be done and they are necessary for this plugin running normally. I can add an option to let you set the time of initializating this plugin. And welcome to star this plugin for further updates in the future.

Thanks and regards

jeffreytse commented 3 years ago

Hi @Asocia

Now this plugin has provided an option called ZVM_INIT_MODE to configure the initialization mode (#113), you can use this snippet ZVM_INIT_MODE=sourcing for doing the initialization inistantly when this plugin is sourcing.

Thanks and regards

sahinakkaya commented 3 years ago

Hi @jeffreytse,

I pulled the recent changes and added ZVM_INIT_MODE=sourcing to my .zshrc. I didn't observe any effect on prompt latency. Thanks.

jeffreytse commented 3 years ago

Hi @Asocia

You're welcome and thanks for your checking out. If you get any other issues in the future, welcome to reach me.

Thanks and regards

sahinakkaya commented 3 years ago

Uhm, I think you got me wrong. By saying "I didn't observe any effect" I meant that the prompt is still slow as before. It was coming in 34ms and after applying the last change it's still 34ms.

jeffreytse commented 3 years ago

Hi @Asocia , sorry for the misunderstanding, did you put the mode option before sourcing this plugin?

sahinakkaya commented 3 years ago

Yes, it's coming before plugins=(... zsh-vi-mode) line. Actually I tried both case but no luck.

jeffreytse commented 3 years ago

In fact, the zvm_init function will do a lot of neccessary works for the initialization. You can have a look and I will try to do some stuff to further reduce the initialization time in the future, thanks for your attention.