Skip to content
Draft
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 Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions fuzz/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/uu/wc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ path = "src/wc.rs"
clap = { workspace = true }
uucore = { workspace = true, features = ["parser", "pipes", "quoting-style"] }
bytecount = { workspace = true, features = ["runtime-dispatch-simd"] }
memchr = { workspace = true }
thiserror = { workspace = true }
unicode-width = { workspace = true }
fluent = { workspace = true }
Expand Down
57 changes: 57 additions & 0 deletions src/uu/wc/src/count_fast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,63 @@ pub(crate) fn count_bytes_chars_and_lines_fast<
const COUNT_LINES: bool,
>(
handle: &mut R,
) -> (WordCount, Option<io::Error>) {
// Use specialized implementations for common cases
match (COUNT_BYTES, COUNT_CHARS, COUNT_LINES) {
// Lines only - use memchr for fastest line counting
(false, false, true) => count_lines_only_fast(handle),
// Bytes + Lines - optimize using bytecount and avoid double counting
(true, false, true) => count_bytes_and_lines_fast(handle),
// Default implementation for other cases
_ => {
count_bytes_chars_and_lines_generic::<R, COUNT_BYTES, COUNT_CHARS, COUNT_LINES>(handle)
}
}
}

/// Specialized fast line counting using memchr
fn count_lines_only_fast<R: Read>(handle: &mut R) -> (WordCount, Option<io::Error>) {
let mut total = WordCount::default();
let buf: &mut [u8] = &mut AlignedBuffer::default().data;

loop {
match handle.read(buf) {
Ok(0) => return (total, None),
Ok(n) => {
total.lines += memchr::memchr_iter(b'\n', &buf[..n]).count();
}
Err(ref e) if e.kind() == ErrorKind::Interrupted => (),
Err(e) => return (total, Some(e)),
}
}
}

/// Specialized fast byte and line counting using bytecount
fn count_bytes_and_lines_fast<R: Read>(handle: &mut R) -> (WordCount, Option<io::Error>) {
let mut total = WordCount::default();
let buf: &mut [u8] = &mut AlignedBuffer::default().data;

loop {
match handle.read(buf) {
Ok(0) => return (total, None),
Ok(n) => {
total.bytes += n;
total.lines += bytecount::count(&buf[..n], b'\n');
}
Err(ref e) if e.kind() == ErrorKind::Interrupted => (),
Err(e) => return (total, Some(e)),
}
}
}

/// Generic implementation for mixed counting
fn count_bytes_chars_and_lines_generic<
R: Read,
const COUNT_BYTES: bool,
const COUNT_CHARS: bool,
const COUNT_LINES: bool,
>(
handle: &mut R,
) -> (WordCount, Option<io::Error>) {
let mut total = WordCount::default();
let buf: &mut [u8] = &mut AlignedBuffer::default().data;
Expand Down
Loading