From fb5f4d8c281242afd450bf19d80c6d0f9fcb18db Mon Sep 17 00:00:00 2001 From: Sylvestre Ledru Date: Sat, 4 Oct 2025 09:27:56 +0200 Subject: [PATCH] expand: improve the performances - 1.80 faster than GNU --- src/uu/expand/src/expand.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/uu/expand/src/expand.rs b/src/uu/expand/src/expand.rs index 0891a2de452..f6289a5733f 100644 --- a/src/uu/expand/src/expand.rs +++ b/src/uu/expand/src/expand.rs @@ -358,6 +358,15 @@ fn expand_line( ) -> std::io::Result<()> { use self::CharType::{Backspace, Other, Tab}; + // Fast path: if there are no tabs, backspaces, and (in UTF-8 mode or no carriage returns), + // we can write the buffer directly without character-by-character processing + if !buf.contains(&b'\t') && !buf.contains(&b'\x08') && (options.uflag || !buf.contains(&b'\r')) + { + output.write_all(buf)?; + buf.truncate(0); + return Ok(()); + } + let mut col = 0; let mut byte = 0; let mut init = true; @@ -435,7 +444,6 @@ fn expand_line( byte += nbytes; // advance the pointer } - output.flush()?; buf.truncate(0); // clear the buffer Ok(()) @@ -471,6 +479,10 @@ fn expand(options: &Options) -> UResult<()> { } } } + // Flush once at the end + output + .flush() + .map_err_context(|| translate!("expand-error-failed-to-write-output"))?; Ok(()) }