Yleisradio / chtf

Terraform version switcher with automatic install
MIT License
21 stars 4 forks source link

Sourcing chtf.sh super-slow on macOS Catalina #4

Closed mkozjak closed 3 years ago

mkozjak commented 3 years ago

chtf.sh sourcing hangs on this line for quite a while: +/usr/local/share/chtf/chtf.sh:32> grep -q '^yleisradio/terraforms$'

Time without chtf:

time  zsh -i -c exit
zsh -i -c exit  0.04s user 0.03s system 86% cpu 0.073 total

and time with chtf:

time  zsh -i -c exit
zsh -i -c exit  0.62s user 0.39s system 93% cpu 1.078 total

This is my zsh zprof with chtf sourcing enabled:

num  calls                time                       self            name
-----------------------------------------------------------------------------------
 1)    1          24.43    24.43   82.70%     24.43    24.43   82.70%  compinit
 2)    2           3.67     1.84   12.44%      3.67     1.84   12.44%  promptinit
 3)    6           0.63     0.10    2.13%      0.63     0.10    2.13%  add-zsh-hook
 4)    1           0.35     0.35    1.18%      0.35     0.35    1.18%  select-word-style
 5)    1           0.94     0.94    3.19%      0.17     0.17    0.59%  set_prompt
 6)    1           1.09     1.09    3.68%      0.15     0.15    0.50%  prompt
 7)    2           0.14     0.07    0.47%      0.14     0.07    0.47%  prompt_suse_setup

-----------------------------------------------------------------------------------

 1)    1          24.43    24.43   82.70%     24.43    24.43   82.70%  compinit

-----------------------------------------------------------------------------------

       1/2         3.05     3.05   10.32%      3.05     3.05             promptinit [2]
 2)    2           3.67     1.84   12.44%      3.67     1.84   12.44%  promptinit
       1/2         3.05     3.05   10.32%      3.05     3.05             promptinit [2]

-----------------------------------------------------------------------------------

 6)    1           1.09     1.09    3.68%      0.15     0.15    0.50%  prompt
       1/1         0.94     0.94    3.19%      0.17     0.17             set_prompt [5]

-----------------------------------------------------------------------------------

       1/1         0.94     0.94    3.19%      0.17     0.17             prompt [6]
 5)    1           0.94     0.94    3.19%      0.17     0.17    0.59%  set_prompt
       1/2         0.14     0.14    0.47%      0.13     0.13             prompt_suse_setup [7]
       6/6         0.63     0.10    2.13%      0.63     0.10             add-zsh-hook [3]

-----------------------------------------------------------------------------------

       6/6         0.63     0.10    2.13%      0.63     0.10             set_prompt [5]
 3)    6           0.63     0.10    2.13%      0.63     0.10    2.13%  add-zsh-hook

-----------------------------------------------------------------------------------

 4)    1           0.35     0.35    1.18%      0.35     0.35    1.18%  select-word-style

-----------------------------------------------------------------------------------

       1/2         0.14     0.14    0.47%      0.13     0.13             set_prompt [5]
       1/2         0.01     0.01    0.03%      0.01     0.01             prompt_suse_setup [7]
 7)    2           0.14     0.07    0.47%      0.14     0.07    0.47%  prompt_suse_setup
       1/2         0.01     0.01    0.03%      0.01     0.01             prompt_suse_setup [7]

Without:

num  calls                time                       self            name
-----------------------------------------------------------------------------------
 1)    1          25.77    25.77   81.32%     25.77    25.77   81.32%  compinit
 2)    2           3.76     1.88   11.87%      3.76     1.88   11.87%  promptinit
 3)    6           0.89     0.15    2.82%      0.89     0.15    2.82%  add-zsh-hook
 4)    1           0.55     0.55    1.74%      0.55     0.55    1.74%  select-word-style
 5)    1           1.44     1.44    4.55%      0.33     0.33    1.05%  set_prompt
 6)    2           0.22     0.11    0.68%      0.22     0.11    0.68%  prompt_suse_setup
 7)    1           1.61     1.61    5.07%      0.16     0.16    0.51%  prompt

-----------------------------------------------------------------------------------

 1)    1          25.77    25.77   81.32%     25.77    25.77   81.32%  compinit

-----------------------------------------------------------------------------------

       1/2         3.15     3.15    9.94%      3.15     3.15             promptinit [2]
 2)    2           3.76     1.88   11.87%      3.76     1.88   11.87%  promptinit
       1/2         3.15     3.15    9.94%      3.15     3.15             promptinit [2]

-----------------------------------------------------------------------------------

 7)    1           1.61     1.61    5.07%      0.16     0.16    0.51%  prompt
       1/1         1.44     1.44    4.55%      0.33     0.33             set_prompt [5]

-----------------------------------------------------------------------------------

       1/1         1.44     1.44    4.55%      0.33     0.33             prompt [7]
 5)    1           1.44     1.44    4.55%      0.33     0.33    1.05%  set_prompt
       1/2         0.22     0.22    0.68%      0.21     0.21             prompt_suse_setup [6]
       6/6         0.89     0.15    2.82%      0.89     0.15             add-zsh-hook [3]

-----------------------------------------------------------------------------------

       6/6         0.89     0.15    2.82%      0.89     0.15             set_prompt [5]
 3)    6           0.89     0.15    2.82%      0.89     0.15    2.82%  add-zsh-hook

-----------------------------------------------------------------------------------

 4)    1           0.55     0.55    1.74%      0.55     0.55    1.74%  select-word-style

-----------------------------------------------------------------------------------

       1/2         0.22     0.22    0.68%      0.21     0.21             set_prompt [5]
       1/2         0.01     0.01    0.03%      0.01     0.01             prompt_suse_setup [6]
 6)    2           0.22     0.11    0.68%      0.22     0.11    0.68%  prompt_suse_setup
       1/2         0.01     0.01    0.03%      0.01     0.01             prompt_suse_setup [6]
mkozjak commented 3 years ago

Apparently, brew tap is hoggy here:

time brew tap
aws/tap
dteoh/devdocs
github/gh
homebrew/cask
homebrew/cask-drivers
homebrew/cask-fonts
homebrew/cask-versions
homebrew/core
vitorgalvao/tiny-scripts
yleisradio/terraforms
brew tap  0.54s user 0.34s system 94% cpu 0.936 total
tmatilai commented 3 years ago

Yes, brew tap is taking a while. I guess it could be possible to fetch the Tap list directly, but that would break if Homebrew changes its internal implementation.

But I have good news for you if the 1 second wait is too much. 😃 Set the environment variable CHTF_AUTO_INSTALL_METHOD to homebrew (or zip, but probably not your case), and the whole test is skipped.

I'll investigate the Taps listing anyway a bit. And suggestions to improve documentation for this are also welcome.

tmatilai commented 3 years ago

Wow, there is quite a lot of code in Homebrew! 😅

But I think it is safe to assume that the file hierarchy for Taps would be stable, so we can just look if the essential directory exists. I have a seemingly working solution, but I'll test it a bit more tomorrow with fresh eyes before making a pull request.

tmatilai commented 3 years ago

@mkozjak You can test #5 if you have time and energy.

make install PREFIX=$(brew --prefix) in the optimize_brew_tap_check branch should override the default Homebrew installed version. Of course you can also load the chtf.sh directly from the cloned repo, or install at other location.

mkozjak commented 3 years ago

@tmatilai Works nice and also brew --repo isn't too expensive on my end. Thanks!

time brew --repo
/usr/local/Homebrew
brew --repo  0.01s user 0.02s system 87% cpu 0.036 total
tmatilai commented 3 years ago

Yeah, brew --repo|caskroom|prefix are handled in Bash and don't load bunch of Ruby files like brew tap, so they are fast. We can't skip those, as Homebrew can be installed in custom paths, too. But setting environment variables is always an option.

I guess the loading the (mostly unneeded) libraries is what takes time in brew tap. And then iterating through directories, but that shouldn't be so bad. Anyway, for us it made unnecessary effort also by dropping the homebrew- prefixes and sorting, so I feel this is the right thing to do now. So thanks for the initiative!

I'll merge the PR soonish if there are no other comments. But I'll try to get one Fish installation improvement in before cutting the release.