From 1cdc63199323a9467a157d9a7715efd1cc3dd4db Mon Sep 17 00:00:00 2001 From: zhitkoff Date: Fri, 1 Dec 2023 20:18:15 -0500 Subject: [PATCH] wc: count_fast seek optimization --- src/uu/wc/src/count_fast.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/uu/wc/src/count_fast.rs b/src/uu/wc/src/count_fast.rs index 1eb1d912724..1d3102f9975 100644 --- a/src/uu/wc/src/count_fast.rs +++ b/src/uu/wc/src/count_fast.rs @@ -100,7 +100,8 @@ pub(crate) fn count_bytes_fast(handle: &mut T) -> (usize, Opti // would count up only to a couple of bytes. // This condition usually occurs for files in pseudo-filesystems like /proc, /sys // that report `st_size` in the multiples of system page size. - // In such cases - attempt `seek()` for the end of file + // In such cases - attempt `seek()` almost to the end of the file + // and then fall back on read to count the rest. // // And finally a special case of input redirection in *nix shell: // `( wc -c ; wc -c ) < file` should return @@ -127,8 +128,9 @@ pub(crate) fn count_bytes_fast(handle: &mut T) -> (usize, Opti // with size that is NOT a multiple of system page size return (stat.st_size as usize, None); } else if let Some(file) = handle.inner_file() { - if let Ok(n) = file.seek(SeekFrom::End(0)) { - return (n as usize, None); + let offset = stat.st_size - stat.st_size % (stat.st_blksize as i64 + 1); + if let Ok(n) = file.seek(SeekFrom::Start(offset as u64)) { + byte_count = n as usize; } } }