Pasting is slow when using bracketed-paste-magic #141

Open
aaronjensen opened this Issue Apr 13, 2016 · 20 comments

7 participants

@aaronjensen
aaronjensen commented Apr 13, 2016 edited

Because every character pasted attempts to autosuggest, pasting something is slow.

slow-paste

Ideally this plugin would support bracketed paste mode out of the box (or the completions would be async as proposed in #134

@aaronjensen aaronjensen changed the title from Pasting is slow to Pasting is slow and doesn't play well w/ safe-paste Apr 13, 2016
@ericfreese ericfreese referenced this issue Apr 14, 2016
Merged

v0.3.2 #142

6 of 6 tasks complete
@ericfreese
zsh-users member

Will look into what I can do to speed the paste up when using bracketed-paste-magic. In the mean time, the workaround from #102 (comment) will work to speed up pasting:

zstyle ':bracketed-paste-magic' active-widgets '.self-*'

Let me know if you'd like me to split this into two issues.

Yeah, can you create a separate issue for interaction with safe-paste? We can just keep this particular issue about slow pasting with bracketed-paste-magic.

@ericfreese
zsh-users member

The best solution I can think of right now would be to add a hook to paste-init that temporarily disables suggestion-fetching and a hook to paste-finish that re-enables it. Using the active-widgets zstyle to detect if bracketed-paste-magic is enabled.

Not the most elegant solution though... I'm gonna sleep on this one a bit, and see if I can come up with something a little nicer.

@aaronjensen

I can't say I noticed much if any change w/ zstyle ':bracketed-paste-magic' active-widgets '.self-*', it's still just as slow to paste as far as I can tell.

@ericfreese ericfreese changed the title from Pasting is slow and doesn't play well w/ safe-paste to Pasting is slow when using bracketed-paste-magic Apr 17, 2016
@ericfreese
zsh-users member

Logging another possible solution here:

bindkey '^[[200~' to a function that disables suggestions and '^[[201~' to a function that re-enables them.

I think I like this better than the solution above. Would be a little more general than detecting and dealing with bracketed-paste-magic specifically.

@aaronjensen

do bindkeys stack? Would that not interfere with bracketed-paste-magic? If not, seems simple enough

@lbolla

This is affecting me, too. Using zsh-autosuggestions.zsh v0.3.1. Workaround didn't work (I am not using oh-my-zsh or other plugins, btw).

@ericfreese
zsh-users member

@lbolla

I am not using oh-my-zsh or other plugins, btw

Are you using bracketed-paste-magic?

Do you see the issue without any other config? Try something like this:

% zsh -f
%% source zsh-autosuggestions.zsh
%% [paste something long]
@lbolla

@ericfreese I tried what you suggested, but pasting is still slow. I can try to create an animation to show how slow, if that helps. I tried using different terminals (Gnome Terminal and xterm), just in case: same result.

@ericfreese
zsh-users member

@aaronjensen Please try the fixes/slow_bracketed_paste_magic branch, and let me know if it solves your issues.

@ericfreese
zsh-users member

zprof output of pasting long command from zsh-users/zsh-syntax-highlighting#295 (comment)

@aaronjensen

@ericfreese sorry, still slow :/ w/o zsh-autosuggestions and my config, paste is near instant. with it, it's slow. Trying from zsh -f it's faster, but still not as fast as my full config w/o zsh-autosuggestions.

@ericfreese ericfreese added performance and removed bug labels May 12, 2016
@jasontibbitts

I have basically the same issue, but for me it depends on the version of zsh in use. On my Fedora 23 machines with zsh 5.2, pasting is always instantaneous. But when I ssh to a RHEL7 machines with zsh 5.0.2, it's slow. I suspect that 5.0.2 is simply to old to support bracketed paste, but I'm not sure.

@aaronjensen

I just tried upgrading to zsh 5.2 and didn't see a performance improvement, even w/ https://github.com/zsh-users/zsh-autosuggestions/tree/fixes/slow_bracketed_paste_magic

@derimagia

If you have a high enough version of zsh (5.2), I recommend using https://github.com/zsh-users/zsh/blob/master/Functions/Zle/bracketed-paste-url-magic (See http://www.zsh.org/mla/workers/2015/msg02610.html) as an alternative.

@aaronjensen

@derimagia thanks, but that's not the problem here.

@ericfreese Pasting with zsh-autosuggstions is still slow even with disabling bracketed-paste-magic, 9fb9675, zsh 5.2, and/or

zstyle ':bracketed-paste-magic' active-widgets '.self-*'

Should one of those have worked for me, or are you still working/thinking on this one? Thanks!

@lbolla
lbolla commented Jul 13, 2016 edited

Like others, I am experiencing slow paste even if I am not using bracketed-paste-magic.
I digged a bit into zsh-autosuggest code and found that the slowness is mainly due to too many levels of indirection when selecting the autosuggest function.
As a test, I tried to manually inline the "default" strategy in _zsh_autosuggest_modify:

diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh
index 4e3c62e..707f745 100644
--- a/zsh-autosuggestions.zsh
+++ b/zsh-autosuggestions.zsh
@@ -243,7 +243,17 @@ _zsh_autosuggest_modify() {
    # Get a new suggestion if the buffer is not empty after modification
    local suggestion
    if [ $#BUFFER -gt 0 ]; then
-       suggestion="$(_zsh_autosuggest_suggestion "$BUFFER")"
+       # suggestion="$(_zsh_autosuggest_suggestion "$BUFFER")"
+
+            # TODO inline _zsh_autosuggest_strategy_default
+                local prefix="$BUFFER"
+
+                # Get the keys of the history items that match
+                local -a histkeys
+                histkeys=(${(k)history[(r)$prefix*]})
+
+                # Echo the value of the first key
+                suggestion="${history[$histkeys[1]]}"
    fi

    # Add the suggestion to the POSTDISPLAY

(The code is in a branch in my own fork: lbolla@dac5b7f)

The result is an overall speed up of autosuggestion in all cases, especially when pasting (with mouse middle click) clipboard data.

@lbolla

Another effective workaround is to disable autosuggestion if $BUFFER is too large.
This effectively means that the autosuggest function won't be called many times (e.g. if you are pasting a very large chunk of text, it'll be called just a few times).

diff --git a/zsh-autosuggestions.zsh b/zsh-autosuggestions.zsh
index 4e3c62e..7a74ab0 100644
--- a/zsh-autosuggestions.zsh
+++ b/zsh-autosuggestions.zsh
@@ -242,7 +242,7 @@ _zsh_autosuggest_modify() {

        # Get a new suggestion if the buffer is not empty after modification
        local suggestion
-       if [ $#BUFFER -gt 0 ]; then
+       if [ $#BUFFER -gt 0 -a $#BUFFER -lt 20 ]; then
                suggestion="$(_zsh_autosuggest_suggestion "$BUFFER")"
        fi

See lbolla@9572ccb

@tonybaroneee

@lbolla thanks for that!

@balta2ar

@lbolla Could you please send these two changes as pull requests so that it's easies for @ericfreese to merge them if he wants to?
As a side note, I'm really looking forward to seeing async implemented natively. I'm currently sitting on that abandoned zsh-async branch and it really saves my day. I can't use zas with it :(

@lbolla

Issued PR #177

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment