Speed up rbenv-rehash with a simpler indexing approach

This commit is contained in:
Sam Stephenson 2012-12-27 17:08:54 -06:00
parent c3fe192243
commit df9bbd7ab3

View file

@ -55,58 +55,27 @@ make_shims() {
done done
} }
# Create an empty array for the list of registered shims. # Create an empty array for the list of registered shims and an empty
# string to use as a search index.
registered_shims=() registered_shims=()
registered_shims_index=""
# We will keep track of shims registered for installation with the # We will keep track of shims registered for installation with the
# global `reigstered_shims` array and with a global variable for each # global `reigstered_shims` array and with a global search index
# shim. The array will let us iterate over all registered shims. The # string. The array will let us iterate over all registered shims. The
# global variables will let us quickly check whether a shim with the # index string will let us quickly check whether a shim with the given
# given name has been registered or not. # name has been registered or not.
register_shim() { register_shim() {
local shim="$@" local shim="$@"
local var="$(shim_variable_name "$shim")" registered_shims["${#registered_shims[@]}"]="$shim"
registered_shims_index="$registered_shims_index/$shim/"
if [ -z "${!var}" ]; then
registered_shims[${#registered_shims[*]}]="$shim"
eval "${var}=1"
fi
}
# To compute the global variable name for a given shim we must first
# escape any non-alphanumeric characters. If the shim name is
# alphanumeric (including a hyphen or underscore) we can take a
# shorter path. Otherwise, we must iterate over each character and
# escape the non-alphanumeric ones using `printf`.
shim_variable_name() {
local shim="$1"
local result="_shim_"
if [[ ! "$shim" =~ [^[:alnum:]_-] ]]; then
shim="${shim//_/_5f}"
shim="${shim//-/_2d}"
result="$result$shim"
else
local length="${#shim}"
local char i
for ((i=0; i<length; i++)); do
char="${shim:$i:1}"
if [[ "$char" =~ [[:alnum:]] ]]; then
result="$result$char"
else
result="$result$(printf "_%02x" \'"$char")"
fi
done
fi
echo "$result"
} }
# To install all the registered shims, we iterate over the # To install all the registered shims, we iterate over the
# `registered_shims` array and create a link if one does not already # `registered_shims` array and create a link if one does not already
# exist. # exist.
install_registered_shims() { install_registered_shims() {
local shim
for shim in "${registered_shims[@]}"; do for shim in "${registered_shims[@]}"; do
[ -e "$shim" ] || ln -f "$PROTOTYPE_SHIM_PATH" "$shim" [ -e "$shim" ] || ln -f "$PROTOTYPE_SHIM_PATH" "$shim"
done done
@ -117,11 +86,10 @@ install_registered_shims() {
# in the directory but has not been registered as a shim should be # in the directory but has not been registered as a shim should be
# removed. # removed.
remove_stale_shims() { remove_stale_shims() {
local var local shim
for shim in *; do for shim in *; do
var="$(shim_variable_name "$shim")" if [[ "$registered_shims_index" != *"/$shim/"* ]]; then
if [ -z "${!var}" ]; then rm -f "$shim"
rm -f "$shim"
fi fi
done done
} }