Merge pull request #1749 from scop/perf

Use associative arrays for performance on bash >= 4
This commit is contained in:
Anton Petrov 2021-01-31 12:27:49 +03:00 committed by GitHub
commit 4c302a022d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 69 additions and 28 deletions

View file

@ -123,17 +123,19 @@ make_shims() {
done
}
registered_shims=" "
if ((${BASH_VERSINFO[0]} > 3)); then
declare -A registered_shims
# Registers the name of a shim to be generated.
register_shim() {
registered_shims="${registered_shims}${1} "
registered_shims["$1"]=1
}
# Install all the shims registered via `make_shims` or `register_shim` directly.
# Install all shims registered via `make_shims` or `register_shim` directly.
install_registered_shims() {
local shim file
for shim in $registered_shims; do
for shim in "${!registered_shims[@]}"; do
file="${SHIM_PATH}/${shim}"
[ -e "$file" ] || cp "$PROTOTYPE_SHIM_PATH" "$file"
done
@ -143,6 +145,31 @@ install_registered_shims() {
# over the contents of the shims directory. Any file that is present
# in the directory but has not been registered as a shim should be
# removed.
remove_stale_shims() {
local shim
for shim in "$SHIM_PATH"/*; do
if [[ ! ${registered_shims["${shim##*/}"]} ]]; then
rm -f "$shim"
fi
done
}
else # Same for bash < 4.
registered_shims=" "
register_shim() {
registered_shims="${registered_shims}${1} "
}
install_registered_shims() {
local shim file
for shim in $registered_shims; do
file="${SHIM_PATH}/${shim}"
[ -e "$file" ] || cp "$PROTOTYPE_SHIM_PATH" "$file"
done
}
remove_stale_shims() {
local shim
for shim in "$SHIM_PATH"/*; do
@ -151,6 +178,7 @@ remove_stale_shims() {
fi
done
}
fi
shopt -s nullglob

View file

@ -64,16 +64,27 @@ if [ -d "$versions_dir" ]; then
versions_dir="$(realpath "$versions_dir")"
fi
if ((${BASH_VERSINFO[0]} > 3)); then
declare -A current_versions
else
current_versions=()
fi
if [ -n "$bare" ]; then
hit_prefix=""
miss_prefix=""
current_versions=()
include_system=""
else
hit_prefix="* "
miss_prefix=" "
OLDIFS="$IFS"
IFS=: current_versions=($(pyenv-version-name || true))
IFS=:
if ((${BASH_VERSINFO[0]} > 3)); then
for i in $(pyenv-version-name || true); do
current_versions["$i"]="1"
done
else
current_versions=($(pyenv-version-name || true))
fi
IFS="$OLDIFS"
include_system="1"
fi
@ -93,7 +104,9 @@ exists() {
}
print_version() {
if exists "$1" "${current_versions[@]}"; then
if [[ ${BASH_VERSINFO[0]} -ge 4 && ${current_versions["$1"]} ]]; then
echo "${hit_prefix}$1 (set by $(pyenv-version-origin))"
elif (( ${BASH_VERSINFO[0]} < 3 )) && exists "$1" "${current_versions[@]}"; then
echo "${hit_prefix}$1 (set by $(pyenv-version-origin))"
else
echo "${miss_prefix}$1"