Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: slow startup of zsh (1x slower than bash)



On Fri, Sep 6, 2019 at 3:44 PM Peng Yu <pengyu.ut@xxxxxxxxx> wrote:
> I build a library that one causes another. In such cases, the
> startup time matters, as most of scripts don’t take a long
> time to run, I don’t want the startup eat up too much time.
> I know I can use ‘source’ circumvent this problem to a certain extent.

Autoloading was designed to solve this problem (among others). Give it
a try. Your zsh scripts might become orders of magnitude faster than
in bash and you don't even have to change them.

For example, suppose you have two files in your ~/bin:

    % cat ~/bin/greet
    #!/usr/bin/env zsh

    print -r "Hello, $1"

    % cat ~/bin/spam
    #!/usr/bin/env zsh

    repeat $1 greet $2

Let's further suppose that ~/bin is in your $PATH.

    % spam 3 $USER
    Hello, romka
    Hello, romka
    Hello, romka

You can make greet and spam much faster by adding these two lines to
your ~/.zshrc:

    fpath+=~/bin
    autoload -Uz ~/bin/*(x:t)

On my system this simple 2-line addition to ~/.zshrc speeds up `spam 3
$USER` by a factor of 5000.

Note that you can still invoke your scripts the same way as before.
Use ~/bin/spam or =spam to force-spawn a new shell process, while the
plain spam will call a function within the same shell process. The
first call to spam will automatically source ~/bin/spam and turn it
into a function.

> But having a shorter startup time is still a good thing. If bash can
> do it, I don’t think zsh is absolutely unable to do it.

Faster is always better than slower but when there is no practical
difference then the development effort might be better spent
elsewhere. Everyone's environment is different but from my point of
view zsh startup is fast enough to be indistinguishable from zero.


Roman.



Messages sorted by: Reverse Date, Date, Thread, Author