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 17, 2021
1 parent a5f17db commit 9b69aa8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 27 deletions.
18 changes: 2 additions & 16 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 @@ -16,20 +14,15 @@ fn replace_path_separator(path: &str, new_path_separator: &str) -> String {
}

// TODO: this function is performance critical and can probably be optimized
pub fn print_entry(
stdout: &mut StdoutLock,
entry: &Path,
config: &Options,
wants_to_quit: &Arc<AtomicBool>,
) {
pub fn print_entry(stdout: &mut StdoutLock, entry: &Path, config: &Options) {
let path = if entry.is_absolute() {
entry
} else {
strip_current_dir(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 +44,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 +58,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
15 changes: 4 additions & 11 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 @@ -247,12 +245,7 @@ fn spawn_receiver(
{
// Flush the buffer
for v in &buffer {
output::print_entry(
&mut stdout,
v,
&config,
&wants_to_quit,
);
output::print_entry(&mut stdout, v, &config);
}
buffer.clear();

Expand All @@ -261,7 +254,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 +279,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 9b69aa8

Please sign in to comment.