redxtech / zsh-asdf-direnv

A zsh plugin that loads asdf & asdf-direnv.
MIT License
11 stars 8 forks source link

Alternate approach with better loading performance #2

Open omares opened 2 years ago

omares commented 2 years ago

Hey,

thanks for the awesome plugin, it helped me a lot to understand the asdf & direnv installation process. Unfortunately, I have found out that the performance was/is not very satisfying when I checked zinit times. The plugin takes ~70ms to load and is the slowest in the list by a fair margin, even slower than other bigger contenders.


(not all entries are visible, left some for comparison)

❯ zini times
Plugin loading times:
    1 ms - zdharma-continuum/zinit-annex-as-monitor

   13 ms - romkatv/powerlevel10k
   11 ms - Aloxaf/fzf-tab
    3 ms - zimfw/utility

    3 ms - BurntSushi/ripgrep (sbin command)
   39 ms - zdharma-continuum/fast-syntax-highlighting
   16 ms - zsh-users/zsh-autosuggestions

   --> 73 ms - redxtech/zsh-asdf-direnv <--

    2 ms - jesseduffield/lazygit (sbin command)

I took that as a personal challenge to see if I can improve the situation. After some tinkering, I came up with a solution that does not require any extra plugin, just using plain zinit. As this is a completely different approach than yours I still decided to share it here.

Following code plugin setup is the result, it offers less comfort than your approach, but the loading times are better by a manifold. I assume the "generate once & only source the generated file" is the big performance winner compared to the used "eval" approach.

_zini_asdf_install() {
    # path export temporary required so the asdf script finds itself
    PATH="${PWD}/bin:$PATH"
    asdf plugin add direnv
    asdf install direnv latest
    asdf global direnv latest
    # see https://github.com/asdf-community/asdf-direnv#setup
    asdf exec direnv hook zsh > asdf_direnv_hook.zsh
    echo 'direnv() { asdf exec direnv "$@"; }' >> asdf_direnv_hook.zsh
}

zinit wait:'0b' \
    lucid \
    light-mode \
    sbin:'bin/asdf' \
    atinit:'export ASDF_DIR="$PWD"' \
    atclone:'_zini_asdf_install' \
    src:'lib/asdf.sh' \
    multisrc:'asdf_direnv_hook.zsh' \
    for \
    depth:1 @asdf-vm/asdf
❯ zini times | grep asdf
    2 ms - asdf-vm/asdf (sbin command)

Cheers!

redxtech commented 1 year ago

Wow that's impressive! Thanks for taking the time to do this!

I don't really use this anymore, so I won't be making those changes myself, but if you wanted to make a PR I would be happy to merge them!