Skip to content

Commit ebc73d8

Browse files
committed
wc: improve wc -c perf
1 parent 1725543 commit ebc73d8

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/uu/wc/src/count_fast.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,9 @@ const FILE_ATTRIBUTE_NORMAL: u32 = 128;
2929
#[cfg(any(target_os = "linux", target_os = "android"))]
3030
use libc::S_IFIFO;
3131
#[cfg(any(target_os = "linux", target_os = "android"))]
32-
use uucore::pipes::{pipe, splice, splice_exact};
32+
use uucore::pipes::{MAX_ROOTLESS_PIPE_SIZE, pipe, splice, splice_exact};
3333

3434
const BUF_SIZE: usize = 256 * 1024;
35-
#[cfg(any(target_os = "linux", target_os = "android"))]
36-
const SPLICE_SIZE: usize = 128 * 1024;
3735

3836
/// This is a Linux-specific function to count the number of bytes using the
3937
/// `splice` system call, which is faster than using `read`.
@@ -55,11 +53,14 @@ fn count_bytes_using_splice(fd: &impl AsFd) -> Result<usize, usize> {
5553
// Bit of an edge case, but it has been known to happen
5654
return Err(0);
5755
}
56+
// todo: avoid generating broker if input is pipe (fcntl_setpipe_size succeed) and directly splice() to /dev/null to save RAM usage
5857
let (pipe_rd, pipe_wr) = pipe().map_err(|_| 0_usize)?;
5958

6059
let mut byte_count = 0;
60+
// improve throughput from pipe
61+
let _ = rustix::pipe::fcntl_setpipe_size(fd, MAX_ROOTLESS_PIPE_SIZE);
6162
loop {
62-
match splice(fd, &pipe_wr, SPLICE_SIZE) {
63+
match splice(fd, &pipe_wr, MAX_ROOTLESS_PIPE_SIZE) {
6364
Ok(0) => break,
6465
Ok(res) => {
6566
byte_count += res;

0 commit comments

Comments
 (0)