From df9bbd7ab3be2ae44b598b25bd9d49bdf62cd809 Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Thu, 27 Dec 2012 17:08:54 -0600 Subject: [PATCH] Speed up rbenv-rehash with a simpler indexing approach --- libexec/rbenv-rehash | 58 ++++++++++---------------------------------- 1 file changed, 13 insertions(+), 45 deletions(-) diff --git a/libexec/rbenv-rehash b/libexec/rbenv-rehash index eebc4d3c..a1d79864 100755 --- a/libexec/rbenv-rehash +++ b/libexec/rbenv-rehash @@ -55,58 +55,27 @@ make_shims() { 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_index="" # We will keep track of shims registered for installation with the -# global `reigstered_shims` array and with a global variable for each -# shim. The array will let us iterate over all registered shims. The -# global variables will let us quickly check whether a shim with the -# given name has been registered or not. +# global `reigstered_shims` array and with a global search index +# string. The array will let us iterate over all registered shims. The +# index string will let us quickly check whether a shim with the given +# name has been registered or not. register_shim() { local shim="$@" - local var="$(shim_variable_name "$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