From: Peter Palfrader <zsh-workers=zsh.org@lists.palfrader.org>
To: zsh-workers@zsh.org
Subject: slow ssh host tab completion when many hosts
Date: Wed, 22 Sep 2021 08:30:21 +0000 [thread overview]
Message-ID: <YUrpncsk4+J04KpI@sarek.noreply.org> (raw)
Hi!
with a large ~/.ssh/config, ssh tab completion for hostnames can take a
long time.
I'm on zsh 5.8 in Debian 11, but for testing purposes I am using
Completion/Unix/Command/_ssh and Completion/Unix/Type/_ssh_hosts from
current git master.
As an extreme example, with this as my ~/.ssh/config:
for i in `seq -w 1 10000 `; do echo "Host h$i"; echo " PreferredAuthentications publickey"; done > ssh_config
"ssh h000<tab><tab>" takes about 20 seconds on my laptop to show me the
list of matching hosts.
I suspect, that one of the issues is in _ssh_hosts where it always pops
the first element of an array. At a guess this causes quadratic
runtime behaviour.
The following patch improves the behaviour significantly (to about
2secs):
From bf4280bf4d9c6074fdb01b4817baafefb6d6e12f Mon Sep 17 00:00:00 2001
From: Peter Palfrader <peter@palfrader.org>
Date: Wed, 22 Sep 2021 10:25:08 +0200
Subject: [PATCH] Faster ~/.ssh/config processing
When iterating over the ssh config file, iterate over the array linearly
instead of always processing the first and then removing it from the
list. This speeds up processing significantly.
---
Completion/Unix/Type/_ssh_hosts | 9 +++++----
1 file changed, 5 insertions(+), 4 deletions(-)
diff --git a/Completion/Unix/Type/_ssh_hosts b/Completion/Unix/Type/_ssh_hosts
index bd5366425..67416ab2e 100644
--- a/Completion/Unix/Type/_ssh_hosts
+++ b/Completion/Unix/Type/_ssh_hosts
@@ -20,8 +20,9 @@ if [[ -r $config ]]; then
local key line host
local -a lines=("${(@f)$(<"$config")}") 2>/dev/null
local -a match_args
- while (($#lines)); do
- IFS=$'=\t ' read -r key line <<<"${lines[1]}"
+ local idx=1
+ while [[ $idx -le $#lines ]]; do
+ IFS=$'=\t ' read -r key line <<<"${lines[$idx]}"
if [[ "$key" == ((#i)match) ]]; then
match_args=(${(z)line})
while [[ $#match_args -ge 2 ]]; do
@@ -35,7 +36,7 @@ if [[ -r $config ]]; then
fi
case "$key" in
((#i)include)
- lines[1]=("${(@f)$(cd $HOME/.ssh; cat ${(z)~line})}") 2>/dev/null;;
+ lines[$idx]=("${(@f)$(cd $HOME/.ssh; cat ${(z)~line})}") 2>/dev/null;;
((#i)host(|name))
for host in ${(z)line}; do
case $host in
@@ -43,7 +44,7 @@ if [[ -r $config ]]; then
(*) config_hosts+=("$host") ;;
esac
done ;&
- (*) shift lines;;
+ (*) idx=$((idx + 1));;
esac
done
if (( ${#config_hosts} )); then
--
2.30.2
For your consideration :)
Cheers,
--
| .''`. ** Debian **
Peter Palfrader | : :' : The universal
https://www.palfrader.org/ | `. `' Operating System
| `- https://www.debian.org/
next reply other threads:[~2021-09-22 8:30 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-09-22 8:30 Peter Palfrader [this message]
2021-09-22 17:00 ` Bart Schaefer
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=YUrpncsk4+J04KpI@sarek.noreply.org \
--to=zsh-workers=zsh.org@lists.palfrader.org \
--cc=zsh-workers@zsh.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.vuxu.org/mirror/zsh/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).