Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 5 Mar 2015 00:42:07 +0000 (UTC)
From:      Devin Teske <dteske@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-10@freebsd.org
Subject:   svn commit: r279616 - in stable/10/usr.sbin/bsdconfig/timezone: . share
Message-ID:  <201503050042.t250g7AA090671@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: dteske
Date: Thu Mar  5 00:42:06 2015
New Revision: 279616
URL: https://svnweb.freebsd.org/changeset/base/279616

Log:
  MFC r278489: Eliminate sub-shells where possible for performance.

Modified:
  stable/10/usr.sbin/bsdconfig/timezone/share/continents.subr
  stable/10/usr.sbin/bsdconfig/timezone/share/countries.subr
  stable/10/usr.sbin/bsdconfig/timezone/timezone
Directory Properties:
  stable/10/   (props changed)

Modified: stable/10/usr.sbin/bsdconfig/timezone/share/continents.subr
==============================================================================
--- stable/10/usr.sbin/bsdconfig/timezone/share/continents.subr	Thu Mar  5 00:41:03 2015	(r279615)
+++ stable/10/usr.sbin/bsdconfig/timezone/share/continents.subr	Thu Mar  5 00:42:06 2015	(r279616)
@@ -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: stable/10/usr.sbin/bsdconfig/timezone/share/countries.subr
==============================================================================
--- stable/10/usr.sbin/bsdconfig/timezone/share/countries.subr	Thu Mar  5 00:41:03 2015	(r279615)
+++ stable/10/usr.sbin/bsdconfig/timezone/share/countries.subr	Thu Mar  5 00:42:06 2015	(r279616)
@@ -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: stable/10/usr.sbin/bsdconfig/timezone/timezone
==============================================================================
--- stable/10/usr.sbin/bsdconfig/timezone/timezone	Thu Mar  5 00:41:03 2015	(r279615)
+++ stable/10/usr.sbin/bsdconfig/timezone/timezone	Thu Mar  5 00:42:06 2015	(r279616)
@@ -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
@@ -277,8 +277,8 @@ f_make_menus         # creates $continen
 #
 # Launch application main menu
 #
-defaultctry=""
-defaultzone=""
+defaultctry=
+defaultzone=
 NEED_CONTINENT=1
 NEED_COUNTRY=1
 while :; do
@@ -296,10 +296,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
@@ -342,7 +342,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\" \
@@ -375,7 +375,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
@@ -384,12 +384,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
@@ -397,13 +397,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
 
@@ -432,10 +432,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



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201503050042.t250g7AA090671>