Skip to content

Commit 74e0ddd

Browse files
committed
wc: reduce lines of code
1 parent 4f9a39b commit 74e0ddd

1 file changed

Lines changed: 18 additions & 23 deletions

File tree

src/uu/wc/src/count_fast.rs

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,30 +41,25 @@ const BUF_SIZE: usize = 64 * 1024;
4141
fn count_bytes_using_splice(fd: &impl AsFd) -> Result<usize, usize> {
4242
let null_file = uucore::pipes::dev_null().ok_or(0_usize)?;
4343
let mut byte_count = 0;
44-
if let Ok(res) = splice(fd, &null_file, MAX_ROOTLESS_PIPE_SIZE) {
45-
byte_count += res;
46-
// no need to increase pipe size of input fd since
47-
// - sender with splice probably increased size already
48-
// - sender without splice is bottleneck of our wc -c
49-
loop {
50-
match splice(fd, &null_file, MAX_ROOTLESS_PIPE_SIZE) {
51-
Ok(0) => return Ok(byte_count),
52-
Ok(res) => byte_count += res,
53-
Err(_) => return Err(byte_count),
54-
}
44+
// no need to increase pipe size of input fd since
45+
// - sender with splice probably increased size already
46+
// - sender without splice is bottleneck of our wc -c
47+
loop {
48+
match splice(fd, &null_file, MAX_ROOTLESS_PIPE_SIZE) {
49+
Ok(0) => return Ok(byte_count),
50+
Ok(res) => byte_count += res,
51+
Err(_) => break, // input is not pipe. needs additional pipe...
5552
}
56-
} else {
57-
// input is not pipe. needs broker to use splice() with additional cost
58-
let (pipe_rd, pipe_wr) = pipe::<false>(MAX_ROOTLESS_PIPE_SIZE).map_err(|_| 0_usize)?;
59-
loop {
60-
match splice(fd, &pipe_wr, MAX_ROOTLESS_PIPE_SIZE).map_err(|_| byte_count)? {
61-
0 => return Ok(byte_count),
62-
res => {
63-
byte_count += res;
64-
// pipe to null is not blocked. So this returns res at most cases
65-
// next splice does not hang if we discarded 1+ pages
66-
splice(&pipe_rd, &null_file, res).map_err(|_| byte_count)?;
67-
}
53+
}
54+
let (pipe_rd, pipe_wr) = pipe::<false>(MAX_ROOTLESS_PIPE_SIZE).map_err(|_| 0_usize)?;
55+
loop {
56+
match splice(fd, &pipe_wr, MAX_ROOTLESS_PIPE_SIZE).map_err(|_| byte_count)? {
57+
0 => return Ok(byte_count),
58+
res => {
59+
byte_count += res;
60+
// pipe to null is not blocked. So this returns res at most cases
61+
// next splice does not hang if we discarded 1+ pages
62+
splice(&pipe_rd, &null_file, res).map_err(|_| byte_count)?;
6863
}
6964
}
7065
}

0 commit comments

Comments
 (0)