grep-devel
[Top][All Lists]
Advanced

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

[Grep-devel] [PATCH] grep: speed up -x with many patterns


From: Paul Eggert
Subject: [Grep-devel] [PATCH] grep: speed up -x with many patterns
Date: Sat, 31 Dec 2016 16:19:03 -0800

* src/kwsearch.c (Fcompile): Improve buffer allocation overhead
with -x and multiple patterns.  In the common case where '\n' is
the end-of-line byte, avoid copying other than the first and last
patterns.
---
 src/kwsearch.c | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)

diff --git a/src/kwsearch.c b/src/kwsearch.c
index fedbe32..a36381f 100644
--- a/src/kwsearch.c
+++ b/src/kwsearch.c
@@ -26,6 +26,8 @@ Fcompile (char const *pattern, size_t size, reg_syntax_t 
ignored)
 {
   kwset_t kwset;
   size_t total = size;
+  char *buf = NULL;
+  size_t bufalloc = 0;
 
   kwset = kwsinit (true);
 
@@ -46,23 +48,32 @@ Fcompile (char const *pattern, size_t size, reg_syntax_t 
ignored)
           total = 0;
         }
 
-      char *buf = NULL;
       if (match_lines)
         {
-          buf = xmalloc (len + 2);
-          buf[0] = eolbyte;
-          memcpy (buf + 1, p, len);
-          buf[len + 1] = eolbyte;
-          p = buf;
+          if (eolbyte == '\n' && pattern < p && sep)
+            p--;
+          else
+            {
+              if (bufalloc < len + 2)
+                {
+                  free (buf);
+                  bufalloc = len + 2;
+                  buf = x2realloc (NULL, &bufalloc);
+                  buf[0] = eolbyte;
+                }
+              memcpy (buf + 1, p, len);
+              buf[len + 1] = eolbyte;
+              p = buf;
+            }
           len += 2;
         }
       kwsincr (kwset, p, len);
-      free (buf);
 
       p = sep;
     }
   while (p);
 
+  free (buf);
   kwsprep (kwset);
 
   return kwset;
-- 
2.7.4




reply via email to

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