From 2d030649324f5fea55be684c40dea196a82f6b8c Mon Sep 17 00:00:00 2001 From: oech3 <79379754+oech3@users.noreply.github.com> Date: Sat, 23 May 2026 21:13:36 +0900 Subject: [PATCH] wc: reduce lines of code --- src/uu/wc/src/count_fast.rs | 41 ++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 23 deletions(-) diff --git a/src/uu/wc/src/count_fast.rs b/src/uu/wc/src/count_fast.rs index d1bd609a95..bf35c06a4f 100644 --- a/src/uu/wc/src/count_fast.rs +++ b/src/uu/wc/src/count_fast.rs @@ -41,30 +41,25 @@ const BUF_SIZE: usize = 64 * 1024; fn count_bytes_using_splice(fd: &impl AsFd) -> Result { let null_file = uucore::pipes::dev_null().ok_or(0_usize)?; let mut byte_count = 0; - if let Ok(res) = splice(fd, &null_file, MAX_ROOTLESS_PIPE_SIZE) { - byte_count += res; - // no need to increase pipe size of input fd since - // - sender with splice probably increased size already - // - sender without splice is bottleneck of our wc -c - loop { - match splice(fd, &null_file, MAX_ROOTLESS_PIPE_SIZE) { - Ok(0) => return Ok(byte_count), - Ok(res) => byte_count += res, - Err(_) => return Err(byte_count), - } + // no need to increase pipe size of input fd since + // - sender with splice probably increased size already + // - sender without splice is bottleneck of our wc -c + loop { + match splice(fd, &null_file, MAX_ROOTLESS_PIPE_SIZE) { + Ok(0) => return Ok(byte_count), + Ok(res) => byte_count += res, + Err(_) => break, // input is not pipe. needs additional pipe... } - } else { - // input is not pipe. needs broker to use splice() with additional cost - let (pipe_rd, pipe_wr) = pipe::(MAX_ROOTLESS_PIPE_SIZE).map_err(|_| 0_usize)?; - loop { - match splice(fd, &pipe_wr, MAX_ROOTLESS_PIPE_SIZE).map_err(|_| byte_count)? { - 0 => return Ok(byte_count), - res => { - byte_count += res; - // pipe to null is not blocked. So this returns res at most cases - // next splice does not hang if we discarded 1+ pages - splice(&pipe_rd, &null_file, res).map_err(|_| byte_count)?; - } + } + let (pipe_rd, pipe_wr) = pipe::(MAX_ROOTLESS_PIPE_SIZE).map_err(|_| byte_count)?; + loop { + match splice(fd, &pipe_wr, MAX_ROOTLESS_PIPE_SIZE).map_err(|_| byte_count)? { + 0 => return Ok(byte_count), + res => { + byte_count += res; + // pipe to null is not blocked. So this returns res at most cases + // next splice does not hang if we discarded 1+ pages + splice(&pipe_rd, &null_file, res).map_err(|_| byte_count)?; } } }