diff options
author | Ari Sundholm <ari@tuxera.com> | 2019-01-28 19:41:12 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2019-01-29 14:53:22 +0100 |
commit | d4b568c108b89fb726181b0fabb19f2d62bc1930 (patch) | |
tree | d3c2a54c68123890c055d334a1cb41623abc2f9f | |
parent | 9a9c6e39ba4c8f4f2cecb147c7e6464bec2d8b55 (diff) | |
download | busybox-w32-d4b568c108b89fb726181b0fabb19f2d62bc1930.tar.gz busybox-w32-d4b568c108b89fb726181b0fabb19f2d62bc1930.tar.bz2 busybox-w32-d4b568c108b89fb726181b0fabb19f2d62bc1930.zip |
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>
-rw-r--r-- | findutils/grep.c | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/findutils/grep.c b/findutils/grep.c index 9d9da422c..2cbe7ea91 100644 --- a/findutils/grep.c +++ b/findutils/grep.c | |||
@@ -443,15 +443,23 @@ static int grep_file(FILE *file) | |||
443 | } | 443 | } |
444 | } | 444 | } |
445 | } | 445 | } |
446 | /* If it's non-inverted search, we can stop | 446 | /* If it's a non-inverted search, we can stop |
447 | * at first match */ | 447 | * at first match and report it. |
448 | if (found && !invert_search) | 448 | * If it's an inverted search, we can move on |
449 | goto do_found; | 449 | * to the next line of input, ignoring the |
450 | * rest of the patterns. | ||
451 | */ | ||
452 | if (found) { | ||
453 | //if (invert_search) | ||
454 | // goto do_not_found; | ||
455 | //goto do_found; | ||
456 | break; // this accomplishes both | ||
457 | } | ||
450 | pattern_ptr = pattern_ptr->link; | 458 | pattern_ptr = pattern_ptr->link; |
451 | } /* while (pattern_ptr) */ | 459 | } /* while (pattern_ptr) */ |
452 | 460 | ||
453 | if (found ^ invert_search) { | 461 | if (found ^ invert_search) { |
454 | do_found: | 462 | //do_found: |
455 | /* keep track of matches */ | 463 | /* keep track of matches */ |
456 | nmatches++; | 464 | nmatches++; |
457 | 465 | ||
@@ -552,6 +560,7 @@ static int grep_file(FILE *file) | |||
552 | } | 560 | } |
553 | #if ENABLE_FEATURE_GREP_CONTEXT | 561 | #if ENABLE_FEATURE_GREP_CONTEXT |
554 | else { /* no match */ | 562 | else { /* no match */ |
563 | //do_not_found: | ||
555 | /* if we need to print some context lines after the last match, do so */ | 564 | /* if we need to print some context lines after the last match, do so */ |
556 | if (print_n_lines_after) { | 565 | if (print_n_lines_after) { |
557 | print_line(line, strlen(line), linenum, '-'); | 566 | print_line(line, strlen(line), linenum, '-'); |