diff mbox series

[v3,10/10] generate-cmdlist.sh: don't parse command-list.txt thrice

Message ID patch-v3-10.10-100084070fd-20211105T135058Z-avarab@gmail.com (mailing list archive)
State Accepted
Commit 1cecd06e36452bfb8d074cda2083cc71ff089404
Headers show
Series generate-cmdlist.sh: make it (and "make") run faster | expand

Commit Message

Ævar Arnfjörð Bjarmason Nov. 5, 2021, 2:08 p.m. UTC
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>
---
 generate-cmdlist.sh | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)
diff mbox series

Patch

diff --git a/generate-cmdlist.sh b/generate-cmdlist.sh
index cfe0454d1de..205541e0f7f 100755
--- a/generate-cmdlist.sh
+++ b/generate-cmdlist.sh
@@ -26,7 +26,7 @@  command_list () {
 }
 
 category_list () {
-	command_list "$1" |
+	echo "$1" |
 	cut -d' ' -f2- |
 	tr ' ' '\012' |
 	grep -v '^$' |
@@ -37,7 +37,7 @@  define_categories () {
 	echo
 	echo "/* Command categories */"
 	bit=0
-	category_list "$1" |
+	echo "$1" |
 	while read cat
 	do
 		echo "#define CAT_$cat (1UL << $bit)"
@@ -51,7 +51,7 @@  define_category_names () {
 	echo "/* Category names */"
 	echo "static const char *category_names[] = {"
 	bit=0
-	category_list "$1" |
+	echo "$1" |
 	while read cat
 	do
 		echo "	\"$cat\", /* (1UL << $bit) */"
@@ -64,7 +64,7 @@  define_category_names () {
 print_command_list () {
 	echo "static struct cmdname_help command_list[] = {"
 
-	command_list "$1" |
+	echo "$1" |
 	while read cmd rest
 	do
 		synopsis=
@@ -93,6 +93,9 @@  do
 	shift
 done
 
+commands="$(command_list "$1")"
+categories="$(category_list "$commands")"
+
 echo "/* Automatically generated by generate-cmdlist.sh */
 struct cmdname_help {
 	const char *name;
@@ -100,8 +103,8 @@  struct cmdname_help {
 	uint32_t category;
 };
 "
-define_categories "$1"
+define_categories "$categories"
 echo
-define_category_names "$1"
+define_category_names "$categories"
 echo
-print_command_list "$1"
+print_command_list "$commands"