Eliminate sub-shells where possible for performance.

MFC after:	7 days
This commit is contained in:
dteske 2015-02-10 02:55:10 +00:00
parent 806324e567
commit 081cb3dc34
3 changed files with 89 additions and 41 deletions

View File

@ -1,6 +1,6 @@
if [ ! "$_TIMEZONE_CONTINENTS_SUBR" ]; then _TIMEZONE_CONTINENTS_SUBR=1 if [ ! "$_TIMEZONE_CONTINENTS_SUBR" ]; then _TIMEZONE_CONTINENTS_SUBR=1
# #
# Copyright (c) 2011-2012 Devin Teske # Copyright (c) 2011-2015 Devin Teske
# All rights reserved. # All rights reserved.
# #
# Redistribution and use in source and binary forms, with or without # Redistribution and use in source and binary forms, with or without
@ -87,7 +87,7 @@ export continent_utc_title
############################################################ FUNCTIONS ############################################################ FUNCTIONS
# f_continent $cont $property # f_continent $cont $property [$var_to_set]
# #
# Returns a single property of a given continent. Available properties are: # Returns a single property of a given continent. Available properties are:
# #
@ -102,37 +102,60 @@ export continent_utc_title
# (which appears after continent selection). # (which appears after continent selection).
# menu_list Menu-list of regions for this continent. # menu_list Menu-list of regions for this continent.
# #
# If $var_to_set is missing or NULL, the value of $var_to_get is printed to
# standard output for capturing in a sub-shell (which is less-recommended
# because of performance degredation; for example, when called in a loop).
#
f_continent() f_continent()
{ {
local cont="$1" property="$2" f_getvar "continent_${1}_$2" $3
eval echo \"\${continent_${cont}_$property}\"
} }
# f_find_continent $title # f_find_continent $title [$var_to_set]
# #
# Returns continent identifier given continent title. # Returns continent identifier given continent title.
# #
# If $var_to_set is missing or NULL, the value of $var_to_get is printed to
# standard output for capturing in a sub-shell (which is less-recommended
# because of performance degredation; for example, when called in a loop).
#
f_find_continent() f_find_continent()
{ {
local cont local __cont __title
for cont in $CONTINENTS; do for __cont in $CONTINENTS; do
if [ "$1" = "$( f_continent $cont title )" ]; then f_continent $__cont title __title
echo "$cont" if [ "$1" = "$__title" ]; then
if [ "$2" ]; then
setvar "$2" $__cont
else
echo "$__cont"
fi
return $SUCCESS return $SUCCESS
fi fi
done done
return $FAILURE return $FAILURE
} }
# f_OCEANP $cont # f_OCEANP $cont [$var_to_set]
# #
# Returns "1" if the first argument is an ocean, otherwise NULL. # Returns "1" if the first argument is an ocean, otherwise NULL.
# #
# If $var_to_set is missing or NULL, the value of $var_to_get is printed to
# standard output for capturing in a sub-shell (which is less-recommended
# because of performance degredation; for example, when called in a loop).
#
f_OCEANP() f_OCEANP()
{ {
case "$1" in case "$1" in
arctic|atlantic|indian|pacific) arctic|atlantic|indian|pacific)
echo 1 if [ "$2" ]; then
setvar "$2" 1
else
echo 1
fi
;;
*)
[ "$2" ] && setvar "$2" ""
esac esac
} }

View File

@ -1,6 +1,6 @@
if [ ! "$_TIMEZONE_COUNTRIES_SUBR" ]; then _TIMEZONE_COUNTRIES_SUBR=1 if [ ! "$_TIMEZONE_COUNTRIES_SUBR" ]; then _TIMEZONE_COUNTRIES_SUBR=1
# #
# Copyright (c) 2011-2012 Devin Teske # Copyright (c) 2011-2015 Devin Teske
# All rights reserved. # All rights reserved.
# #
# Redistribution and use in source and binary forms, with or without # Redistribution and use in source and binary forms, with or without
@ -25,8 +25,10 @@ if [ ! "$_TIMEZONE_COUNTRIES_SUBR" ]; then _TIMEZONE_COUNTRIES_SUBR=1
# SUCH DAMAGE. # SUCH DAMAGE.
# #
# $FreeBSD$ # $FreeBSD$
#
############################################################ FUNCTIONS
# f_country $code $property # f_country $code $property [$var_to_set]
# #
# Returns a single property of a given country. Available properties are: # Returns a single property of a given country. Available properties are:
# #
@ -44,10 +46,13 @@ if [ ! "$_TIMEZONE_COUNTRIES_SUBR" ]; then _TIMEZONE_COUNTRIES_SUBR=1
# descr_N Like name, but for the Nth zone when the country has # descr_N Like name, but for the Nth zone when the country has
# multiple zones (nzones > 0) # multiple zones (nzones > 0)
# #
# If $var_to_set is missing or NULL, the value of $var_to_get is printed to
# standard output for capturing in a sub-shell (which is less-recommended
# because of performance degredation; for example, when called in a loop).
#
f_country() f_country()
{ {
local code="$1" property="$2" f_getvar "country_${1}_$2" $3
eval echo \"\${country_${code}_$property}\"
} }
# f_sort_countries # f_sort_countries
@ -59,22 +64,42 @@ f_country()
# afterward is the sh(1) function which utilizes the below awk script. # afterward is the sh(1) function which utilizes the below awk script.
# #
f_sort_countries_awk=' f_sort_countries_awk='
function _asorti(src, dest)
{ {
split($0, array, /[[:space:]]+/) k = nitems = 0
for (i in src) dest[++nitems] = i
for (i = 1; i <= nitems; k = i++) {
idx = dest[i]
while ((k > 0) && (dest[k] > idx)) {
dest[k+1] = dest[k]; k--
}
dest[k+1] = idx
}
return nitems
}
BEGIN {
split(ENVIRON["COUNTRIES"], array, /[[:space:]]+/)
for (item in array) for (item in array)
{ {
tlc = array[item] tlc = array[item]
print ENVIRON["country_" tlc "_name"] " " tlc name = ENVIRON["country_" tlc "_name"]
countries[name] = tlc
} }
n = _asorti(countries, sorted_countries)
for (i = 1; i <= n; i++)
print countries[sorted_countries[i]]
exit
} }
' '
f_sort_countries() f_sort_countries()
{ {
COUNTRIES=$( echo "$COUNTRIES" | awk "$f_sort_countries_awk" | export COUNTRIES # for awk(1) ENVIRON[] visibility
sort | awk '{print $NF}' ) COUNTRIES=$( awk "$f_sort_countries_awk" )
export COUNTRIES export COUNTRIES # Pedantic
} }
############################################################ MAIN
f_dprintf "%s: Successfully loaded." timezone/countries.subr f_dprintf "%s: Successfully loaded." timezone/countries.subr
fi # ! $_TIMEZONE_COUNTRIES_SUBR fi # ! $_TIMEZONE_COUNTRIES_SUBR

View File

@ -1,6 +1,6 @@
#!/bin/sh #!/bin/sh
#- #-
# Copyright (c) 2011-2013 Devin Teske # Copyright (c) 2011-2015 Devin Teske
# All rights reserved. # All rights reserved.
# #
# Redistribution and use in source and binary forms, with or without # Redistribution and use in source and binary forms, with or without
@ -280,8 +280,8 @@ f_make_menus # creates $continent_menu_list and $continent_*_menu_list
# #
# Launch application main menu # Launch application main menu
# #
defaultctry="" defaultctry=
defaultzone="" defaultzone=
NEED_CONTINENT=1 NEED_CONTINENT=1
NEED_COUNTRY=1 NEED_COUNTRY=1
while :; do while :; do
@ -299,10 +299,10 @@ while :; do
continent=$( eval f_dialog_menutag2item \"\$mtag\" \ continent=$( eval f_dialog_menutag2item \"\$mtag\" \
$continent_menu_list ) $continent_menu_list )
cont=$( f_find_continent "$continent" ) f_find_continent "$continent" cont
cont_title=$( f_continent $cont title ) f_continent $cont title cont_title
nitems=$( f_continent $cont nitems ) f_continent $cont nitems nitems
isocean=$( f_OCEANP $cont ) f_OCEANP $cont isocean
fi fi
if [ "$NEED_COUNTRY" ]; then if [ "$NEED_COUNTRY" ]; then
@ -345,7 +345,7 @@ while :; do
# #
# Calculate size of menu # Calculate size of menu
# #
menu_list=$( f_continent $cont menu_list ) f_continent $cont menu_list menu_list
eval f_dialog_menu_size height width rows \ eval f_dialog_menu_size height width rows \
\"\$title\" \ \"\$title\" \
\"\$btitle\" \ \"\$btitle\" \
@ -378,7 +378,7 @@ while :; do
fi fi
# Get the country code from the user's selection # Get the country code from the user's selection
tlc=$( f_continent $cont tlc_$tag ) f_continent $cont tlc_$tag tlc
NEED_COUNTRY= NEED_COUNTRY=
fi fi
@ -387,12 +387,12 @@ while :; do
# If the selection has only one zone (nzones == -1), # If the selection has only one zone (nzones == -1),
# just set it. # just set it.
# #
nzones=$( f_country $tlc nzones ) f_country $tlc nzones nzones
if [ $nzones -lt 0 ]; then if [ $nzones -lt 0 ]; then
real_cont=$( f_country $tlc cont ) f_country $tlc cont real_cont
real_continent=$( f_continent $real_cont name ) f_continent $real_cont name real_continent
name=$( f_country $tlc name ) f_country $tlc name name
filename=$( f_country $tlc filename ) f_country $tlc filename filename
if ! f_confirm_zone "$real_continent/$filename"; then if ! f_confirm_zone "$real_continent/$filename"; then
[ $nitems -eq 1 ] && NEED_CONTINENT=1 [ $nitems -eq 1 ] && NEED_CONTINENT=1
@ -400,13 +400,13 @@ while :; do
continue continue
fi fi
else else
f_sprintf title "$msg_country_time_zones" \ f_country $tlc name name
"$( f_country $tlc name )" f_sprintf title "$msg_country_time_zones" "$name"
f_dialog_title "$title" f_dialog_title "$title"
title="$DIALOG_TITLE" btitle="$DIALOG_BACKTITLE" title="$DIALOG_TITLE" btitle="$DIALOG_BACKTITLE"
f_dialog_title_restore f_dialog_title_restore
prompt="$msg_select_zone" prompt="$msg_select_zone"
menu_list=$( f_country $tlc menu_list ) f_country $tlc menu_list menu_list
eval f_dialog_menu_size height width rows \ eval f_dialog_menu_size height width rows \
\"\$title\" \"\$btitle\" \"\$prompt\" \"\" $menu_list \"\$title\" \"\$btitle\" \"\$prompt\" \"\" $menu_list
@ -435,10 +435,10 @@ while :; do
continue continue
fi fi
real_cont=$( f_country $tlc cont_$n ) f_country $tlc cont_$n real_cont
real_continent=$( f_continent $real_cont name ) f_continent $real_cont name real_continent
name=$( f_country $tlc name ) f_country $tlc name name
filename=$( f_country $tlc filename_$n ) f_country $tlc filename_$n filename
f_confirm_zone "$real_continent/$filename" || continue f_confirm_zone "$real_continent/$filename" || continue
fi fi