Skip to content

Commit

Permalink
Don't check wants_to_quit while printing paths
Browse files Browse the repository at this point in the history
The colorized path printing code was checking wants_to_quit on every
component, since each one might require a slow stat() call.  Rip this
out since the implementation is about to be optimized to avoid those
calls.
  • Loading branch information
tavianator committed Sep 16, 2021
1 parent a5f17db commit 881735e
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 17 deletions.
12 changes: 1 addition & 11 deletions src/output.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
use std::io::{self, StdoutLock, Write};
use std::path::Path;
use std::process;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;

use lscolors::{LsColors, Style};

Expand All @@ -20,7 +18,6 @@ pub fn print_entry(
stdout: &mut StdoutLock,
entry: &Path,
config: &Options,
wants_to_quit: &Arc<AtomicBool>,
) {
let path = if entry.is_absolute() {
entry
Expand All @@ -29,7 +26,7 @@ pub fn print_entry(
};

let r = if let Some(ref ls_colors) = config.ls_colors {
print_entry_colorized(stdout, path, config, ls_colors, wants_to_quit)
print_entry_colorized(stdout, path, config, ls_colors)
} else {
print_entry_uncolorized(stdout, path, config)
};
Expand All @@ -51,7 +48,6 @@ fn print_entry_colorized(
path: &Path,
config: &Options,
ls_colors: &LsColors,
wants_to_quit: &Arc<AtomicBool>,
) -> io::Result<()> {
let default_style = ansi_term::Style::default();

Expand All @@ -66,12 +62,6 @@ fn print_entry_colorized(
*path_string.to_mut() = replace_path_separator(&path_string, separator);
}
write!(stdout, "{}", style.paint(path_string))?;

// TODO: can we move this out of the if-statement? Why do we call it that often?
if wants_to_quit.load(Ordering::Relaxed) {
writeln!(stdout)?;
process::exit(ExitCode::KilledBySigint.into());
}
}

if config.null_separator {
Expand Down
9 changes: 3 additions & 6 deletions src/walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ pub fn scan(path_vec: &[PathBuf], pattern: Arc<Regex>, config: Arc<Options>) ->
}

// Spawn the thread that receives all results through the channel.
let receiver_thread = spawn_receiver(&config, &wants_to_quit, rx);
let receiver_thread = spawn_receiver(&config, rx);

// Spawn the sender threads.
spawn_senders(&config, &wants_to_quit, pattern, parallel_walker, tx);
Expand All @@ -162,11 +162,9 @@ pub fn scan(path_vec: &[PathBuf], pattern: Arc<Regex>, config: Arc<Options>) ->

fn spawn_receiver(
config: &Arc<Options>,
wants_to_quit: &Arc<AtomicBool>,
rx: Receiver<WorkerResult>,
) -> thread::JoinHandle<ExitCode> {
let config = Arc::clone(config);
let wants_to_quit = Arc::clone(wants_to_quit);

let show_filesystem_errors = config.show_filesystem_errors;
let threads = config.threads;
Expand Down Expand Up @@ -251,7 +249,6 @@ fn spawn_receiver(
&mut stdout,
v,
&config,
&wants_to_quit,
);
}
buffer.clear();
Expand All @@ -261,7 +258,7 @@ fn spawn_receiver(
}
}
ReceiverMode::Streaming => {
output::print_entry(&mut stdout, &value, &config, &wants_to_quit);
output::print_entry(&mut stdout, &value, &config);
}
}

Expand All @@ -286,7 +283,7 @@ fn spawn_receiver(
if !buffer.is_empty() {
buffer.sort();
for value in buffer {
output::print_entry(&mut stdout, &value, &config, &wants_to_quit);
output::print_entry(&mut stdout, &value, &config);
}
}

Expand Down

0 comments on commit 881735e

Please sign in to comment.