From a22980679ef993949bf443a9e255ce95a1147ea8 Mon Sep 17 00:00:00 2001 From: Norihiro Tanaka Date: Wed, 7 May 2014 17:55:45 +0900 Subject: [PATCH 1/2] grep: improve performance of -v when combined with -L, -l or -q MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Problem reported by Jörn Hees in: http://bugs.gnu.org/17427 * src/grep.c (grepbuf, grep): When -v is combined with -L, -l, or -q, don't read data unnecessarily after a non-match is found. --- src/grep.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/grep.c b/src/grep.c index a661fc0..9db74d6 100644 --- a/src/grep.c +++ b/src/grep.c @@ -1131,8 +1131,12 @@ grepbuf (char const *beg, char const *lim) prtext (p, b, &n); nlines += n; outleft -= n; - if (!outleft) - return nlines; + if (!outleft || done_on_match) + { + if (exit_on_match) + exit (EXIT_SUCCESS); + return nlines; + } } p = endp; } @@ -1141,6 +1145,8 @@ grepbuf (char const *beg, char const *lim) prtext (p, lim, &n); nlines += n; outleft -= n; + if (exit_on_match) + exit (EXIT_SUCCESS); } return nlines; } @@ -1219,8 +1225,7 @@ grep (int fd, struct stat const *st) nlines += grepbuf (beg, lim); if (pending) prpending (lim); - if ((!outleft && !pending) - || (nlines && done_on_match && !out_invert)) + if ((!outleft && !pending) || (nlines && done_on_match)) goto finish_grep; } -- 1.9.0