Skip to content

Commit e6ff6d5

Browse files
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 <[email protected]> --------- Co-authored-by: Sylvestre Ledru <[email protected]>
1 parent 88cf661 commit e6ff6d5

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/uu/cat/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ path = "src/cat.rs"
1818

1919
[dependencies]
2020
clap = { workspace = true }
21+
memchr = { workspace = true }
2122
thiserror = { workspace = true }
2223
uucore = { workspace = true, features = ["fs", "pipes"] }
2324

src/uu/cat/src/cat.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use std::os::unix::fs::FileTypeExt;
1717
use std::os::unix::net::UnixStream;
1818

1919
use clap::{Arg, ArgAction, Command};
20+
use memchr::memchr2;
2021
#[cfg(unix)]
2122
use nix::fcntl::{FcntlArg, fcntl};
2223
use thiserror::Error;
@@ -118,12 +119,12 @@ struct OutputState {
118119
}
119120

120121
#[cfg(unix)]
121-
trait FdReadable: Read + AsFd + AsRawFd {}
122+
trait FdReadable: Read + AsFd {}
122123
#[cfg(not(unix))]
123124
trait FdReadable: Read {}
124125

125126
#[cfg(unix)]
126-
impl<T> FdReadable for T where T: Read + AsFd + AsRawFd {}
127+
impl<T> FdReadable for T where T: Read + AsFd {}
127128
#[cfg(not(unix))]
128129
impl<T> FdReadable for T where T: Read {}
129130

@@ -612,7 +613,8 @@ fn write_end<W: Write>(writer: &mut W, in_buf: &[u8], options: &OutputOptions) -
612613
// however, write_nonprint_to_end doesn't need to stop at \r because it will always write \r as ^M.
613614
// Return the number of written symbols
614615
fn write_to_end<W: Write>(in_buf: &[u8], writer: &mut W) -> usize {
615-
match in_buf.iter().position(|c| *c == b'\n' || *c == b'\r') {
616+
// using memchr2 significantly improves performances
617+
match memchr2(b'\n', b'\r', in_buf) {
616618
Some(p) => {
617619
writer.write_all(&in_buf[..p]).unwrap();
618620
p

0 commit comments

Comments
 (0)