bug-grep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

grep is slow with huge before-contexts


From: Gary Wong
Subject: grep is slow with huge before-contexts
Date: Mon, 7 Jan 2008 12:41:11 -0500
User-agent: Mutt/1.4.2.2i

Hi,

CVS grep is slower than it needs to be if the leading context is
ridiculously large and matches are found.  For instance, try:

    grep -B 1000000000 / /etc/shells

The problem seems to be that the first loop in prtext() is almost
useless when out_before is much higher than the buffered context.
It can be fixed by moving the interior test out a level:

Index: src/grep.c
===================================================================
RCS file: /sources/grep/grep/src/grep.c,v
retrieving revision 1.125
diff -u -r1.125 grep.c
--- src/grep.c  10 Oct 2007 04:29:47 -0000      1.125
+++ src/grep.c  7 Jan 2008 17:25:41 -0000
@@ -974,11 +974,10 @@
       /* Deal with leading context crap. */
 
       bp = lastout ? lastout : bufbeg;
-      for (i = 0; i < out_before; ++i)
-       if (p > bp)
-         do
-           --p;
-         while (p[-1] != eol);
+      for (i = 0; i < out_before && p > bp; ++i)
+       do
+         --p;
+       while (p[-1] != eol);
 
       /* We print the SEP_STR_GROUP separator only if our output is
         discontiguous from the last output in the file. */

Cheers,
Gary.
-- 
     Gary Wong           address@hidden           http://cs-people.bu.edu/gtw/

Attachment: pgp60QP123D6D.pgp
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]