airblade / vim-gitgutter

A Vim plugin which shows git diff markers in the sign column and stages/previews/undoes hunks and partial hunks.
MIT License
8.34k stars 296 forks source link

Performance issues after base_path fix #878

Closed azuenko closed 8 months ago

azuenko commented 8 months ago

I'm experiencing performance issues after the latest git-gutter update https://github.com/airblade/vim-gitgutter/commit/84bc2d68c0b32440bd1b12f245607a563a9421e4

Notice how the cursor starts blinking annoyingly.

Before

Kapture 2024-01-04 at 16 51 15

After

Kapture 2024-01-04 at 16 53 29

What is the latest commit SHA in your installed vim-gitgutter?

84bc2d6

What vim/nvim version are you on?

> brew info neovim
==> neovim: stable 0.9.4 (bottled), HEAD
> brew info kitty
==> kitty: 0.31.0
airblade commented 8 months ago

There's no performance problem for me which makes it difficult for me to determine when it is fixed.

Please could you profile gitgutter? This will show exactly where the slowdown is occurring.

I'm surprised, though, because 84bc2d6 simply replaced one built-in function – split() – with another – strridx() – and two array slices.

azuenko commented 8 months ago

Hello Andy,

Here are two profile logs, for the normal & slow versions. In both cases I opened nvim, then run 4x O<ESC>u on a file in git repo, and closed it. Hope that it helps!

gitgutter_profile_normal.log gitgutter_profile_slow.log

azuenko commented 8 months ago

Some more info that could help you to debug the issue.

I put the debug statements

    call gitgutter#debug#log(base)
    call gitgutter#debug#log(bpath)

and here is what I get in the log:

Old version (before the fix)

  0.956426 function <SNR>74_on_exit_nvim[2]..gitgutter#diff#handler[1]:
  0.956426 @@ -255,0 +256,2 @@ function! gitgutter#utility#base_path(bufnr)^@@@ -256,0 +259 @@ function! gitgutter#utility#base_path(bufnr)

  1.484144 CursorHold Autocommands for "*"..function gitgutter#process_buffer[23]..gitgutter#diff#run_diff[55]..gitgutter#utility#base_path[7]:
  1.484144 

  1.485144 CursorHold Autocommands for "*"..function gitgutter#process_buffer[23]..gitgutter#diff#run_diff[55]..gitgutter#utility#base_path[8]:
  1.485144 autoload/gitgutter/utility.vim

New version (slow)

  0.959054 function <SNR>74_on_exit_nvim[2]..gitgutter#diff#handler[1]:
  0.959054 @@ -256,0 +257,3 @@ function! gitgutter#utility#base_path(bufnr)

  1.207955 CursorHold Autocommands for "*"..function gitgutter#process_buffer[23]..gitgutter#diff#run_diff[55]..gitgutter#utility#base_path[8]:
  1.207955 :autoload/gitgutter/utility.vim

  1.208686 CursorHold Autocommands for "*"..function gitgutter#process_buffer[23]..gitgutter#diff#run_diff[55]..gitgutter#utility#base_path[9]:
  1.208686 autoload/gitgutter/utility.vim

The notable difference is in base var, which is set to :autoload/gitgutter/utility.vim in the new (slow) version. I suspect the non-empty base triggers some additional code that causes slow down.

airblade commented 8 months ago

Thanks for digging into that.

You're right: I was using substring indexes incorrectly which meant that base wasn't empty when it should have been, which was triggering additional code including a system() call.

I think I've got it right now...!

azuenko commented 8 months ago

Thank you Andy for fixing the issue. And generally thanks for developing and supporting this small yet quite useful plugin!

I can confirm the fix does its job, I've no more performance issues with the plugin.