]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
[gdb/contrib] Speed up spellcheck.sh --check
authorTom de Vries <tdevries@suse.de>
Mon, 21 Oct 2024 13:07:02 +0000 (15:07 +0200)
committerTom de Vries <tdevries@suse.de>
Mon, 21 Oct 2024 13:07:02 +0000 (15:07 +0200)
Speed up gdb/contrib/shellcheck.sh by caching the grep pattern.

Without cached grep pattern:
...
$ time ./gdb/contrib/spellcheck.sh --check gdb/gdb.c

real    0m2,750s
user    0m0,013s
sys     0m0,032s
...
and with cached grep pattern:
...
$ time ./gdb/contrib/spellcheck.sh --check gdb/gdb.c

real    0m0,192s
user    0m0,022s
sys     0m0,024s
...

Tested on aarch64-linux.

gdb/contrib/spellcheck.sh

index 3188734331c6ba5ca2dcd6bd9538101476706ca1..08c745aa92dd3fb5d0041213d8c317f92f8c5b0b 100755 (executable)
 # $ ./gdb/contrib/spellcheck.sh gdb*
 
 scriptdir=$(cd "$(dirname "$0")" || exit; pwd -P)
+this_script=$scriptdir/$(basename "$0")
 
 url=https://en.wikipedia.org/wiki/Wikipedia:Lists_of_common_misspellings/For_machines
 cache_dir=$scriptdir/../../.git
 cache_file=wikipedia-common-misspellings.txt
 dictionary=$cache_dir/$cache_file
 local_dictionary=$scriptdir/common-misspellings.txt
+cache_file2=spell-check.pat1
 
 # Separators: space, slash, tab, colon, comma.
 declare -a grep_separators
@@ -191,21 +193,38 @@ parse_dictionary ()
 
 find_files_matching_words ()
 {
-    local pat
-    pat=""
-    for word in "${words[@]}"; do
-       if [ "$pat" = "" ]; then
-           pat="$word"
-       else
-           pat="$pat|$word"
-       fi
-    done
-    pat="($pat)"
-
-    local sep
-    sep=$grep_separator
-
-    pat="(^|$sep)$pat($sep|$)"
+    local cache_id
+    cache_id=$(cat "$local_dictionary" "$dictionary" "$this_script" \
+                | md5sum  \
+                | awk '{print $1}')
+
+    local patfile
+    patfile="$cache_dir/$cache_file2".$cache_id
+
+    if [ -f "$patfile" ]; then
+       pat=$(cat "$patfile")
+    else
+       rm -f "$cache_dir/$cache_file2".*
+
+       local pat
+       pat=""
+       for word in "${words[@]}"; do
+           if [ "$pat" = "" ]; then
+               pat="$word"
+           else
+               pat="$pat|$word"
+           fi
+       done
+       pat="($pat)"
+
+       local sep
+       sep=$grep_separator
+
+       pat="(^|$sep)$pat($sep|$)"
+
+       echo "$pat" \
+            > "$patfile"
+    fi
 
     grep -E \
        -l \