generate-cmdlist.sh: don't parse command-list.txt thrice

Change the "define_categories()" and "define_category_names()" functions to take the already-parsed output of "category_list()" as an argument, which brings our number of passes over "command-list.txt" from three to two. Then have "category_list()" itself take the output of "command_list()" as an argument, bringing the number of times we parse the file to one. Compared to the pre-image this speeds us up quite a bit: $ git show HEAD~:generate-cmdlist.sh >generate-cmdlist.sh.old $ hyperfine --warmup 10 -L v ,.old 'sh generate-cmdlist.sh{v} command-list.txt' Benchmark #1: sh generate-cmdlist.sh command-list.txt Time (mean ± σ): 22.9 ms ± 0.3 ms [User: 15.8 ms, System: 9.6 ms] Range (min … max): 22.5 ms … 24.0 ms 125 runs Benchmark #2: sh generate-cmdlist.sh.old command-list.txt Time (mean ± σ): 30.1 ms ± 0.4 ms [User: 24.4 ms, System: 17.5 ms] Range (min … max): 29.5 ms … 32.3 ms 96 runs Summary 'sh generate-cmdlist.sh command-list.txt' ran 1.32 ± 0.02 times faster than 'sh generate-cmdlist.sh.old command-list.txt' Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Ævar Arnfjörð Bjarmason committed Nov 5, 2021 at 15:08 UTC 1cecd06e36452bfb8d074cda2083cc71ff089404
1 file changed +10 -7
generate-cmdlist.sh
+10 -7
@@ -26,7 +26,7 @@ command_list () {
26 }
27
28 category_list () {
29 - command_list "$1" |
29 + echo "$1" |
30 cut -d' ' -f2- |
31 tr ' ' '\012' |
32 grep -v '^$' |
@@ -37,7 +37,7 @@ define_categories () {
37 echo
38 echo "/* Command categories */"
39 bit=0
40 - category_list "$1" |
40 + echo "$1" |
41 while read cat
42 do
43 echo "#define CAT_$cat (1UL << $bit)"
@@ -51,7 +51,7 @@ define_category_names () {
51 echo "/* Category names */"
52 echo "static const char *category_names[] = {"
53 bit=0
54 - category_list "$1" |
54 + echo "$1" |
55 while read cat
56 do
57 echo " \"$cat\", /* (1UL << $bit) */"
@@ -64,7 +64,7 @@ define_category_names () {
64 print_command_list () {
65 echo "static struct cmdname_help command_list[] = {"
66
67 - command_list "$1" |
67 + echo "$1" |
68 while read cmd rest
69 do
70 synopsis=
@@ -93,6 +93,9 @@ do
93 shift
94 done
95
96 +commands="$(command_list "$1")"
97 +categories="$(category_list "$commands")"
98 +
99 echo "/* Automatically generated by generate-cmdlist.sh */
100 struct cmdname_help {
101 const char *name;
@@ -100,8 +103,8 @@ struct cmdname_help {
103 uint32_t category;
104 };
105 "
103 -define_categories "$1"
106 +define_categories "$categories"
107 echo
105 -define_category_names "$1"
108 +define_category_names "$categories"
109 echo
107 -print_command_list "$1"
110 +print_command_list "$commands"