command_util: grep source files only once.
authorAndre Noll <maan@systemlinux.org>
Sun, 15 Apr 2012 05:25:22 +0000 (07:25 +0200)
committerAndre Noll <maan@systemlinux.org>
Fri, 29 Jun 2012 19:36:31 +0000 (21:36 +0200)
Currently we grep once for each template member. This patch speeds
up the header mode of command_util.sh by a factor of 3.

command_util.sh

index 2bceda4..b37c5b0 100755 (executable)
@@ -165,15 +165,17 @@ com_man()
 
 make_proto()
 {
-       local source_file match="" CR='
+       local regex='\(__noreturn \)*\(static \)*int com_'
+       local source_file match="" all_commands CR='
 '
        if test -n "$prototype"; then
                result="$prototype$CR"
                return
        fi
+       all_commands="$(cat $source_files | grep "$regex")"
        result=
        for source_file in $source_files; do
-               match=$(grep "^\(__noreturn \)*\(static \)*int com_$name_txt(" $source_file | head -n 1 | sed -e 's/$/;/1')
+               match=$(grep "$regex$name_txt(" <<< "$all_commands" | head -n 1 | sed -e 's/$/;/1')
                if test -n "$match"; then
                        result="$result$match$CR"
                        break
@@ -216,15 +218,12 @@ template_loop()
        local t_source_files="$source_files"
        local member
        for member in $template_members; do
-               local sed_cmd="sed -e s/@member@/$member/g"
-               #echo "sed_cmd: $sed_cmd"
-               name_txt=$(echo "$t_name" | $sed_cmd)
-               #name_txt="$tname"
-               perms_txt=$(echo "$t_perms" | $sed_cmd)
-               desc_txt=$(echo "$t_desc" | $sed_cmd)
-               usage_txt=$(echo "$t_usage" | $sed_cmd)
-               help_txt=$(printf "%s\n" "$t_help" | $sed_cmd)
-               prototype=$(echo "$template_prototype" | $sed_cmd)
+               name_txt="${t_name//@member@/$member}"
+               perms_txt="${t_perms//@member@/$member}"
+               desc_txt="${t_desc//@member@/$member}"
+               usage_txt="${t_usage//@member@/$member}"
+               help_txt="${t_help//@member@/$member}"
+               prototype="${template_prototype//@member@/$member}"
                result=
                $1
                loop_result="$loop_result$result"