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

Re: Shell startup, aliases vs. functions vs. autoloadable functions, and zcompile benefits



Good points all around!  I did play around with hyperfine compiling all zsh scripts in ~/.zprezto and didn't see a tangible difference.

The benchmark I used 

$ hyperfine 'zsh -i -l "exit 0"'

Obviously this is not the BEST benchmark, but it is useful for checking shell startup time -- I might give zsh-bench a look.  I did spend a lot of time with zprof to benchmark my init scripts, but the most time-consuming is simply the "source" statement loading other things, but there's no context as to WHICH files being sourced take the most time.

> you might end up with a mismatched zwc with greater mtime.

This is something I expected to cause hard-to-diagnose problems.  Luckily, the most common way I'd change files inadvertently (git checkout branchname) does not use the time from the commit.

I would expect that the md5sum of a file is reasonably fast, and could be stored in the .zwc for sanity checking, instead of just the "newer than" check.

Again you're unlikely to notice unless your $fpath entries are numerous and deep

> The tradeoff is that the third one occupies less memory than either of the other two (though about the same as the alias) which is significant if there are a lot of seldom-used functions.

I expect that I have more $fpath entries than usual, but the total number of autoloadable functions is much more.

$ echo $#fpath
22

$ for d in $fpath; do n=$(ls $d/* | wc -l); echo "$n $d"; done | sort -nr | head -3
    1162 /usr/share/zsh/5.8/functions
     559 /usr/share/zsh/site-functions
     141 /Users/zachriggle/.zprezto/modules/completion/external/src

Zach Riggle



On Sat, Nov 27, 2021 at 2:22 PM Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
On Sat, Nov 27, 2021 at 12:33 AM Roman Perepelitsa
<roman.perepelitsa@xxxxxxxxx> wrote:
>
> On Sat, Nov 27, 2021 at 9:11 AM Zach Riggle <zachriggle@xxxxxxxxx> wrote:
> >
> > alias foo='foo --flag1 --flag2'
> > foo() { command foo --flag1 --flag2 "$@" }
> > autoloadable module containing (2)
>
> In theory the first and the last should have the same performance and
> the middle should be slower because it has to parse the body. In
> practice you won't find any difference: zsh parser is very fast.

This is true for shell startup time; at runtime there's a small
penalty for the middle one (allocating the function context) and on
the very first execution there's an additional load-time cost for the
third one.  Again you're unlikely to notice unless your $fpath entries
are numerous and deep.

The tradeoff is that the third one occupies less memory than either of
the other two (though about the same as the alias) which is
significant if there are a lot of seldom-used functions.

Otherwise, everything Roman said.


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