diff options
author | Andrew Gallant <jamslam@gmail.com> | 2018-06-23 20:18:56 -0400 |
---|---|---|
committer | Andrew Gallant <jamslam@gmail.com> | 2018-06-23 20:49:05 -0400 |
commit | 004bb35694a564e3f677003dddc97e455bcd7922 (patch) | |
tree | 808f7a3cab221f7b6de5362f69154e2800bc2e0d /src | |
parent | cd6c1909674648f13d9de16aa7413ea7ed240e49 (diff) |
ripgrep/printer: fix small performance regression
This commit removes an unconditional extra regex search that is in fact
not always necessary. This can result in a 2x performance improvement in
cases where ripgrep reports many matches.
The fix itself isn't ideal, but we continue to punt on cleaning up the
printer until it is rewritten for libripgrep, which is happening Real
Soon Now.
Fixes #955
Diffstat (limited to 'src')
-rw-r--r-- | src/printer.rs | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/src/printer.rs b/src/printer.rs index 721967e2..20fd1c4d 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -272,10 +272,14 @@ impl<W: WriteColor> Printer<W> { byte_offset: Option<u64> ) { if !self.line_per_match && !self.only_matching { - let mat = re - .find(&buf[start..end]) - .map(|m| (m.start(), m.end())) - .unwrap_or((0, 0)); + let mat = + if !self.needs_match() { + (0, 0) + } else { + re.find(&buf[start..end]) + .map(|m| (m.start(), m.end())) + .unwrap_or((0, 0)) + }; return self.write_match( re, path, buf, start, end, line_number, byte_offset, mat.0, mat.1); @@ -287,6 +291,12 @@ impl<W: WriteColor> Printer<W> { } } + fn needs_match(&self) -> bool { + self.column + || self.replace.is_some() + || self.only_matching + } + fn write_match<P: AsRef<Path>>( &mut self, re: &Regex, |