Skip to content

Commit

Permalink
wc: count_fast seek optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
zhitkoff committed Dec 2, 2023
1 parent ba83d43 commit 1cdc631
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/uu/wc/src/count_fast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ pub(crate) fn count_bytes_fast<T: WordCountable>(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
Expand All @@ -127,8 +128,9 @@ pub(crate) fn count_bytes_fast<T: WordCountable>(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;
}
}
}
Expand Down

0 comments on commit 1cdc631

Please sign in to comment.