Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add buffering to stdout when it's not a terminal. #885

Merged
merged 4 commits into from
Nov 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
- Add opposing command-line options, see #595 (@Asha20)
- Add support for more filesystem indicators in `LS_COLORS`, see
https://github.com/sharkdp/lscolors/pull/35 (@tavianator)
- Buffer writing to stdout if stdout is not a TTY, to improve performance.

## Bugfixes

Expand Down
4 changes: 2 additions & 2 deletions src/exec/job.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub fn job(
// Obtain the next result from the receiver, else if the channel
// has closed, exit from the loop
let value: PathBuf = match lock.recv() {
Ok(WorkerResult::Entry(val)) => val,
Ok(WorkerResult::Entry(path)) => path,
Ok(WorkerResult::Error(err)) => {
if show_filesystem_errors {
print_error(err.to_string());
Expand All @@ -53,7 +53,7 @@ pub fn batch(
limit: usize,
) -> ExitCode {
let paths = rx.iter().filter_map(|value| match value {
WorkerResult::Entry(val) => Some(val),
WorkerResult::Entry(path) => Some(path),
WorkerResult::Error(err) => {
if show_filesystem_errors {
print_error(err.to_string());
Expand Down
29 changes: 15 additions & 14 deletions src/output.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::borrow::Cow;
use std::io::{self, StdoutLock, Write};
use std::io::{self, Write};
use std::path::Path;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;

use lscolors::{Indicator, LsColors, Style};

Expand All @@ -16,11 +15,11 @@ 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,
pub fn print_entry<W: Write>(
stdout: &mut W,
entry: &Path,
config: &Config,
wants_to_quit: &Arc<AtomicBool>,
wants_to_quit: &AtomicBool,
) {
let path = if config.strip_cwd_prefix {
strip_current_dir(entry)
Expand All @@ -46,12 +45,12 @@ pub fn print_entry(
}

// TODO: this function is performance critical and can probably be optimized
fn print_entry_colorized(
stdout: &mut StdoutLock,
fn print_entry_colorized<W: Write>(
stdout: &mut W,
path: &Path,
config: &Config,
ls_colors: &LsColors,
wants_to_quit: &Arc<AtomicBool>,
wants_to_quit: &AtomicBool,
) -> io::Result<()> {
// Split the path between the parent and the last component
let mut offset = 0;
Expand Down Expand Up @@ -94,15 +93,17 @@ fn print_entry_colorized(
}

if wants_to_quit.load(Ordering::Relaxed) {
// Ignore any errors on flush, because we're about to exit anyway
let _ = stdout.flush();
ExitCode::KilledBySigint.exit();
}

Ok(())
}

// TODO: this function is performance critical and can probably be optimized
fn print_entry_uncolorized_base(
stdout: &mut StdoutLock,
fn print_entry_uncolorized_base<W: Write>(
stdout: &mut W,
path: &Path,
config: &Config,
) -> io::Result<()> {
Expand All @@ -116,17 +117,17 @@ fn print_entry_uncolorized_base(
}

#[cfg(not(unix))]
fn print_entry_uncolorized(
stdout: &mut StdoutLock,
fn print_entry_uncolorized<W: Write>(
stdout: &mut W,
path: &Path,
config: &Config,
) -> io::Result<()> {
print_entry_uncolorized_base(stdout, path, config)
}

#[cfg(unix)]
fn print_entry_uncolorized(
stdout: &mut StdoutLock,
fn print_entry_uncolorized<W: Write>(
stdout: &mut W,
path: &Path,
config: &Config,
) -> io::Result<()> {
Expand Down
29 changes: 18 additions & 11 deletions src/walk.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::borrow::Cow;
use std::ffi::OsStr;
use std::fs::{FileType, Metadata};
use std::io;
Expand All @@ -8,6 +7,7 @@ use std::sync::mpsc::{channel, Receiver, Sender};
use std::sync::{Arc, Mutex};
use std::thread;
use std::time;
use std::{borrow::Cow, io::Write};

use anyhow::{anyhow, Result};
use ignore::overrides::OverrideBuilder;
Expand Down Expand Up @@ -220,51 +220,58 @@ fn spawn_receiver(
} else {
let start = time::Instant::now();

let mut buffer = vec![];

// Start in buffering mode
let mut mode = ReceiverMode::Buffering;

// Maximum time to wait before we start streaming to the console.
let max_buffer_time = config.max_buffer_time.unwrap_or(DEFAULT_MAX_BUFFER_TIME);

let stdout = io::stdout();
let mut stdout = stdout.lock();
let stdout = stdout.lock();
let mut stdout = io::BufWriter::new(stdout);

let mut num_results = 0;

let is_interactive = config.interactive_terminal;
let mut buffer = Vec::with_capacity(MAX_BUFFER_LENGTH);
for worker_result in rx {
match worker_result {
WorkerResult::Entry(value) => {
WorkerResult::Entry(path) => {
if config.quiet {
return ExitCode::HasResults(true);
}

match mode {
ReceiverMode::Buffering => {
buffer.push(value);
buffer.push(path);

// Have we reached the maximum buffer size or maximum buffering time?
if buffer.len() > MAX_BUFFER_LENGTH
|| start.elapsed() > max_buffer_time
{
// Flush the buffer
for v in &buffer {
for path in &buffer {
output::print_entry(
&mut stdout,
v,
path,
&config,
&wants_to_quit,
);
}
buffer.clear();

if is_interactive && stdout.flush().is_err() {
// Probably a broken pipe. Exit gracefully.
return ExitCode::GeneralError;
}
// Start streaming
mode = ReceiverMode::Streaming;
}
}
ReceiverMode::Streaming => {
output::print_entry(&mut stdout, &value, &config, &wants_to_quit);
output::print_entry(&mut stdout, &path, &config, &wants_to_quit);
if is_interactive && stdout.flush().is_err() {
// Probably a broken pipe. Exit gracefully.
return ExitCode::GeneralError;
}
}
}

tmccombs marked this conversation as resolved.
Show resolved Hide resolved
Expand Down