Skip to content

Commit

Permalink
Use grep to avoid slow dash read
Browse files Browse the repository at this point in the history
'while read line ... < /file' is very slow with dash, because it
reads character by character.

The change here is to change config_check to incur the cost of a
single 'grep' fork.  That way, grep is able to do the matching.
Grep is much faster than dash (or bash) at filtering the non-matching
lines.

LP: #2030729
  • Loading branch information
smoser committed Aug 9, 2023
1 parent 7e80239 commit 1c18965
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions tools/ds-identify
Original file line number Diff line number Diff line change
Expand Up @@ -777,21 +777,25 @@ check_config() {
if [ "$1" = "$files" -a ! -f "$1" ]; then
return 1
fi
local fname="" line="" ret="" found=0 found_fn=""
# shellcheck disable=2094
for fname in "$@"; do
[ -f "$fname" ] || continue
while read line; do
line=${line%%#*}
case "$line" in
$key:\ *|"${key}":)
ret=${line#*:};
ret=${ret# };
found=$((found+1))
found_fn="$fname";;
esac
done <"$fname"
local line="" ret="" found=0 found_fn="" oifs="$IFS" out=""
debug 1 "looking for '$key' in $# files: $*"
out=$(grep "$key\"\?:" "$@" 2>/dev/null)
IFS=${CR}
for line in $out; do
# drop '# comment'
line=${line%%#*}
# if more than one file was 'grep'ed, then grep will output filename:
# but if only one file, line will not be prefixed.
if [ $# -eq 1 ]; then
found_fn="$1"
else
found_fn="${line%%:*}"
line=${line#*:}
fi
ret=${line#*: };
found=$((found+1))
done
IFS="$oifs"
if [ $found -ne 0 ]; then
_RET="$ret"
_RET_fname="$found_fn"
Expand Down

0 comments on commit 1c18965

Please sign in to comment.