From f33df28c6dc056f24d9c011ee506e978fcf3c8f3 Mon Sep 17 00:00:00 2001 From: oech3 <79379754+oech3@users.noreply.github.com> Date: Sun, 25 Jan 2026 21:30:00 +0900 Subject: [PATCH] shuf: Tune performance for -i 1-1000000 --- src/uu/shuf/src/shuf.rs | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/uu/shuf/src/shuf.rs b/src/uu/shuf/src/shuf.rs index 73290a0fc27..970a623e254 100644 --- a/src/uu/shuf/src/shuf.rs +++ b/src/uu/shuf/src/shuf.rs @@ -378,6 +378,7 @@ impl Writable for &OsStr { } impl Writable for u64 { + #[inline] fn write_all_to(&self, output: &mut impl OsWrite) -> Result<(), Error> { // The itoa crate is surprisingly much more efficient than a formatted write. // It speeds up `shuf -r -n1000000 -i1-1024` by 1.8×. @@ -386,13 +387,22 @@ impl Writable for u64 { } } +#[cold] +#[inline(never)] +fn handle_write_error(e: std::io::Error) -> Box { + use uucore::error::FromIo; + let ctx = translate!("shuf-error-write-failed"); + e.map_err_context(move || ctx) +} + +#[inline(never)] fn shuf_exec( input: &mut impl Shufable, opts: &Options, rng: &mut WrappedRng, output: &mut BufWriter>, ) -> UResult<()> { - let ctx = || translate!("shuf-error-write-failed"); + let sep = [opts.sep]; if opts.repeat { if input.is_empty() { return Err(USimpleError::new( @@ -402,20 +412,19 @@ fn shuf_exec( } for _ in 0..opts.head_count { let r = input.choose(rng)?; - - r.write_all_to(output).map_err_context(ctx)?; - output.write_all(&[opts.sep]).map_err_context(ctx)?; + r.write_all_to(output).map_err(handle_write_error)?; + output.write_all(&sep).map_err(handle_write_error)?; } } else { let shuffled = input.partial_shuffle(rng, opts.head_count)?; for r in shuffled { let r = r?; - r.write_all_to(output).map_err_context(ctx)?; - output.write_all(&[opts.sep]).map_err_context(ctx)?; + r.write_all_to(output).map_err(handle_write_error)?; + output.write_all(&sep).map_err(handle_write_error)?; } } - output.flush().map_err_context(ctx)?; + output.flush().map_err(handle_write_error)?; Ok(()) }