1
0
mirror of https://github.com/BurntSushi/ripgrep.git synced 2025-04-19 09:02:15 +02:00

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
This commit is contained in:
Andrew Gallant 2018-06-23 20:18:56 -04:00
parent cd6c190967
commit 004bb35694

View File

@ -272,10 +272,14 @@ impl<W: WriteColor> Printer<W> {
byte_offset: Option<u64> byte_offset: Option<u64>
) { ) {
if !self.line_per_match && !self.only_matching { if !self.line_per_match && !self.only_matching {
let mat = re let mat =
.find(&buf[start..end]) if !self.needs_match() {
.map(|m| (m.start(), m.end())) (0, 0)
.unwrap_or((0, 0)); } else {
re.find(&buf[start..end])
.map(|m| (m.start(), m.end()))
.unwrap_or((0, 0))
};
return self.write_match( return self.write_match(
re, path, buf, start, end, line_number, re, path, buf, start, end, line_number,
byte_offset, mat.0, mat.1); 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>>( fn write_match<P: AsRef<Path>>(
&mut self, &mut self,
re: &Regex, re: &Regex,