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

Incredibly slow on write #215

Open
theahura opened this issue Mar 24, 2023 · 6 comments
Open

Incredibly slow on write #215

theahura opened this issue Mar 24, 2023 · 6 comments
Labels

Comments

@theahura
Copy link

theahura commented Mar 24, 2023

Describe the bug
Recently upgraded vim, and I noticed that vim basically froze every time I wrote a file. I opened a typescript file with about 200loc and profiled using this set of commands: https://stackoverflow.com/a/12216578/3269537
To profile I just opened the file and saved it, then closed the file.

Output:

FUNCTIONS SORTED ON TOTAL TIME                                    count  total (s)   self (s)  function
    9   5.210759   0.000624  maktaba#function#Call()  
    2   2.642967   0.000312  codefmt#FormatBuffer()
    3   2.611224   0.000173  maktaba#syscall#Call()   
    3   2.610018   0.000436  <SNR>80_DoSyscallCommon()
    3   2.605518   0.000057  maktaba#function#Apply()
    3   2.603859   0.001205  maktaba#syscall#DoCall()
    2   2.402088   0.001135  42()                         
    1   1.472221   0.000255  <SNR>33_FormatBufferAndSetRepeat()
    1   1.171017   0.000016  <SNR>32_FmtIfAutoEnabled()  
    2   0.240567   0.000527  <SNR>38_GetFormatter()   
    2   0.239915   0.000078  <SNR>38_IsAvailable()   
    2   0.239817   0.000679  40() 
    2   0.020809   0.001005  maktaba#buffer#Overwrite()
    2   0.019677   0.019450  maktaba#python#ImportModule()
    7   0.004788   0.000730  maktaba#syscall#GetCommand()
   24   0.003627   0.001727  maktaba#string#Strip()      
    5   0.002999   0.000466  codefmt#formatterhelpers#ResolveFlagT
oArray()                                                          
    2   0.002801   0.000086  maktaba#syscall#WithCwd()
    2   0.002715   0.000076  maktaba#syscall#And()   
  116   0.002186   0.001214  maktaba#ensure#IsString()

notice that maktaba takes ~5s and codefmt takes ~2.5

To Reproduce
Minimal vimrc:

set rtp+=~/.vim/bundle/Vundle.vim     
call vundle#begin()                   
Plugin 'VundleVim/Vundle.vim' 
                                                                            
Plugin 'google/vim-maktaba'                                                 
Plugin 'google/vim-codefmt'                                                 
Plugin 'google/vim-glaive'                                                                                              
                                                                            
call vundle#end()       
call glaive#Install()                                                       
                                                                            
" Set up useful defaults for codefmt.
augroup autoformat_settings                                                 
  autocmd FileType bzl AutoFormatBuffer buildifier
  autocmd FileType c,cpp,proto,arduino AutoFormatBuffer clang-format
  autocmd FileType dart AutoFormatBuffer dartfmt
  autocmd FileType go AutoFormatBuffer gofmt                                
  autocmd FileType gn AutoFormatBuffer gn
  autocmd FileType html,css,sass,scss,less,json,vue,javascript,javascriptreact,typescript,typescriptreact AutoFormatBuffer prettier
  autocmd FileType java AutoFormatBuffer google-java-format
  autocmd FileType python AutoFormatBuffer yapf
  autocmd FileType rust AutoFormatBuffer rustfmt                            
augroup END                                                                                                                                             
Glaive codefmt plugin[mappings]  

autocmd BufWritePre * FormatCode

Expected behavior
Previously this was not nearly this slow. I was expecting a non-noticeable latency on save.

OS (version)
Ubuntu 20.04.3 LTS

Additional context
vim version: VIM - Vi IMproved 9.0 (2022 Jun 28, compiled May 10 2022 08:40:37)

@theahura theahura added the bug label Mar 24, 2023
@dbarnett
Copy link
Contributor

Interesting! I notice for 2 FormatBuffer calls there it has 3 syscall#Call ones, but still I believe the 2.6s is total cumulative time for all calls, so it doesn't explain the discrepancy. Off the top of my head, the sequence of events is just the syscall plus writing to the buffer, so I don't have any clear hypothesis where the time could be going… maybe python delays since we use python if available for nicer in-place patching of diff chunks?

Few ideas to try:

  • Check if all languages/formatters seem consistently slow
  • Try with vim's python support disabled and see if performance is any different
  • See if running :syntax off first affects performance any
  • Try performing the same operations manually without using codefmt plugin. Especially the call to write to buffer (something like maktaba#buffer#Overwrite?) it'd be interesting to check performance on.

FWIW, I also have a vision for codefmt where it works more in the background to detect suggesting formatting changes and queue them up, then just applies those on save (invalidating and rerunning formatter if there were any conflicting edits in between). That would really help ameliorate performance for cases where the formatter tool itself just refuses to give reasonable performance + people want format-on-save. But that's a side thought, this issue sounds acute and hopefully something with an easier quick fix.

@theahura
Copy link
Author

theahura commented Mar 26, 2023

thanks for the quick response!

  • python was also noticeable, but not nearly as slow as typescript
  • not sure what you mean by disabling vim's python support?
  • syntax off seemed to make it much slower!
  • i'm also not sure what you mean by running the same operations manually -- where / how would I do that?

@dbarnett
Copy link
Contributor

K thanks! Don't worry too much about the other steps… those were more suggestions to plugin devs as we're digging into troubleshooting (unless you're really itching to do some deeper troubleshooting yourself… LMK and I can clarify).

@theahura
Copy link
Author

cant say that I am 😅 im more in the camp of 'id love if these tools just worked out of the box without that much config'

@theahura
Copy link
Author

theahura commented Jul 5, 2023

Hey @dbarnett just checking to see if there was any movement on this one -- definitely still noticing a fair bit of slowness!

@dbarnett
Copy link
Contributor

dbarnett commented Jul 8, 2023

Nope, I don't have vim 9.0 handy and wasn't able to easily repro performance problems from a few things I tried. I did find vim 9 without working python support didn't seem to have any performance problems.

If you want to locally edit this line https://github.com/google/vim-maktaba/blob/fe95bb10f6bb250943a44632107f6a3d76ce5f28/autoload/maktaba/buffer.vim#L84 to temporarily replace if has('python3') || has('python') with if 0 it might help.

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

No branches or pull requests

2 participants