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

Slow vimgrep commands in Backlinks #154

Open
MartyLake opened this issue Oct 31, 2024 · 5 comments
Open

Slow vimgrep commands in Backlinks #154

MartyLake opened this issue Oct 31, 2024 · 5 comments

Comments

@MartyLake
Copy link
Contributor

MartyLake commented Oct 31, 2024

Hello,

I noticed that vimgrep is quite "slow" in the end. Do you have any advice on how to replace it with grep instead? Then users will be able to customize their grepprg for rg --vimgrep or ag --vimgrep even.

Best,

@michal-h21
Copy link
Owner

vimgrep is used only in the backlinks collection. It seems that it is possible to change the command used as a backend for vimgrep like this:

if executable('ag')
	set grepprg=ag\ --vimgrep\ $* grepformat=%f:%l:%c:%m
endif

@MartyLake
Copy link
Contributor Author

MartyLake commented Nov 25, 2024

That’s neat! Thank you very much!! Maybe that advice could be part of the README ? I volunteer to write it if you deemed it useful.

Text could be:


Tips

vim-zettel uses vimgrep when collecting backlinks for ZettelBackLinks. It is possible to speed it up by
configuring vimgrep to use an external grep engine, for example ag:

if executable('ag')
	set grepprg=ag\ --vimgrep\ $* grepformat=%f:%l:%c:%m
endif

@michal-h21
Copy link
Owner

@MartyLake that would be great, thanks!

@MartyLake
Copy link
Contributor Author

MartyLake commented Nov 25, 2024

Uh, I said false things: I wanted to make sure it actually does speed up, but for some reason it does not.

Generating backlinks for the same file

Without grepprg

FUNCTION  zettel#vimwiki#wikigrep()
    Defined: ~/.vim/bundle/vim-zettel/autoload/zettel/vimwiki.vim:454
Called 1 time
Total time:   0.619722000
 Self time:   0.619690000

count     total (s)      self (s)
    1                 0.000000000   let paths = []
    1   0.000009000   0.000002000   let idx = vimwiki#vars#get_bufferlocal('wiki_nr')
    1   0.000027000   0.000006000   let path = fnameescape(zettel#vimwiki#path(idx))
    1   0.000005000   0.000001000   let ext = vimwiki#vars#get_wikilocal('ext', idx)
    1                 0.000001000   try
    1                 0.000002000     let command = 'vimgrep ' . a:pattern . 'j ' . path . "**/*" . ext
    1                 0.619371000     noautocmd  execute  command
                                    catch /^Vim\%((\a\+)\)\=:E480/   " No Match
                                      "Ignore it, and move on to the next file
    1                 0.000002000   endtry
   20                 0.000034000   for d in getqflist()
   19                 0.000220000     let filename = fnamemodify(bufname(d.bufnr), ":p")
   19                 0.000024000     call add(paths, filename)
   20                 0.000006000   endfor
    1                 0.000003000   call uniq(paths)
    1                 0.000001000   return paths

With grepperg=ag etc

FUNCTION  zettel#vimwiki#wikigrep()
    Defined: ~/.vim/bundle/vim-zettel/autoload/zettel/vimwiki.vim:454
Called 1 time
Total time:   0.591418000
 Self time:   0.591385000

count     total (s)      self (s)
    1                 0.000001000   let paths = []
    1   0.000010000   0.000002000   let idx = vimwiki#vars#get_bufferlocal('wiki_nr')
    1   0.000028000   0.000006000   let path = fnameescape(zettel#vimwiki#path(idx))
    1   0.000005000   0.000002000   let ext = vimwiki#vars#get_wikilocal('ext', idx)
    1                 0.000001000   try
    1                 0.000002000     let command = 'vimgrep ' . a:pattern . 'j ' . path . "**/*" . ext
    1                 0.590944000     noautocmd  execute  command
                                    catch /^Vim\%((\a\+)\)\=:E480/   " No Match
                                      "Ignore it, and move on to the next file
    1                 0.000002000   endtry
   20                 0.000033000   for d in getqflist()
   19                 0.000342000     let filename = fnamemodify(bufname(d.bufnr), ":p")
   19                 0.000025000     call add(paths, filename)
   20                 0.000008000   endfor
    1                 0.000003000   call uniq(paths)
    1                 0.000001000   return paths

Maybe grepprg only affects calls to grep ? the vim doc says:

Program to use for the `:grep` command.

I guess the speed-up can only happen if vimgrep is replaced by grep instead ?

Something like

    let command = 'grep ' . a:pattern . ' -r ' . path . " *" . ext

but then the s:grep_link_pattern needs to become grep compatible instead of vimgrep ?

@michal-h21
Copy link
Owner

I think more changes would be needed if you wanted to replace vimgrep with something else, because we loop over the list of matched filenames in getqflist. So we would need to parse filenames from Grep output. The pattern would need to be changed too, but that shouldn't be that hard. Hopefully.

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

No branches or pull requests

2 participants