Author: dteske
Date: Tue Feb 10 02:55:10 2015
New Revision: 278489
URL: https://svnweb.freebsd.org/changeset/base/278489

Log:
  Eliminate sub-shells where possible for performance.
  
  MFC after:    7 days

Modified:
  head/usr.sbin/bsdconfig/timezone/share/continents.subr
  head/usr.sbin/bsdconfig/timezone/share/countries.subr
  head/usr.sbin/bsdconfig/timezone/timezone

Modified: head/usr.sbin/bsdconfig/timezone/share/continents.subr
==============================================================================
--- head/usr.sbin/bsdconfig/timezone/share/continents.subr      Tue Feb 10 
02:53:26 2015        (r278488)
+++ head/usr.sbin/bsdconfig/timezone/share/continents.subr      Tue Feb 10 
02:55:10 2015        (r278489)
@@ -1,6 +1,6 @@
 if [ ! "$_TIMEZONE_CONTINENTS_SUBR" ]; then _TIMEZONE_CONTINENTS_SUBR=1
 #
-# Copyright (c) 2011-2012 Devin Teske
+# Copyright (c) 2011-2015 Devin Teske
 # All rights reserved.
 #
 # Redistribution and use in source and binary forms, with or without
@@ -87,7 +87,7 @@ export continent_utc_title
 
 ############################################################ FUNCTIONS
 
-# f_continent $cont $property
+# f_continent $cont $property [$var_to_set]
 #
 # Returns a single property of a given continent. Available properties are:
 #
@@ -102,37 +102,60 @@ export continent_utc_title
 #                  (which appears after continent selection).
 #      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()
 {
-       local cont="$1" property="$2"
-       eval echo \"\${continent_${cont}_$property}\"
+       f_getvar "continent_${1}_$2" $3
 }
 
-# f_find_continent $title
+# f_find_continent $title [$var_to_set]
 #
 # 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()
 {
-       local cont
-       for cont in $CONTINENTS; do
-               if [ "$1" = "$( f_continent $cont title )" ]; then
-                       echo "$cont"
+       local __cont __title
+       for __cont in $CONTINENTS; do
+               f_continent $__cont title __title
+               if [ "$1" = "$__title" ]; then
+                       if [ "$2" ]; then
+                               setvar "$2" $__cont
+                       else
+                               echo "$__cont"
+                       fi
                        return $SUCCESS
                fi
        done
        return $FAILURE
 }
 
-# f_OCEANP $cont
+# f_OCEANP $cont [$var_to_set]
 #
 # 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()
 {
        case "$1" in
        arctic|atlantic|indian|pacific)
-               echo 1
+               if [ "$2" ]; then
+                       setvar "$2" 1
+               else
+                       echo 1
+               fi
+               ;;
+       *)
+               [ "$2" ] && setvar "$2" ""
        esac
 }
 

Modified: head/usr.sbin/bsdconfig/timezone/share/countries.subr
==============================================================================
--- head/usr.sbin/bsdconfig/timezone/share/countries.subr       Tue Feb 10 
02:53:26 2015        (r278488)
+++ head/usr.sbin/bsdconfig/timezone/share/countries.subr       Tue Feb 10 
02:55:10 2015        (r278489)
@@ -1,6 +1,6 @@
 if [ ! "$_TIMEZONE_COUNTRIES_SUBR" ]; then _TIMEZONE_COUNTRIES_SUBR=1
 #
-# Copyright (c) 2011-2012 Devin Teske
+# Copyright (c) 2011-2015 Devin Teske
 # All rights reserved.
 #
 # Redistribution and use in source and binary forms, with or without
@@ -25,8 +25,10 @@ if [ ! "$_TIMEZONE_COUNTRIES_SUBR" ]; th
 # SUCH DAMAGE.
 #
 # $FreeBSD$
+#
+############################################################ FUNCTIONS
 
-# f_country $code $property
+# f_country $code $property [$var_to_set]
 #
 # Returns a single property of a given country. Available properties are:
 #
@@ -44,10 +46,13 @@ if [ ! "$_TIMEZONE_COUNTRIES_SUBR" ]; th
 #      descr_N      Like name, but for the Nth zone when the country has
 #                   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()
 {
-       local code="$1" property="$2"
-       eval echo \"\${country_${code}_$property}\"
+       f_getvar "country_${1}_$2" $3
 }
 
 # f_sort_countries
@@ -59,22 +64,42 @@ f_country()
 # afterward is the sh(1) function which utilizes the below awk script.
 #
 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)
        {
                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()
 {
-       COUNTRIES=$( echo "$COUNTRIES" | awk "$f_sort_countries_awk" |
-                       sort | awk '{print $NF}' )
-       export COUNTRIES
+       export COUNTRIES # for awk(1) ENVIRON[] visibility
+       COUNTRIES=$( awk "$f_sort_countries_awk" )
+       export COUNTRIES # Pedantic
 }
 
+############################################################ MAIN
+
 f_dprintf "%s: Successfully loaded." timezone/countries.subr
 
 fi # ! $_TIMEZONE_COUNTRIES_SUBR

Modified: head/usr.sbin/bsdconfig/timezone/timezone
==============================================================================
--- head/usr.sbin/bsdconfig/timezone/timezone   Tue Feb 10 02:53:26 2015        
(r278488)
+++ head/usr.sbin/bsdconfig/timezone/timezone   Tue Feb 10 02:55:10 2015        
(r278489)
@@ -1,6 +1,6 @@
 #!/bin/sh
 #-
-# Copyright (c) 2011-2013 Devin Teske
+# Copyright (c) 2011-2015 Devin Teske
 # All rights reserved.
 #
 # Redistribution and use in source and binary forms, with or without
@@ -280,8 +280,8 @@ f_make_menus         # creates $continen
 #
 # Launch application main menu
 #
-defaultctry=""
-defaultzone=""
+defaultctry=
+defaultzone=
 NEED_CONTINENT=1
 NEED_COUNTRY=1
 while :; do
@@ -299,10 +299,10 @@ while :; do
 
                continent=$( eval f_dialog_menutag2item \"\$mtag\" \
                                        $continent_menu_list )
-               cont=$( f_find_continent "$continent" )
-               cont_title=$( f_continent $cont title )
-               nitems=$( f_continent $cont nitems )
-               isocean=$( f_OCEANP $cont )
+               f_find_continent "$continent" cont
+               f_continent $cont title cont_title
+               f_continent $cont nitems nitems
+               f_OCEANP $cont isocean
        fi
 
        if [ "$NEED_COUNTRY" ]; then
@@ -345,7 +345,7 @@ while :; do
                        #
                        # 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 \
                                                \"\$title\"  \
                                                \"\$btitle\" \
@@ -378,7 +378,7 @@ while :; do
                fi
 
                # Get the country code from the user's selection 
-               tlc=$( f_continent $cont tlc_$tag )
+               f_continent $cont tlc_$tag tlc
 
                NEED_COUNTRY=
        fi
@@ -387,12 +387,12 @@ while :; do
        # If the selection has only one zone (nzones == -1),
        # just set it.
        #
-       nzones=$( f_country $tlc nzones )
+       f_country $tlc nzones nzones
        if [ $nzones -lt 0 ]; then
-               real_cont=$( f_country $tlc cont )
-               real_continent=$( f_continent $real_cont name )
-               name=$( f_country $tlc name )
-               filename=$( f_country $tlc filename )
+               f_country $tlc cont real_cont
+               f_continent $real_cont name real_continent
+               f_country $tlc name name
+               f_country $tlc filename filename
 
                if ! f_confirm_zone "$real_continent/$filename"; then
                        [ $nitems -eq 1 ] && NEED_CONTINENT=1
@@ -400,13 +400,13 @@ while :; do
                        continue
                fi
        else
-               f_sprintf title "$msg_country_time_zones" \
-                               "$( f_country $tlc name )"
+               f_country $tlc name name
+               f_sprintf title "$msg_country_time_zones" "$name"
                f_dialog_title "$title"
                title="$DIALOG_TITLE" btitle="$DIALOG_BACKTITLE"
                f_dialog_title_restore
                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 \
                        \"\$title\"  \"\$btitle\" \"\$prompt\" \"\" $menu_list
 
@@ -435,10 +435,10 @@ while :; do
                        continue
                fi
 
-               real_cont=$( f_country $tlc cont_$n )
-               real_continent=$( f_continent $real_cont name )
-               name=$( f_country $tlc name )
-               filename=$( f_country $tlc filename_$n )
+               f_country $tlc cont_$n real_cont
+               f_continent $real_cont name real_continent
+               f_country $tlc name name
+               f_country $tlc filename_$n filename
 
                f_confirm_zone "$real_continent/$filename" || continue
        fi
_______________________________________________
svn-src-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to