Zsh-autocomplete: Typying laggs due to slow completion

Created on 6 May 2020  路  22Comments  路  Source: marlonrichert/zsh-autocomplete

Hey,
thanks again for this plugin! :pray:

I use it now just my second day, but what really bothers me are the lags. This is especially the case for paths. Each time I enter a slash it starts to lag. I guess this will apply for all completions that are not optimized or simply slow.
I must admit I have rarely a knowledge of such things in Zsh, but is is possible to do asynchronous completion? As much as I like this plugin: this is a no-go for me at the moment. Maybe I need to disable completion sources that are slow. I don't know.

Most helpful comment

Ok, 1, asynchronous doesn't actually have to be faster to make a difference, it just needs to be non-blocking. ie, usually laggy behavior is because something is hanging up the process you're concentrated on.
-You want typing to update but it's being preventing be completion generation.
2, it's not that my monitor impacts performance, it's that I have more that twice the opportunity to (frames) to notice any delay. Where at 60fps I have maybe 16ms to see any lag for any given frame. 160ms for 10 frames.
For a 144Hz monitor it's more like 6-8ms.
There is a small flickering, and I noticed it with https://github.com/zsh-users/zsh-autosuggestions
and I actually disabled it for now (not that worried about it)
But now that I think about it it may be because you were causing the display to refresh a lot, which makes sense because you need to update the completions every character that's typed, but I wonder if the refresh is taking longer than it needs to.
I don't doubt some of it is quirks with the zsh library an the other half is your plugin.

All 22 comments

Can you give me an exact test case where the completion feels particularly slow? Then I can try to reproduce it and figure out how to speed it up.

For me, path completion feels very snappy. I have not noticed any problems with it. On the other hand, I find that completing certain git commands can feel slow sometimes.

I must admit I have rarely a knowledge of such things in Zsh, but is is possible to do asynchronous completion?

I have tried to do that using https://github.com/mafredri/zsh-async/, but the problem with async completion is that if the completion finishes when the Zsh Line Editor is no longer active, it simply will not update. So, basically, while typing and then you pause, the last couple of letters fail to trigger completion. It only worked as long as I kept typing, which didn't feel quite right. 馃檪

Hmm okay it is my fault. Sorry for your inconvenience. It happens when my PC is under load. But is is quite "interesting". So it happens already when I run some simulations for work. but actually these just increase the CPU usage to ~30%. But doing more "tests", I recognized that this only happens for special cases. So I use TMux. So if I'm in a TMux window (I hope you know the terminology, it is like a terminal tab or a virtual desktop) and have the simulation running in one pane (like a window on a virtual desktop) and them simply do like cd ~/ in another pane along the simulation, it lags on each slash. Open/switch to a TMux window without the simulation it is smooth. So it is not the fault of this plugin. Probably this is a general issue, but this plugin makes me feel it more directly. Super weird behavior. No clue where to start debugging this. :see_no_evil:

But sad in general about the async stuff. Always nice to have it. :/

But glad it's not a problem in my plugin. 馃槈

Reopening, because @Mallchad reports the same problem.

@Mallchad The problem was that zsh-autocomplete wasn't expecting AUTO_CD to be enabled and no matches were generated when you typed a directory in the command position. This should be fixed now. In addition, I've added some enhancements to the way directories and files are presented in the completion results. Please update and check it out. 馃檪

It does indeed feel less laggy, although, it does not seem to be gone entirely.
Namely, if I misspell any character it starts to lag again when completing a path.
So I assume this is due to some kind of completion timeout with directories.
But only (as far as I can tell) when you have the direction on it's own without other commands in front.

Thanks, I鈥檒l investigate.

@Mallchad I cannot reproduce it. When you make a misspelling, what do you do next? Do you just continue typing? Can you give me an example of a misspelling plus what you typed afterwards that made the completion slow?

I usually have typed out a whole word / command by the time I have a chance to process the fact that I've made a mistake, but current it is made painfully aware to me that I've made the mistake because if I ignore, or don't realize the popup that is telling that I made a mistake.
Even if I put 1 character wrong sometimes it blocks input for a few hundred miliseconds.
This ONLY happens if it can't find any candidates though.

@Mallchad All right, I pushed in a new fix. You now get error correction on any path you type whenever you type a /. Please update and let me know how that's working out for you. 馃檪

This does seem to improve things quite a lot.
Thanks man.
But, I think this is still a hack to the symptom, rather than tackling the core issue.
I still notice that the error popup occurs, although it's quite a lot less bad when the "magic-slash"
autocorrects because it stops once the candidate has been completed successfully.
Peek 2020-05-17 14-00
This shows some of what I'm seeing, again, when it can't find completion candidates sometimes
(but not always) there is noticeable lag that can mess typing up.
This is most notable if you type one of the earliest characters wrong and the prompt lags because
either you or the magic-slash gets a chance to try and completing it which can mess up successive
keystrokes.
Thanks again for the fast responses and fixes :)

PS
I had to force myself to not reflex tab complete and purposefully make errors just to get that click D:

@Mallchad I watched your video several times, but I cannot tell the difference between you stopping with typing or the system lagging. 馃檪 Can you please tell at which points exactly you experienced lag? With which inputs did the system get stuck? And what were the completions offered afterwards?

Rather than post a video, I'd prefer if you could just type some test cases for me. 馃檪

Like: "Given directory structure A, when I type B, the system hangs. Then, when the system resumes, it outputs C."

If am looking for, say a binary in /home/mallchad/.local
I might type /home/nallchad by mistake.
With the magic slash it will successfully complete (because it's the only completion).
However, after I've hit the n key the small delay has kicked in, that can throw me off.
Another example.
If I type man
then hit space, it then defiantly hangs (probably because it's 20,828 completion candidates). -It seems like it's supposed to cache candidates but I'm not feeling it.
Another example.
Say I want to delve into my desktop applications
I might type /usr/share/applacations
if I hit one wrong key in that wrong word I get the red text that flickers when I type and delays me seeing the result.
The biggest thing I notice is if I (naturally) try to backspace to correct my mistake, then it hangs really quite bad, making it hard to correct my mistake. (I could trust in the magic-slash but I'm 100% on board with that kind of completion :) ).

-Side note
I do have a very high refresh rate monitor so the apparent slowest it way more visible to me than it would be to most people.
*sometimes I see delays that are in the tens of milliseconds range, but that I can tolerate.

Thanks for the detailed description! I still cannot reproduce it at all, but now at least I have some idea of how to fix it. 馃檪

@Mallchad I pushed in a new fix. Please try it out and let me know if it works. 馃檪

@marlonrichert this is more than good enough.
I still think that it might be possible to do async, and this would be the best final result.
But this is very good for me now, I won't ask this of you, it would require a lot of rethinking.
Thank you for all your hard work :tada:

You're welcome! 馃コ

I actually tried to implement asynchronous completion at some point, but it was very buggy 鈥攁nd not actually any faster. 馃槀

My current code does the automatic listing in a different way, though, than originally. So, I might give it a try again at some point. 馃檪

I do have a very high refresh rate monitor so the apparent slowest it way more visible to me than it would be to most people.

@Mallchad How does this impact the performance of your shell? Is the shell refreshed each time your monitor refreshes?

Ok, 1, asynchronous doesn't actually have to be faster to make a difference, it just needs to be non-blocking. ie, usually laggy behavior is because something is hanging up the process you're concentrated on.
-You want typing to update but it's being preventing be completion generation.
2, it's not that my monitor impacts performance, it's that I have more that twice the opportunity to (frames) to notice any delay. Where at 60fps I have maybe 16ms to see any lag for any given frame. 160ms for 10 frames.
For a 144Hz monitor it's more like 6-8ms.
There is a small flickering, and I noticed it with https://github.com/zsh-users/zsh-autosuggestions
and I actually disabled it for now (not that worried about it)
But now that I think about it it may be because you were causing the display to refresh a lot, which makes sense because you need to update the completions every character that's typed, but I wonder if the refresh is taking longer than it needs to.
I don't doubt some of it is quirks with the zsh library an the other half is your plugin.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yutakatay picture yutakatay  路  3Comments

windowswithoutborders picture windowswithoutborders  路  6Comments

paw-lu picture paw-lu  路  3Comments

jcassee picture jcassee  路  6Comments

pcjmfranken picture pcjmfranken  路  5Comments