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,34 +123,62 @@ make_shims() {
done done
} }
registered_shims=" " if ((${BASH_VERSINFO[0]} > 3)); then
# Registers the name of a shim to be generated. declare -A registered_shims
register_shim() {
registered_shims="${registered_shims}${1} "
}
# Install all the shims registered via `make_shims` or `register_shim` directly. # Registers the name of a shim to be generated.
install_registered_shims() { register_shim() {
local shim file registered_shims["$1"]=1
for shim in $registered_shims; do }
file="${SHIM_PATH}/${shim}"
[ -e "$file" ] || cp "$PROTOTYPE_SHIM_PATH" "$file"
done
}
# Once the registered shims have been installed, we make a second pass # Install all shims registered via `make_shims` or `register_shim` directly.
# over the contents of the shims directory. Any file that is present install_registered_shims() {
# in the directory but has not been registered as a shim should be local shim file
# removed. for shim in "${!registered_shims[@]}"; do
remove_stale_shims() { file="${SHIM_PATH}/${shim}"
local shim [ -e "$file" ] || cp "$PROTOTYPE_SHIM_PATH" "$file"
for shim in "$SHIM_PATH"/*; do done
if [[ "$registered_shims" != *" ${shim##*/} "* ]]; then }
rm -f "$shim"
fi # Once the registered shims have been installed, we make a second pass
done # 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
if [[ "$registered_shims" != *" ${shim##*/} "* ]]; then
rm -f "$shim"
fi
done
}
fi
shopt -s nullglob shopt -s nullglob

View file

@ -64,16 +64,27 @@ if [ -d "$versions_dir" ]; then
versions_dir="$(realpath "$versions_dir")" versions_dir="$(realpath "$versions_dir")"
fi fi
if ((${BASH_VERSINFO[0]} > 3)); then
declare -A current_versions
else
current_versions=()
fi
if [ -n "$bare" ]; then if [ -n "$bare" ]; then
hit_prefix="" hit_prefix=""
miss_prefix="" miss_prefix=""
current_versions=()
include_system="" include_system=""
else else
hit_prefix="* " hit_prefix="* "
miss_prefix=" " miss_prefix=" "
OLDIFS="$IFS" 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" IFS="$OLDIFS"
include_system="1" include_system="1"
fi fi
@ -93,7 +104,9 @@ exists() {
} }
print_version() { 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))" echo "${hit_prefix}$1 (set by $(pyenv-version-origin))"
else else
echo "${miss_prefix}$1" echo "${miss_prefix}$1"