From 004bb35694a564e3f677003dddc97e455bcd7922 Mon Sep 17 00:00:00 2001 From: Andrew Gallant Date: Sat, 23 Jun 2018 20:18:56 -0400 Subject: [PATCH] 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 --- src/printer.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/printer.rs b/src/printer.rs index 721967e..20fd1c4 100644 --- a/src/printer.rs +++ b/src/printer.rs @@ -272,10 +272,14 @@ impl Printer { byte_offset: Option ) { 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 Printer { } } + fn needs_match(&self) -> bool { + self.column + || self.replace.is_some() + || self.only_matching + } + fn write_match>( &mut self, re: &Regex,