grep: short-circuit -v to bail out on first match
A small optimization. There is no need to try matching the current input line against any further patterns if a match was already found and -v is specified. function old new delta grep_file 1463 1440 -23 Signed-off-by: Ari Sundholm <ari@tuxera.com> Signed-off-by: Niko Vähäsarja <niko@tuxera.com> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
parent
9a9c6e39ba
commit
d4b568c108
@ -443,15 +443,23 @@ static int grep_file(FILE *file)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* If it's non-inverted search, we can stop
|
/* If it's a non-inverted search, we can stop
|
||||||
* at first match */
|
* at first match and report it.
|
||||||
if (found && !invert_search)
|
* If it's an inverted search, we can move on
|
||||||
goto do_found;
|
* to the next line of input, ignoring the
|
||||||
|
* rest of the patterns.
|
||||||
|
*/
|
||||||
|
if (found) {
|
||||||
|
//if (invert_search)
|
||||||
|
// goto do_not_found;
|
||||||
|
//goto do_found;
|
||||||
|
break; // this accomplishes both
|
||||||
|
}
|
||||||
pattern_ptr = pattern_ptr->link;
|
pattern_ptr = pattern_ptr->link;
|
||||||
} /* while (pattern_ptr) */
|
} /* while (pattern_ptr) */
|
||||||
|
|
||||||
if (found ^ invert_search) {
|
if (found ^ invert_search) {
|
||||||
do_found:
|
//do_found:
|
||||||
/* keep track of matches */
|
/* keep track of matches */
|
||||||
nmatches++;
|
nmatches++;
|
||||||
|
|
||||||
@ -552,6 +560,7 @@ static int grep_file(FILE *file)
|
|||||||
}
|
}
|
||||||
#if ENABLE_FEATURE_GREP_CONTEXT
|
#if ENABLE_FEATURE_GREP_CONTEXT
|
||||||
else { /* no match */
|
else { /* no match */
|
||||||
|
//do_not_found:
|
||||||
/* if we need to print some context lines after the last match, do so */
|
/* if we need to print some context lines after the last match, do so */
|
||||||
if (print_n_lines_after) {
|
if (print_n_lines_after) {
|
||||||
print_line(line, strlen(line), linenum, '-');
|
print_line(line, strlen(line), linenum, '-');
|
||||||
|
Loading…
Reference in New Issue
Block a user