Skip to content

Commit d8a78c1

Browse files
mattsu2020sylvestre
authored andcommitted
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.
1 parent 88219aa commit d8a78c1

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/uu/wc/src/countable.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ use std::io::{BufRead, BufReader, Read, StdinLock};
1313
#[cfg(unix)]
1414
use std::os::fd::{AsFd, AsRawFd};
1515

16+
const WORD_COUNT_BUF_SIZE: usize = 256 * 1024;
17+
1618
#[cfg(unix)]
1719
pub trait WordCountable: AsFd + AsRawFd + Read {
1820
type Buffered: BufRead;
@@ -28,10 +30,10 @@ pub trait WordCountable: Read {
2830
}
2931

3032
impl WordCountable for StdinLock<'_> {
31-
type Buffered = Self;
33+
type Buffered = BufReader<Self>;
3234

3335
fn buffered(self) -> Self::Buffered {
34-
self
36+
BufReader::with_capacity(WORD_COUNT_BUF_SIZE, self)
3537
}
3638
fn inner_file(&mut self) -> Option<&mut File> {
3739
None
@@ -42,7 +44,7 @@ impl WordCountable for File {
4244
type Buffered = BufReader<Self>;
4345

4446
fn buffered(self) -> Self::Buffered {
45-
BufReader::new(self)
47+
BufReader::with_capacity(WORD_COUNT_BUF_SIZE, self)
4648
}
4749

4850
fn inner_file(&mut self) -> Option<&mut File> {

0 commit comments

Comments
 (0)