vijaymarupudi / nvim-fzf

A Lua API for using fzf in neovim.
MIT License
340 stars 13 forks source link

Performance issue: small delay between the command and its output #43

Closed gbrlsnchs closed 3 years ago

gbrlsnchs commented 3 years ago

First of all, thanks for this library, it's awesome! I'm having lots of fun with it. :smile:

One thing that still bites me is this small delay after running fzf.fzf(...) and the floating window showing up with FZF ready to be used. I'm comparing this to, say, :FZF, which is the command that comes with FZF itself.

Considering it's not an issue on my side (which could still be), in what ways do you think we could improve this to make it as fast as :FZF? I'd be willing to work on this, but I need some pointers. :confused:

vijaymarupudi commented 3 years ago

On my machine, this feels imperceptible to me, could you explain more about which specific delay you are talking about i.e. is it before the window appears on your screen or before? I'm not quite sure why this delay would be caused, could you also provide the specs and operating system of your machine?

cpkio commented 3 years ago

@gbrlsnchs are you using keyboard shortcuts to call fzf.fzf?

gbrlsnchs commented 3 years ago

On my machine, this feels imperceptible to me, could you explain more about which specific delay you are talking about i.e. is it before the window appears on your screen or before? I'm not quite sure why this delay would be caused, could you also provide the specs and operating system of your machine?

@vijaymarupudi It's a little delay for the floating window to appear, it's a really small delay but perceptible compared to :FZF.

I'm running on Arch Linux. I don't think it's my specs, like I said, :FZF opens its window really fast.

@gbrlsnchs are you using keyboard shortcuts to call fzf.fzf?

@cpkio Yes, I am! Both :FZF and fzf.fzf are called via mappings registered to https://github.com/folke/which-key.nvim.

cpkio commented 3 years ago

@gbrlsnchs Try to call corresponding function through command line.

gbrlsnchs commented 3 years ago

@cpkio I exec'd :lua coroutine.wrap(function() require("fzf").fzf({"choice1", "choice2"}) end)() and it still has the small delay I mentioned. :cry:

vijaymarupudi commented 3 years ago

Interesting... I do want to fix this. Does the delay happen when you run this?

vim.cmd "vnew"
vim.fn.termopen("fzf")
vim.cmd "startinsert"
gbrlsnchs commented 3 years ago

One question: does it use my $SHELL to execute the command? If that's the case, I'm pretty sure that's why this is happening, since my ZSH has a little startup delay as well. I always use sh to execute stuff in order to avoid this.

Edit: I ran the commands above and, yes, I felt it has a micro hiccup, which is not present for :FZF.

vijaymarupudi commented 3 years ago

That might be it! Try this

vim.cmd "vnew"
vim.fn.termopen({"fzf"})
vim.cmd "startinsert"
gbrlsnchs commented 3 years ago

I don't know how to benchmark both commands, but I have the impression that vim.fn.termopen({"fzf"}) is a little snappier than vim.fn.termopen("fzf").

Nonetheless, both still seem faster than fzf.fzf(...). Not sure if it's shell related, though, that was just a guess... also, fzf (the command, without arguments) seems to open the prompt before loading the input, not sure if that helps with speed as well.

vijaymarupudi commented 3 years ago

I guess the question is if the second variant seems as fast as :FZF, since I cannot perceive any difference.

nvim-fzf doesn't send any data until after the termopen function is executed. It does, however, execute two shell commands currently, one to make a named pipe and one to launch fzf. I can eliminate those, but need data to see if it's worth it.

gbrlsnchs commented 3 years ago

Yes, vim.fn.termopen({"fzf"}) is as fast as :FZF.

vijaymarupudi commented 3 years ago

Great! I've just pushed an optimization patch for this to master, could you test that?

gbrlsnchs commented 3 years ago

All right, it's as fast as :FZF now! Nice one, and thanks for the help! :clap:

vijaymarupudi commented 3 years ago

Great! Thank you for the excellent issue and figuring out that it was the shell that was the cause of the problems.