Skip to content

Commit fb5f4d8

Browse files
committed
expand: improve the performances - 1.80 faster than GNU
1 parent f860610 commit fb5f4d8

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/uu/expand/src/expand.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,15 @@ fn expand_line(
358358
) -> std::io::Result<()> {
359359
use self::CharType::{Backspace, Other, Tab};
360360

361+
// Fast path: if there are no tabs, backspaces, and (in UTF-8 mode or no carriage returns),
362+
// we can write the buffer directly without character-by-character processing
363+
if !buf.contains(&b'\t') && !buf.contains(&b'\x08') && (options.uflag || !buf.contains(&b'\r'))
364+
{
365+
output.write_all(buf)?;
366+
buf.truncate(0);
367+
return Ok(());
368+
}
369+
361370
let mut col = 0;
362371
let mut byte = 0;
363372
let mut init = true;
@@ -435,7 +444,6 @@ fn expand_line(
435444
byte += nbytes; // advance the pointer
436445
}
437446

438-
output.flush()?;
439447
buf.truncate(0); // clear the buffer
440448

441449
Ok(())
@@ -471,6 +479,10 @@ fn expand(options: &Options) -> UResult<()> {
471479
}
472480
}
473481
}
482+
// Flush once at the end
483+
output
484+
.flush()
485+
.map_err_context(|| translate!("expand-error-failed-to-write-output"))?;
474486
Ok(())
475487
}
476488

0 commit comments

Comments
 (0)