From d8a78c1d5d15573d38680bf4b5ea6276c646898d Mon Sep 17 00:00:00 2001 From: mattsu Date: Tue, 10 Mar 2026 18:46:16 +0900 Subject: [PATCH] refactor(wc): optimize buffer size for word counting operations Increased buffer size to 256KB for improved performance in word counting operations across all input types (stdin and files). This optimization reduces I/O overhead by processing larger chunks of data at once. --- src/uu/wc/src/countable.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/uu/wc/src/countable.rs b/src/uu/wc/src/countable.rs index 5b2ad7a9965..1fc7c253e27 100644 --- a/src/uu/wc/src/countable.rs +++ b/src/uu/wc/src/countable.rs @@ -13,6 +13,8 @@ use std::io::{BufRead, BufReader, Read, StdinLock}; #[cfg(unix)] use std::os::fd::{AsFd, AsRawFd}; +const WORD_COUNT_BUF_SIZE: usize = 256 * 1024; + #[cfg(unix)] pub trait WordCountable: AsFd + AsRawFd + Read { type Buffered: BufRead; @@ -28,10 +30,10 @@ pub trait WordCountable: Read { } impl WordCountable for StdinLock<'_> { - type Buffered = Self; + type Buffered = BufReader; fn buffered(self) -> Self::Buffered { - self + BufReader::with_capacity(WORD_COUNT_BUF_SIZE, self) } fn inner_file(&mut self) -> Option<&mut File> { None @@ -42,7 +44,7 @@ impl WordCountable for File { type Buffered = BufReader; fn buffered(self) -> Self::Buffered { - BufReader::new(self) + BufReader::with_capacity(WORD_COUNT_BUF_SIZE, self) } fn inner_file(&mut self) -> Option<&mut File> {