Skip to content

Commit c76c0ad

Browse files
authored
Merge pull request #7934 from drinkcat/wc-faster
wc: Speed optimization
2 parents d7d0a33 + 1fc14d8 commit c76c0ad

File tree

1 file changed

+20
-3
lines changed

1 file changed

+20
-3
lines changed

src/uu/wc/src/count_fast.rs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use libc::S_IFIFO;
3232
#[cfg(any(target_os = "linux", target_os = "android"))]
3333
use uucore::pipes::{pipe, splice, splice_exact};
3434

35-
const BUF_SIZE: usize = 16 * 1024;
35+
const BUF_SIZE: usize = 256 * 1024;
3636
#[cfg(any(target_os = "linux", target_os = "android"))]
3737
const SPLICE_SIZE: usize = 128 * 1024;
3838

@@ -197,6 +197,23 @@ pub(crate) fn count_bytes_fast<T: WordCountable>(handle: &mut T) -> (usize, Opti
197197
}
198198
}
199199

200+
/// A simple structure used to align a BUF_SIZE buffer to 32-byte boundary.
201+
///
202+
/// This is useful as bytecount uses 256-bit wide vector operations that run much
203+
/// faster on aligned data (at least on x86 with AVX2 support).
204+
#[repr(align(32))]
205+
struct AlignedBuffer {
206+
data: [u8; BUF_SIZE],
207+
}
208+
209+
impl Default for AlignedBuffer {
210+
fn default() -> Self {
211+
Self {
212+
data: [0; BUF_SIZE],
213+
}
214+
}
215+
}
216+
200217
/// Returns a WordCount that counts the number of bytes, lines, and/or the number of Unicode characters encoded in UTF-8 read via a Reader.
201218
///
202219
/// This corresponds to the `-c`, `-l` and `-m` command line flags to wc.
@@ -213,9 +230,9 @@ pub(crate) fn count_bytes_chars_and_lines_fast<
213230
handle: &mut R,
214231
) -> (WordCount, Option<io::Error>) {
215232
let mut total = WordCount::default();
216-
let mut buf = [0; BUF_SIZE];
233+
let buf: &mut [u8] = &mut AlignedBuffer::default().data;
217234
loop {
218-
match handle.read(&mut buf) {
235+
match handle.read(buf) {
219236
Ok(0) => return (total, None),
220237
Ok(n) => {
221238
if COUNT_BYTES {

0 commit comments

Comments
 (0)