Skip to content

Instantly share code, notes, and snippets.

@shayelkin
Created May 1, 2024 17:47
Show Gist options
  • Save shayelkin/348413a4e9d042cdd538e61dcaf00873 to your computer and use it in GitHub Desktop.
Save shayelkin/348413a4e9d042cdd538e61dcaf00873 to your computer and use it in GitHub Desktop.
Speed up goenv init by caching it
#!/bin/sh
# Running "eval $(goenv init -)" can take more than 100ms on a recent Macbook, most of that time spent
# on generating the commands, rather than evaluating them. But those commands are the same every time,
# so this generates those once, and caches it to disk. It doesn't check for freshness though, so it is
# prudent to delete ~/.goenv.cached.init.sh after updating goenv.
# To use: replace the call to "eval $(goenv init -)" with the following code
_cached_goenv_init() {
local goenv_init_sh=$HOME/.goenv.cached.init.sh
if [ ! -x "$goenv_init_sh" ]; then
echo "$goenv_init_sh, caching \"goenv init -\""
local goenv_version=$(git -C $(dirname $(realpath $(command -v goenv))) rev-parse HEAD)
echo "# cached for goenv $goenv_version" > $goenv_init_sh
goenv init - --no-rehash | head -n -1 >> $goenv_init_sh
goenv sh-rehash --only-manage-paths >> $goenv_init_sh
chmod +x $goenv_init_sh
fi
. $goenv_init_sh
}
_cached_goenv_init
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment