summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNorihiro Tanaka <noritnk@kcn.ne.jp>2019-01-13 07:53:32 +0900
committerJim Meyering <meyering@fb.com>2019-12-19 07:20:02 -0800
commitbe9224d9719633674d92b1f2eaed4729dba19eb9 (patch)
tree6f50d01a4726d25a0b9b277350cd385971f0a2f6
parent0cd76e7aee143c494c9c7b5abb59dfd635df108e (diff)
downloadgrep-be9224d9719633674d92b1f2eaed4729dba19eb9.tar.gz
grep: speed up multiple word matching
grep uses its KWset matcher for multiple word matching, but that is very slow when most of the parts matched to a pattern are not words. So, if the first match to a pattern is not a word, use the grep matcher to match for its line. Note that when START_PTR is set, the grep matcher uses the regex matcher which is very slow to match words. Therefore, we use the grep matcher when only START_PTR is NULL. * src/kwsearch.c (Fexecute): If an initial match is incomplete because not on a word boundary, use the grep matcher to find a matching line.
-rw-r--r--src/kwsearch.c18
1 files changed, 18 insertions, 0 deletions
diff --git a/src/kwsearch.c b/src/kwsearch.c
index f121816e..76443502 100644
--- a/src/kwsearch.c
+++ b/src/kwsearch.c
@@ -250,6 +250,23 @@ Fexecute (void *vcp, char const *buf, size_t size, size_t *match_size,
else
goto success;
}
+ if (!start_ptr && !localeinfo.multibyte)
+ {
+ if (! kwsearch->re)
+ {
+ fgrep_to_grep_pattern (&kwsearch->pattern, &kwsearch->size);
+ kwsearch->re = GEAcompile (kwsearch->pattern,
+ kwsearch->size,
+ RE_SYNTAX_GREP);
+ }
+ end = memchr (beg + len, eol, (buf + size) - (beg + len));
+ end = end ? end + 1 : buf + size;
+ if (EGexecute (kwsearch->re, beg, end - beg, match_size, NULL)
+ != (size_t) -1)
+ goto success_match_words;
+ beg = end - 1;
+ break;
+ }
if (!len)
break;
offset = kwsexec (kwset, beg, --len, &kwsmatch, true);
@@ -270,6 +287,7 @@ Fexecute (void *vcp, char const *buf, size_t size, size_t *match_size,
success:
end = memchr (beg + len, eol, (buf + size) - (beg + len));
end = end ? end + 1 : buf + size;
+ success_match_words:
beg = memrchr (buf, eol, beg - buf);
beg = beg ? beg + 1 : buf;
len = end - beg;