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

Significantly improve performance by using a buffered writer #3101

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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 @@ -44,6 +44,7 @@
- Use bat's ANSI iterator during tab expansion, see #2998 (@eth-p)
- Support 'statically linked binary' for aarch64 in 'Release' page, see #2992 (@tzq0301)
- Update options in shell completions and the man page of `bat`, see #2995 (@akinomyoga)
- Significantly improve performance by using a buffered writer, see #3101 (@MoSal)

## Syntaxes

Expand Down
5 changes: 4 additions & 1 deletion src/bin/bat/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,13 +338,16 @@ impl App {
return Err("Must be one file name per input type.".into());
}

let stdin_is_terminal = || std::io::stdin().is_terminal();

let mut filenames_or_none: Box<dyn Iterator<Item = Option<&Path>>> = match filenames {
Some(filenames) => Box::new(filenames.into_iter().map(Some)),
None => Box::new(std::iter::repeat(None)),
};
if files.is_none() {
return Ok(vec![new_stdin_input(
filenames_or_none.next().unwrap_or(None),
stdin_is_terminal(),
)]);
}
let files_or_none: Box<dyn Iterator<Item = _>> = match files {
Expand All @@ -356,7 +359,7 @@ impl App {
for (filepath, provided_name) in files_or_none.zip(filenames_or_none) {
if let Some(filepath) = filepath {
if filepath.to_str().unwrap_or_default() == "-" {
file_input.push(new_stdin_input(provided_name));
file_input.push(new_stdin_input(provided_name, stdin_is_terminal()));
} else {
file_input.push(new_file_input(filepath, provided_name));
}
Expand Down
4 changes: 2 additions & 2 deletions src/bin/bat/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ pub fn new_file_input<'a>(file: &'a Path, name: Option<&'a Path>) -> Input<'a> {
named(Input::ordinary_file(file), name.or(Some(file)))
}

pub fn new_stdin_input(name: Option<&Path>) -> Input {
named(Input::stdin(), name)
pub fn new_stdin_input(name: Option<&Path>, is_terminal: bool) -> Input {
named(Input::stdin(is_terminal), name)
}

fn named<'a>(input: Input<'a>, name: Option<&Path>) -> Input<'a> {
Expand Down
40 changes: 28 additions & 12 deletions src/controller.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::io::{self, BufRead, Write};
use std::io::{self, BufRead, BufWriter, Write};

use crate::assets::HighlightingAssets;
use crate::config::{Config, VisibleLines};
Expand Down Expand Up @@ -88,9 +88,12 @@ impl<'b> Controller<'b> {
clircle::Identifier::stdout()
};

const BUF_W_SZ: usize = 1 << 14;
let mut writer = match output_buffer {
Some(buf) => OutputHandle::FmtWrite(buf),
None => OutputHandle::IoWrite(output_type.handle()?),
None => {
OutputHandle::IoWrite(BufWriter::with_capacity(BUF_W_SZ, output_type.handle()?))
Copy link
Collaborator

@Enselic Enselic Oct 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That might very well be the case. If so, it would be good to add a comment e.g. here that explains that. So that anyone that gets the same idea will find the comment when they are about to change the code.

And/or add a regression test that tests this use case so that CI does not become green.

}
};
let mut no_errors: bool = true;
let stderr = io::stderr();
Expand Down Expand Up @@ -124,10 +127,10 @@ impl<'b> Controller<'b> {
Ok(no_errors)
}

fn print_input<R: BufRead>(
fn print_input<R: BufRead, W: io::Write>(
&self,
input: Input,
writer: &mut OutputHandle,
writer: &mut OutputHandle<W>,
stdin: R,
stdout_identifier: Option<&Identifier>,
is_first: bool,
Expand Down Expand Up @@ -174,7 +177,7 @@ impl<'b> Controller<'b> {
None
};

let mut printer: Box<dyn Printer> = if self.config.loop_through {
let mut printer: Box<dyn Printer<_>> = if self.config.loop_through {
Box::new(SimplePrinter::new(self.config))
} else {
Box::new(InteractivePrinter::new(
Expand All @@ -196,10 +199,10 @@ impl<'b> Controller<'b> {
)
}

fn print_file(
fn print_file<W: io::Write>(
&self,
printer: &mut dyn Printer,
writer: &mut OutputHandle,
printer: &mut dyn Printer<W>,
writer: &mut OutputHandle<W>,
input: &mut OpenedInput,
add_header_padding: bool,
#[cfg(feature = "git")] line_changes: &Option<LineChanges>,
Expand Down Expand Up @@ -227,19 +230,26 @@ impl<'b> Controller<'b> {
}
};

self.print_file_ranges(printer, writer, &mut input.reader, &line_ranges)?;
self.print_file_ranges(
printer,
writer,
&mut input.reader,
&line_ranges,
input.is_terminal,
)?;
}
printer.print_footer(writer, input)?;

Ok(())
}

fn print_file_ranges(
fn print_file_ranges<W: io::Write>(
&self,
printer: &mut dyn Printer,
writer: &mut OutputHandle,
printer: &mut dyn Printer<W>,
writer: &mut OutputHandle<W>,
reader: &mut InputReader,
line_ranges: &LineRanges,
input_is_terminal: bool,
) -> Result<()> {
let mut line_buffer = Vec::new();
let mut line_number: usize = 1;
Expand Down Expand Up @@ -278,7 +288,13 @@ impl<'b> Controller<'b> {

line_number += 1;
line_buffer.clear();

// flush per line if input is terminal to allow interactive use
if input_is_terminal {
writer.flush()?;
}
}
writer.flush()?;
Ok(())
}
}
10 changes: 9 additions & 1 deletion src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ pub(crate) struct InputMetadata {
pub struct Input<'a> {
pub(crate) kind: InputKind<'a>,
pub(crate) metadata: InputMetadata,
pub(crate) is_terminal: bool,
pub(crate) description: InputDescription,
}

Expand All @@ -106,6 +107,7 @@ pub(crate) enum OpenedInputKind {
pub(crate) struct OpenedInput<'a> {
pub(crate) kind: OpenedInputKind,
pub(crate) metadata: InputMetadata,
pub(crate) is_terminal: bool,
pub(crate) reader: InputReader<'a>,
pub(crate) description: InputDescription,
}
Expand Down Expand Up @@ -141,15 +143,17 @@ impl<'a> Input<'a> {
description: kind.description(),
metadata,
kind,
is_terminal: false,
}
}

pub fn stdin() -> Self {
pub fn stdin(is_terminal: bool) -> Self {
let kind = InputKind::StdIn;
Input {
description: kind.description(),
metadata: InputMetadata::default(),
kind,
is_terminal,
}
}

Expand All @@ -159,6 +163,7 @@ impl<'a> Input<'a> {
description: kind.description(),
metadata: InputMetadata::default(),
kind,
is_terminal: false,
}
}

Expand Down Expand Up @@ -207,6 +212,7 @@ impl<'a> Input<'a> {
kind: OpenedInputKind::StdIn,
description,
metadata: self.metadata,
is_terminal: self.is_terminal,
reader: InputReader::new(stdin),
})
}
Expand All @@ -215,6 +221,7 @@ impl<'a> Input<'a> {
kind: OpenedInputKind::OrdinaryFile(path.clone()),
description,
metadata: self.metadata,
is_terminal: self.is_terminal,
reader: {
let mut file = File::open(&path)
.map_err(|e| format!("'{}': {}", path.to_string_lossy(), e))?;
Expand Down Expand Up @@ -243,6 +250,7 @@ impl<'a> Input<'a> {
description,
kind: OpenedInputKind::CustomReader,
metadata: self.metadata,
is_terminal: self.is_terminal,
reader: InputReader::new(BufReader::new(reader)),
}),
}
Expand Down
2 changes: 1 addition & 1 deletion src/pretty_printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ impl<'a> Input<'a> {

/// A new input from STDIN.
pub fn from_stdin() -> Self {
input::Input::stdin().into()
input::Input::stdin(true).into()
}

/// The filename of the input.
Expand Down
Loading