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:
parent
88cf66174f
commit
e6ff6d5c69
3 changed files with 7 additions and 3 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -2535,6 +2535,7 @@ name = "uu_cat"
|
||||||
version = "0.0.30"
|
version = "0.0.30"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap",
|
"clap",
|
||||||
|
"memchr",
|
||||||
"nix",
|
"nix",
|
||||||
"thiserror 2.0.12",
|
"thiserror 2.0.12",
|
||||||
"uucore",
|
"uucore",
|
||||||
|
|
|
@ -18,6 +18,7 @@ path = "src/cat.rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clap = { workspace = true }
|
clap = { workspace = true }
|
||||||
|
memchr = { workspace = true }
|
||||||
thiserror = { workspace = true }
|
thiserror = { workspace = true }
|
||||||
uucore = { workspace = true, features = ["fs", "pipes"] }
|
uucore = { workspace = true, features = ["fs", "pipes"] }
|
||||||
|
|
||||||
|
|
|
@ -17,6 +17,7 @@ use std::os::unix::fs::FileTypeExt;
|
||||||
use std::os::unix::net::UnixStream;
|
use std::os::unix::net::UnixStream;
|
||||||
|
|
||||||
use clap::{Arg, ArgAction, Command};
|
use clap::{Arg, ArgAction, Command};
|
||||||
|
use memchr::memchr2;
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
use nix::fcntl::{FcntlArg, fcntl};
|
use nix::fcntl::{FcntlArg, fcntl};
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
@ -118,12 +119,12 @@ struct OutputState {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
trait FdReadable: Read + AsFd + AsRawFd {}
|
trait FdReadable: Read + AsFd {}
|
||||||
#[cfg(not(unix))]
|
#[cfg(not(unix))]
|
||||||
trait FdReadable: Read {}
|
trait FdReadable: Read {}
|
||||||
|
|
||||||
#[cfg(unix)]
|
#[cfg(unix)]
|
||||||
impl<T> FdReadable for T where T: Read + AsFd + AsRawFd {}
|
impl<T> FdReadable for T where T: Read + AsFd {}
|
||||||
#[cfg(not(unix))]
|
#[cfg(not(unix))]
|
||||||
impl<T> FdReadable for T where T: Read {}
|
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.
|
// 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
|
// Return the number of written symbols
|
||||||
fn write_to_end<W: Write>(in_buf: &[u8], writer: &mut W) -> usize {
|
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) => {
|
Some(p) => {
|
||||||
writer.write_all(&in_buf[..p]).unwrap();
|
writer.write_all(&in_buf[..p]).unwrap();
|
||||||
p
|
p
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue