Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ssh completion is slow if /etc/hosts is long #4604

Closed
nthapaliya opened this issue Dec 17, 2017 · 4 comments
Closed

ssh completion is slow if /etc/hosts is long #4604

nthapaliya opened this issue Dec 17, 2017 · 4 comments

Comments

@nthapaliya
Copy link
Contributor

nthapaliya commented Dec 17, 2017

> cat /etc/hosts| wc -l
   47737

If /etc/hosts is a large file (for example, using https://github.com/StevenBlack/hosts to block adware), then ssh completion is really slow. I've isolated the offending line that causes most of the hangup:
https://github.com/fish-shell/fish-shell/blob/master/share/functions/__fish_print_hostnames.fish#L12

If that line is replaced with a grep/awk pipeline, ssh completion feels instantaneous again.

For example:

diff --git a/share/functions/__fish_print_hostnames.fish b/share/functions/__fish_print_hostnames.fish
index aabbf0f6..748eda2c 100644
--- a/share/functions/__fish_print_hostnames.fish
+++ b/share/functions/__fish_print_hostnames.fish
@@ -9,7 +9,7 @@ function __fish_print_hostnames -d "Print a list of known hostnames"
         getent hosts | string match -r -v '^0.0.0.0' | string replace -r '^\s*\S+\s+' '' | string split ' '
     else if test -r /etc/hosts
         # Ignore commented lines and functionally empty lines.
-        string match -r -v '^\s*0.0.0.0|^\s*#|^\s*$' </etc/hosts | string replace -r -a '#.*$' '' | string replace -r '^\s*\S+\s+' '' | string trim | string replace -r -a '\s+' ' ' | string split ' '
+        grep -Ev '^\s*0.0.0.0|^\s*#|^\s*$' /etc/hosts | awk '{print $NF}' | sort | uniq
     end
 
     # Print nfs servers from /etc/fstab

Two solutions come to mind:

  1. Long term: profile and optimize the string match builtin.
    • Pros: all scripts benefit from this.
    • Cons: probably more work, as someone needs to dig into the c/c++ code (out of my skillset)
  2. Short term: Replace those offending lines with some grep/awk.

MacOS High Sierra
fish, version 2.7.0

@faho faho mentioned this issue Dec 18, 2017
3 tasks
@faho
Copy link
Member

faho commented Dec 18, 2017

@nthapaliya: Please try #4610. I have a hosts file of about 30k entries, and for me that speeds up __fish_print_hostnames from 240ms to 70ms.

@nthapaliya
Copy link
Contributor Author

nthapaliya commented Dec 19, 2017

@faho its much better, much more usable now. ssh <tab> completes much faster.

However __complete_user_at_hosts doesn't seem to be working as intended: ssh root@<tab> doesn't complete, just rings the visual bell. If it is an unrelated problem, I'm glad to open another issue.

@faho
Copy link
Member

faho commented Dec 19, 2017

However __complete_user_at_hosts doesn't seem to be working as intended: ssh root@ doesn't complete, just rings the visual bell. If it is an unrelated problem, I'm glad to open another issue.

Are you sure that worked before? That function is defined as

# Defined in /usr/share/fish/functions/__fish_complete_user_at_hosts.fish @ line 1
function __fish_complete_user_at_hosts --description 'Print list host-names with user@'
	for user_at in (commandline -ct | string match -r '.*@'; or echo "")(__fish_print_hostnames)
        echo $user_at
    end
end

So there's some string stuff involved. With the current token being "root@", that commandline -ct stuff should echo it, so it should be crossed with __fish_print_hostnames - which you have already confirmed works. It's possible that some bytes get skipped (an off-by-one-error or similar), but this is the first I'm seeing of that.

@faho faho added this to the fish-3.0 milestone Dec 19, 2017
@nthapaliya
Copy link
Contributor Author

Are you sure that worked before?

Now that you mention it, that 'broken' behavior existed before your patch, and is unrelated to your changes. I am convinced I was able to complete ssh root@<tab> at some point in the past, but I don't remember when.

@faho faho closed this as completed in 2de38ef Dec 20, 2017
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 17, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants