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

alias command is slower than function #828

Closed
elentok opened this Issue May 25, 2013 · 8 comments

Comments

Projects
None yet
9 participants
@elentok
Copy link

elentok commented May 25, 2013

Hi,

I've switched to fish about a week ago,
I'm using oh-my-fish,
When I added my aliases (80 aliases) I noticed that it takes about 23 seconds to open a new shell,

after changing every "alias {a}='{b}'" to "function {a}; {b}; end" the shell suddenly opened instantly.

slow aliases: https://github.com/elentok/dotfiles/blob/master/fish/aliases_slow.load.disabled

fast aliases: https://github.com/elentok/dotfiles/blob/master/fish/aliases.load

@dag

This comment has been minimized.

Copy link
Contributor

dag commented May 25, 2013

alias is just using eval to make a function anyway. It also seems to call out to sed, so that might be why it's slow. I tend to think alias should be removed from fish anyway.

For optimal startup time you should use functions and make them lazy-loaded, which you can do interactively with funced and funcsave, or manually by saving each function in its own file ~/.config/fish/functions/name-of-function.fish. In your case it might be easiest to use your existing aliases and add funcsave <alias name> for each alias in the config, source the config once and then remove all the alias and funcsave lines from it.

@ridiculousfish

This comment has been minimized.

Copy link
Member

ridiculousfish commented May 28, 2013

The sed invocations are slowing us down. Things are faster if you invoke aliases like alias g 'git' instead of alias g='git'

@Globegitter

This comment has been minimized.

Copy link

Globegitter commented Oct 27, 2013

Experiencing the same thing now. Replacing the alias x='yyy' with alias x 'yyy' doesn't seem to make things that much faster.
While it seems functions are a workaround, is there a chance that alias will, at some point, have a better performance, as for example zsh has?

@terlar

This comment has been minimized.

Copy link
Contributor

terlar commented Oct 27, 2013

Which fish version are you running? The code shouldn't really be doing any slow operations if you switched to alias x 'yyy'.

The only workaround here is using alias, any reason why you would want to use alias? I suggest it should be removed or replaced with abbreviations.

If you run the latest fish with abbreviations, then you could define a function to do this:

function alias --description 'Define a new abbreviation'
  if test (count $argv) -lt 2
    echo 'abbr: Takes two arguments. First abbreviation and then expanded command'
    return 1
  end

  echo $argv | read -l abbreviation command

  for i in $fish_user_abbreviations
    switch $i
      case "$abbreviation=*"
        echo $i
        break
    end
  end | read -l old_abbreviation

  if set -q old_abbreviation
    set -l idx (contains -i $old_abbreviation $fish_user_abbreviations)
    set -e fish_user_abbreviations[$idx]
  end

  set fish_user_abbreviations $fish_user_abbreviations "$abbreviation=$command"
end

To speed things up you could wrap all your aliases in a conditional block or function run on an universal veriable:

function fish_user_aliases
  alias gs 'git status'
end
set -q __fish_user_aliases; or fish_user_aliases

The upside of using this is also that you could interactively define aliases with this method and they will be saved since the abbreviations are stored in a universal variable.

Third option is to do the same as above but replace the above alias function with the same content as default but put a funcsave at the end. There is no reason why a one-time operation should be run every time you boot your shell.

@xfix

This comment has been minimized.

Copy link
Member

xfix commented Oct 27, 2013

Actually, alias is slow because it involves eval. sed isn't the main problem. It runs another process, right, but this process should run relatively quickly.

@Globegitter

This comment has been minimized.

Copy link

Globegitter commented Oct 27, 2013

@terlar I am running 2.0.0 (via homebrew). Do you think it would be faster if I switch to master branch?
I will probably end up rewriting it and thanks for the help/suggestions, that does make it easier. I was more wondering why my startup of fish is so much slower than zsh was (I switched relatively recently).
Also, it does make switching shells a more pleasant experience if I can just copy&paste some of the the config files.
Anyway, thanks again for the detailed answer.

@justinmayer

This comment has been minimized.

Copy link

justinmayer commented Oct 27, 2013

@Globegitter: I can't say whether the master branch is any faster, but if you like you can help test 2.1.0 by uninstalling your current Homebrew version and installing the fish 2.1.0 .pkg instead.

@faho

This comment has been minimized.

Copy link
Member

faho commented Sep 28, 2015

This should be improved a bit by 54d1d98 and I don't see any low-hanging fruit anymore. Since I don't think we'll make alias a built-in, this is as good as it's gonna get.

@faho faho closed this Sep 28, 2015

@zanchey zanchey modified the milestones: next-2.x, fish-tank Oct 1, 2015

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