From cd6c1909674648f13d9de16aa7413ea7ed240e49 Mon Sep 17 00:00:00 2001 From: Andrew Gallant Date: Sat, 23 Jun 2018 20:17:29 -0400 Subject: ripgrep: use new BufferedStandardStream from termcolor Specifically, this will use a buffered writer when not printing to a tty. This fixes a long standing performance regression where ripgrep would slow down dramatically if it needed to report a lot of matches. Fixes #955 --- src/args.rs | 9 +++++++-- src/main.rs | 12 ++++-------- 2 files changed, 11 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/args.rs b/src/args.rs index 0c231d0f..e410cd7c 100644 --- a/src/args.rs +++ b/src/args.rs @@ -228,8 +228,13 @@ impl Args { } /// Create a new writer for single-threaded searching with color support. - pub fn stdout(&self) -> termcolor::StandardStream { - termcolor::StandardStream::stdout(self.color_choice) + pub fn stdout(&self) -> Box { + if atty::is(atty::Stream::Stdout) { + Box::new(termcolor::StandardStream::stdout(self.color_choice)) + } else { + Box::new( + termcolor::BufferedStandardStream::stdout(self.color_choice)) + } } /// Returns a handle to stdout for filtering search. diff --git a/src/main.rs b/src/main.rs index e374338b..6f010135 100644 --- a/src/main.rs +++ b/src/main.rs @@ -165,8 +165,7 @@ fn run_parallel(args: &Arc) -> Result { fn run_one_thread(args: &Arc) -> Result { let start_time = Instant::now(); - let stdout = args.stdout(); - let mut stdout = stdout.lock(); + let mut stdout = args.stdout(); let mut worker = args.worker(); let mut paths_searched: u64 = 0; let mut match_line_count = 0; @@ -223,8 +222,7 @@ fn run_files_parallel(args: Arc) -> Result { let print_args = Arc::clone(&args); let (tx, rx) = mpsc::channel::(); let print_thread = thread::spawn(move || { - let stdout = print_args.stdout(); - let mut printer = print_args.printer(stdout.lock()); + let mut printer = print_args.printer(print_args.stdout()); let mut file_count = 0; for dent in rx.iter() { if !print_args.quiet() { @@ -254,8 +252,7 @@ fn run_files_parallel(args: Arc) -> Result { } fn run_files_one_thread(args: &Arc) -> Result { - let stdout = args.stdout(); - let mut printer = args.printer(stdout.lock()); + let mut printer = args.printer(args.stdout()); let mut file_count = 0; for result in args.walker() { let dent = match get_or_log_dir_entry( @@ -277,8 +274,7 @@ fn run_files_one_thread(args: &Arc) -> Result { } fn run_types(args: &Arc) -> Result { - let stdout = args.stdout(); - let mut printer = args.printer(stdout.lock()); + let mut printer = args.printer(args.stdout()); let mut ty_count = 0; for def in args.type_defs() { printer.type_def(def); -- cgit v1.2.3