summaryrefslogtreecommitdiff
path: root/src/grep.c
diff options
context:
space:
mode:
authorNorihiro Tanaka <noritnk@kcn.ne.jp>2020-09-20 15:39:13 +0900
committerJim Meyering <meyering@fb.com>2020-09-22 08:46:26 -0700
commitae65513edc80a1b65f19264b9bed95d870602967 (patch)
tree73f431df76c3a3b4f9572f27d1cdf79c1d49f3da /src/grep.c
parent34ada37baac651312b0e30092c086833a7223df6 (diff)
downloadgrep-ae65513edc80a1b65f19264b9bed95d870602967.tar.gz
grep: avoid unnecessary regex compilation
Grep resorts to using the regex engine when the precision of either -o or --color is required, or when the pattern is not supported by our DFA engine (e.g., backref). Otherwise, grep would perform regex compilation solely to check the syntax. This change makes grep skip that compilation in the common case for which it is unnecessary. The compilation we are avoiding is quite costly, consuming O(N^2) RSS for N regular expressions. * src/dfasearch.c (GEAcompile): Add new argument, and avoid unneeded compilation of regex. * src/grep.c (compile_fp_t): Update prototype. (main): Update caller. * src/kwsearch.c (Fcompile): Update caller and add new argument. * src/pcresearch.c (Pcompile): Add new argument. * src/search.h (GEAcompile, Fcompile, Pcompile): Update prototype.
Diffstat (limited to 'src/grep.c')
-rw-r--r--src/grep.c7
1 files changed, 4 insertions, 3 deletions
diff --git a/src/grep.c b/src/grep.c
index 3b40bbb7..1453b14a 100644
--- a/src/grep.c
+++ b/src/grep.c
@@ -635,7 +635,7 @@ static bool seek_failed;
static bool seek_data_failed;
/* Functions we'll use to search. */
-typedef void *(*compile_fp_t) (char *, size_t, reg_syntax_t);
+typedef void *(*compile_fp_t) (char *, size_t, reg_syntax_t, bool);
typedef size_t (*execute_fp_t) (void *, char const *, size_t, size_t *,
char const *);
static execute_fp_t execute;
@@ -2973,8 +2973,9 @@ main (int argc, char **argv)
matcher = try_fgrep_pattern (matcher, keys, &keycc);
execute = matchers[matcher].execute;
- compiled_pattern = matchers[matcher].compile (keys, keycc,
- matchers[matcher].syntax);
+ compiled_pattern =
+ matchers[matcher].compile (keys, keycc, matchers[matcher].syntax,
+ only_matching | color_option);
/* We need one byte prior and one after. */
char eolbytes[3] = { 0, eolbyte, 0 };
size_t match_size;