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

Slow startup #80

Closed
yujinyuz opened this issue Oct 9, 2021 · 11 comments
Closed

Slow startup #80

yujinyuz opened this issue Oct 9, 2021 · 11 comments

Comments

@yujinyuz
Copy link
Contributor

yujinyuz commented Oct 9, 2021

I'm trying out this plugin and everything seems to be fast except during startup.

Every time I open a new terminal, it kinda slows down for awhile. I'm getting this message

direnv: loading ~/.envrc
direnv: using asdf
direnv: loading ~/.asdf/installs/direnv/2.28.0/env/310956497-1026309726-4161296171-1200834465
direnv: using asdf direnv 2.28.0
direnv: using asdf awscli 2.2.38
direnv: using asdf yarn 1.22.10
direnv: using asdf rust stable
direnv: using asdf golang 1.14
direnv: using asdf nodejs 16.4.1
direnv: using asdf python 3.8.10
direnv: export +CARGO_HOME +GOPATH +GOROOT +NPM_CONFIG_PREFIX +RUSTUP_HOME ~PATH

Is this normal or are there any things I can do for further optimizations?

I'm using fish shell if that helps

@craigfurman
Copy link

craigfurman commented Nov 18, 2021

I'm new to this project so my comment might be wide of the mark, but my understanding is that the latency is mostly caused by evaluating each relevant asdf shim - a tax you'd otherwise pay every time you run the executables behind those shims.

Unfortunately I'm not sure how to use an extensive global ~/.tool-versions and avoid this problem. Not sure if this will help you, but I don't use a use asdf .envrc globally, so by default I am executing asdf shims every time I call a tool, but avoid this direnv startup tax. I put use asdf .envrcs in individual projects, so that when I pop "random system stuff" shells that aren't in those project dirs, I don't pay the startup tax - but I do pay the shim tax, if that makes any sense.

@smorimoto
Copy link
Member

Probably I know the cause, so when I get home, I will make sure it's true and come back here. Anyway, I'm still on an aeroplane!

@jfly
Copy link
Contributor

jfly commented Feb 27, 2022

@smorimoto, did you ever figure out the cause of this?

@ericem
Copy link

ericem commented Mar 25, 2022

A certain amount of startup latency is due to loading the direnv stdlib via the asdf shim. If you replace that line with a system installed version then startup is faster (at least for me).

# Load direnv stdlib if not already loaded
if [ -z "$(declare -f -F watch_file)" ]; then
  if [ -n "$ASDF_DIRENV_SYSTEM_BINARY" ]; then
    eval "$(direnv stdlib)"
  else
    eval "$(asdf exec direnv stdlib)"
  fi
fi

@jfly
Copy link
Contributor

jfly commented Mar 25, 2022

That's an interesting observation, @ericem! I do see a noticeable difference myself:

$ hyperfine --warmup 3 --runs 100 "$(asdf which direnv) stdlib" 'asdf exec direnv stdlib'
Benchmark 1: /home/jeremy/.asdf/installs/direnv/2.30.3/bin/direnv stdlib
  Time (mean ± σ):       1.8 ms ±   0.3 ms    [User: 1.5 ms, System: 1.4 ms]
  Range (min … max):     1.5 ms …   3.0 ms    100 runs

  Warning: Command took less than 5 ms to complete. Results might be inaccurate.

Benchmark 2: asdf exec direnv stdlib
  Time (mean ± σ):      85.6 ms ±   1.1 ms    [User: 103.3 ms, System: 28.3 ms]
  Range (min … max):    83.9 ms …  89.5 ms    100 runs

Summary
  '/home/jeremy/.asdf/installs/direnv/2.30.3/bin/direnv stdlib' ran
   46.64 ± 7.56 times faster than 'asdf exec direnv stdlib'

Running direnv stdlib directly takes under 2ms, whereas going through asdf exec takes ~80-90ms. That's a human perceptible difference, I think.

It does seem like most of that time is spent with asdf exec trying to figure out which direnv to execute. Here's a benchmark of asdf which direnv, which I would expect to be the bulk of the work that asdf exec has to do before actually executing the correct version of the relevant program:

$ hyperfine --warmup 3 --runs 100 "asdf which direnv"
Benchmark 1: asdf which direnv
  Time (mean ± σ):      70.6 ms ±   0.9 ms    [User: 85.0 ms, System: 22.7 ms]
  Range (min … max):    69.0 ms …  74.6 ms    100 runs

@vic
Copy link
Member

vic commented Mar 28, 2022

Hm, interesting @ericem. Using asdf exec direnv goes thru asdf version resolution (asdf trying to determine which direnv version to use) and that seems to be slow and one of the reasons why this project was started and being able to skip version resolution done by shims.

@vic
Copy link
Member

vic commented Mar 28, 2022

asdf version resolution IS slow. :/

@vic
Copy link
Member

vic commented Mar 28, 2022

@ericem thank you so much for your suggestion!. There's now an ASDF_DIRENV_BIN variable inspired by your snippet and documented in main branch.

@yujinyuz
Copy link
Contributor Author

Thanks for the recent PR for ASDF_DIRENV_BIN support. It feels much faster now compared to last time

@jfly
Copy link
Contributor

jfly commented Mar 31, 2022

@vic, thanks for working on this! Do you think we can close out this issue now?

@vic
Copy link
Member

vic commented Mar 31, 2022

Closing, looks like @yujinyuz and other's issue has been solved. Let's open another issue if we find other things to improve.

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

No branches or pull requests

6 participants