From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on inbox.vuxu.org X-Spam-Level: X-Spam-Status: No, score=-3.3 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,UNPARSEABLE_RELAY autolearn=ham autolearn_force=no version=3.4.4 Received: (qmail 19239 invoked from network); 22 Sep 2021 08:30:55 -0000 Received: from zero.zsh.org (2a02:898:31:0:48:4558:7a:7368) by inbox.vuxu.org with ESMTPUTF8; 22 Sep 2021 08:30:55 -0000 ARC-Seal: i=1; cv=none; a=rsa-sha256; d=zsh.org; s=rsa-20210803; t=1632299455; b=OWPhQuHsCq/8Ig704vjEZI7KY8nlBvkBJdInMTJZEURC3fR4/RtEdJISMcWNFl+Y3PMCmqr4zj npVWQFuywOpna5hq6vMtQhLW5JdRDg68H5b7VX44raPo6v1uYlfOp02MOXTrA4FLpTYIJ+Ufk2 ifQNfKh5CP7csL4AZGOmQ2tg9YjoQB3x9Hk5LvpfrtIYCgMKWpAdcu5GRug0bkBtpayf0VF4v/ akJ2AWL3SIQFbap9cCibhx05Y/bk4TvBTXz98tH3HOhvniH1djPHS/2A/74VBMWHBnZyu7PxPy SiLuBm/VAwVvNPVYhtQifGECYTRLfKd5iTALHCPgF38S/A==; ARC-Authentication-Results: i=1; zsh.org; iprev=pass (sarek.noreply.org) smtp.remote-ip=213.235.231.205; dmarc=none header.from=lists.palfrader.org; arc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed; d=zsh.org; s=rsa-20210803; t=1632299455; bh=37ctvtJ38aaK9wzqKBxOUrnyhAw8WHLB5tdljaq6kJQ=; h=List-Archive:List-Owner:List-Post:List-Unsubscribe:List-Subscribe:List-Help: List-Id:Sender:Content-Type:MIME-Version:Message-ID:Subject:To:From:Date: DKIM-Signature; b=ldqpR2N2U57th9ZCVx7Rt/Z8W0WzC/gTD4qIOhX8j1QxvmwrrLoTKHf/GGZaYxPXQoUkxrQQBF e5s2B7sh1JMpb0FSE16VJ/8qkbR2hH8UoaKOsCJNidFEGVU37AKklZPhm9hnnQdMB74VQPtP7K mUYqJQy664NFyY7Qfp1JvS+ECMufeoLZwdPrZk6HZnwHTk5PeWje4ZOIsw0AT+Ou8VNGIK4Dt5 VKGo3Xppc+doFR8j0dwUZhkcQi0fnMn3HrFEZ/T3EpqOAdx4dsZ2gj+Whv9C4N4j8fpLrLTQcM +tfgh0AykXelg7wC8HsUFfREMZdPsUH1dICN6EUgsV2bhw==; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=zsh.org; s=rsa-20210803; h=List-Archive:List-Owner:List-Post:List-Unsubscribe: List-Subscribe:List-Help:List-Id:Sender:Content-Type:MIME-Version:Message-ID: Subject:To:From:Date:Reply-To:Cc:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References; bh=A/LSjRelcNk4+mzVIcZOwPZcqRsCSVtWKZTPWg5hKiI=; b=Bnr+/LkjTV5grEC3/VO6dnEXuk G+bqin9hkMQFGPrm706GvcWaZu8CpQPpB2eQC/0sVEl6nh7HwluWXZt4ukxTeP/yvXRS7nZ9rZua/ 2hic4iLJEtceH4CIcCT2m4AlzPijUQMlPMeVf6gZbILuAy3RgTZB373boHvoNlaEtlf/3FHhYb9Wd OoCYCM48NM7HYJg1wd7vaI7TJofo/B+KMR+wLMMe5P8oDjpX2v0adPzPMobzWy2mAmwiuQZSjhPP4 WCc96VUaBHkcITjnxN4xT+Dyp3knZZF5H+/6nQsKhLwZojRShMp1Rl9wAAXUtAfh/5d1CK/myGAwK d5rL3Ysw==; Received: from authenticated user by zero.zsh.org with local id 1mSxeY-000EMw-7U; Wed, 22 Sep 2021 08:30:54 +0000 Authentication-Results: zsh.org; iprev=pass (sarek.noreply.org) smtp.remote-ip=213.235.231.205; dmarc=none header.from=lists.palfrader.org; arc=none Received: from sarek.noreply.org ([213.235.231.205]:42630) by zero.zsh.org with esmtps (TLS1.3:TLS_AES_256_GCM_SHA384:256) id 1mSxe2-000E5y-Bd; Wed, 22 Sep 2021 08:30:22 +0000 Received: by sarek.noreply.org (Postfix, from userid 1000) id 051F2E0883; Wed, 22 Sep 2021 08:30:22 +0000 (UTC) Date: Wed, 22 Sep 2021 08:30:21 +0000 From: Peter Palfrader To: zsh-workers@zsh.org Subject: slow ssh host tab completion when many hosts Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Seq: 49431 Archived-At: X-Loop: zsh-workers@zsh.org Errors-To: zsh-workers-owner@zsh.org Precedence: list Precedence: bulk Sender: zsh-workers-request@zsh.org X-no-archive: yes List-Id: List-Help: List-Subscribe: List-Unsubscribe: List-Post: List-Owner: List-Archive: 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" 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 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/