Subject: bug#24009: [PATCH] grep: use fastmap in regex

From: Norihiro Tanaka <[email-masked]>
Date: Sun, 17 Jul 2016 09:55:00 +0900

On Sat, 16 Jul 2016 22:06:53 +0200 (CEST)
"Jens Schleusener" <[email-masked]> wrote:

> Wow, that is a spectacular speed improvement. Since I use grep with
> regex patterns heavily in some of my scripts I could not resist to
> make some first simple tests (including your example pattern with a
> back reference). The non-representative results using grep 2.25 shows
> a gain of a factor 5-10 (while the unpatched self-compiled grep 2.25
> itself was already a factor 1.4-2.8 faster than the grep 2.16 offered
> by the OS (OpenSUSE Leap 42.1). At least in my tests all the grep
> outputs  were identical.

I believe that cases to speed up by this patch is not so much, as grep
makes a lot of other optimizations.  In fact, I spent a little time to
make a test case to demonstrate to speed up by this patch.  So I have an
interest with what kind of test cases you could confirm to speed up by
this patch.

> By the way I had to remove one of the two "=" in your patch otherwise
> gcc issued an error (but caution, I am a C-layman).

Thanks, I fixed it.  I made a mistake before sending the patch.  Of
course, "=" should be one.
From 1337006597a7d7e14993af14e57d47d6b483fb0d Mon Sep 17 00:00:00 2001
From: Norihiro Tanaka <[email-masked]>
Date: Sun, 17 Jul 2016 01:25:18 +0900
Subject: [PATCH] grep: use fastmap in regex

* src/dfasearch.c (GEAcompile): Use fastmap in regex.
---
 src/dfasearch.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)

diff --git a/src/dfasearch.c b/src/dfasearch.c
index 8052ef0..e5223e5 100644
--- a/src/dfasearch.c
+++ b/src/dfasearch.c
@@ -154,6 +154,9 @@ GEAcompile (char const *pattern, size_t size, reg_syntax_t syntax_bits)
       patterns = xnrealloc (patterns, pcount + 1, sizeof *patterns);
       patterns[pcount] = patterns0;
 
+      patterns[pcount].regexbuf.fastmap
+        = xmalloc ((UCHAR_MAX + 1) * sizeof (char));
+
       char const *err = re_compile_pattern (p, len,
                                             &(patterns[pcount].regexbuf));
       if (err)
-- 
1.7.1