1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 11:37:44 +00:00

cat: Formatting performance improvement (#7642)

* cat: Formatting performance improvement

Use memchr library in `cat` to improve performance when detecting
newlines.
Significantly improves performance when running with -n, -s, -E, -b
flags.


Co-authored-by: Sylvestre Ledru <sylvestre@debian.org>

---------

Co-authored-by: Sylvestre Ledru <sylvestre@debian.org>
This commit is contained in:
karlmcdowall 2025-04-04 15:47:28 -06:00 committed by GitHub
parent 88cf66174f
commit e6ff6d5c69
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 7 additions and 3 deletions

1
Cargo.lock generated
View file

@ -2535,6 +2535,7 @@ name = "uu_cat"
version = "0.0.30"
dependencies = [
"clap",
"memchr",
"nix",
"thiserror 2.0.12",
"uucore",

View file

@ -18,6 +18,7 @@ path = "src/cat.rs"
[dependencies]
clap = { workspace = true }
memchr = { workspace = true }
thiserror = { workspace = true }
uucore = { workspace = true, features = ["fs", "pipes"] }

View file

@ -17,6 +17,7 @@ use std::os::unix::fs::FileTypeExt;
use std::os::unix::net::UnixStream;
use clap::{Arg, ArgAction, Command};
use memchr::memchr2;
#[cfg(unix)]
use nix::fcntl::{FcntlArg, fcntl};
use thiserror::Error;
@ -118,12 +119,12 @@ struct OutputState {
}
#[cfg(unix)]
trait FdReadable: Read + AsFd + AsRawFd {}
trait FdReadable: Read + AsFd {}
#[cfg(not(unix))]
trait FdReadable: Read {}
#[cfg(unix)]
impl<T> FdReadable for T where T: Read + AsFd + AsRawFd {}
impl<T> FdReadable for T where T: Read + AsFd {}
#[cfg(not(unix))]
impl<T> FdReadable for T where T: Read {}
@ -612,7 +613,8 @@ fn write_end<W: Write>(writer: &mut W, in_buf: &[u8], options: &OutputOptions) -
// however, write_nonprint_to_end doesn't need to stop at \r because it will always write \r as ^M.
// Return the number of written symbols
fn write_to_end<W: Write>(in_buf: &[u8], writer: &mut W) -> usize {
match in_buf.iter().position(|c| *c == b'\n' || *c == b'\r') {
// using memchr2 significantly improves performances
match memchr2(b'\n', b'\r', in_buf) {
Some(p) => {
writer.write_all(&in_buf[..p]).unwrap();
p