Subject: bug#21763: bug#22239: bug#22357: grep -f not only huge memory usage, but also huge time cost

From: Norihiro Tanaka <[email-masked]>
Date: Wed, 28 Dec 2016 09:21:02 +0900

On Mon, 26 Dec 2016 12:07:49 -0800
Paul Eggert <[email-masked]> wrote:

> Norihiro Tanaka wrote:
> > Hmm, how about the following test cases, although it is extreame?
> 
> I don't think we need to worry about performance for the case when -w
> is given, and a pattern matches data that contains non-word
> characters. In practice, such cases are rare. I expect that most
> users would be surprised that -w can match non-word characters, and
> that users wouldn't object to -w rejecting such matches (if this
> wouldn't hurt performance significantly).
> 
> While looking into this I did find a very small performance tweak for
> the test case, and installed the attached.

Thanks.

BTW, with multiple patterns in current master, former uses fgrep matcher,
and later users grep matcher.  I think that it is not reasonable.

  env LC_ALL=C grep -w -f pat inp

  env LC_ALL=C grep -F -w -f pat inp

So I wrote the patch to use fgrep matcher for both.
From 08d426d8a0bf8683b0e34ead2fd3561671414735 Mon Sep 17 00:00:00 2001
From: Norihiro Tanaka <[email-masked]>
Date: Wed, 28 Dec 2016 08:57:54 +0900
Subject: [PATCH] grep: imorove performance with multiple patterns

* src/grep.c (main): Avoid fgrep-to-grep conversion for word matching
with multiple patterns in single byte locales.
---
 src/grep.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)

diff --git a/src/grep.c b/src/grep.c
index aebab20..8cf522c 100644
--- a/src/grep.c
+++ b/src/grep.c
@@ -2862,7 +2862,7 @@ main (int argc, char **argv)
      for -iF.  */
   if (matcher == F_MATCHER_INDEX
       && (! localeinfo.multibyte
-          ? match_words
+          ? (n_patterns == 1 && match_words)
           : (contains_encoding_error (keys, keycc)
              || (match_icase && !fgrep_icase_available (keys, keycc)))))
     {
-- 
1.7.1