Skip to content

Commit e7429b9

Browse files
committed
Merge pull request #2847 from ddunbar/build-script-impl-optimize-setting-to-variable-lookups
[build-script-impl] Optimize setting -> variable name lookups.
2 parents 0f17b89 + 909d2ce commit e7429b9

File tree

1 file changed

+55
-24
lines changed

1 file changed

+55
-24
lines changed

utils/build-script-impl

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -646,26 +646,58 @@ function set_build_options_for_host() {
646646
)
647647
}
648648

649-
# Set up an "associative array" of settings for error checking, and set
650-
# (or unset) each corresponding variable to its default value
651-
# If the Mac's bash were not stuck in the past, we could "declare -A" an
652-
# associative array, but instead we have to hack it by defining variables
653-
# declare -A IS_KNOWN_SETTING
654-
for ((i = 0; i < ${#KNOWN_SETTINGS[@]}; i += 3)); do
655-
setting="${KNOWN_SETTINGS[i]}"
656-
657-
default_value="${KNOWN_SETTINGS[$((i+1))]}"
658-
659-
varname="$(to_varname "${setting}")" # upcase the setting name to get the variable
660-
eval "${varname}_IS_KNOWN_SETTING=1"
661-
662-
if [[ "${default_value}" ]] ; then
663-
# For an explanation of the backslash see http://stackoverflow.com/a/9715377
664-
eval ${varname}=$\default_value
665-
else
666-
unset ${varname}
667-
fi
668-
done
649+
# get_setting_varname(setting-name) -> variable-name
650+
#
651+
# Converts a known setting name, like "install-prefix" to an internal variable
652+
# name, like "INSTALL_PREFIX".
653+
#
654+
# \returns Empty if the setting is not a known one.
655+
function get_setting_varname() {
656+
local setting="$1"
657+
# Look up in the associative array built by configure_default_options().
658+
local setting_varname_var="${setting//-/_}_VARNAME"
659+
echo ${!setting_varname_var}
660+
}
661+
662+
function configure_default_options() {
663+
# Build a table of all of the known setting variables names.
664+
#
665+
# This is an optimization to do the argument to variable conversion (which is
666+
# slow) in a single pass.
667+
local all_settings=()
668+
for ((i = 0; i < ${#KNOWN_SETTINGS[@]}; i += 3)); do
669+
all_settings+=("${KNOWN_SETTINGS[i]}")
670+
done
671+
local known_setting_varnames=($(to_varname "${all_settings[*]}"))
672+
673+
# Build up an "associative array" mapping setting names to variable names
674+
# (we use this for error checking to identify "known options", and as a fast
675+
# way to map the setting name to a variable name). See get_setting_varname().
676+
#
677+
# This loop also sets (or unsets) each corresponding variable to its default
678+
# value.
679+
#
680+
# NOTE: If the Mac's bash were not stuck in the past, we could "declare -A"
681+
# an associative array, but instead we have to hack it by defining variables.
682+
for ((i = 0; i < ${#KNOWN_SETTINGS[@]}; i += 3)); do
683+
local setting="${KNOWN_SETTINGS[i]}"
684+
local default_value="${KNOWN_SETTINGS[$((i+1))]}"
685+
686+
# Find the variable name in our lookup table.
687+
local varname="${known_setting_varnames[$((i/3))]]}"
688+
689+
# Establish the associative array mapping.
690+
eval "${setting//-/_}_VARNAME=${varname}"
691+
692+
if [[ "${default_value}" ]] ; then
693+
# For an explanation of the backslash see http://stackoverflow.com/a/9715377
694+
eval ${varname}=$\default_value
695+
else
696+
unset ${varname}
697+
fi
698+
done
699+
}
700+
configure_default_options
669701

670702
COMMAND_NAME="$(basename "$0")"
671703

@@ -728,12 +760,11 @@ while [[ "$1" ]] ; do
728760
setting="${dashless%%=*}"
729761

730762
# compute the variable to set
731-
varname="$(to_varname "${setting}")"
763+
varname=$(get_setting_varname $setting)
732764

733765
# check to see if this is a known option
734-
known_var_name="${varname}_IS_KNOWN_SETTING"
735-
if [[ ! "${!known_var_name}" ]] ; then
736-
echo "Error: Unknown setting: ${setting}" 1>&2
766+
if [[ "${varname}" = "" ]] ; then
767+
echo "error: unknown setting: ${setting}" 1>&2
737768
usage 1>&2
738769
exit 1
739770
fi

0 commit comments

Comments
 (0)