Skip to content

Commit e061f8e

Browse files
authored
shuf: Tune performance for -i 1-1000000 (#10478)
1 parent cbb7f63 commit e061f8e

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

src/uu/shuf/src/shuf.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ impl Writable for &OsStr {
378378
}
379379

380380
impl Writable for u64 {
381+
#[inline]
381382
fn write_all_to(&self, output: &mut impl OsWrite) -> Result<(), Error> {
382383
// The itoa crate is surprisingly much more efficient than a formatted write.
383384
// It speeds up `shuf -r -n1000000 -i1-1024` by 1.8×.
@@ -386,13 +387,22 @@ impl Writable for u64 {
386387
}
387388
}
388389

390+
#[cold]
391+
#[inline(never)]
392+
fn handle_write_error(e: std::io::Error) -> Box<dyn uucore::error::UError> {
393+
use uucore::error::FromIo;
394+
let ctx = translate!("shuf-error-write-failed");
395+
e.map_err_context(move || ctx)
396+
}
397+
398+
#[inline(never)]
389399
fn shuf_exec(
390400
input: &mut impl Shufable,
391401
opts: &Options,
392402
rng: &mut WrappedRng,
393403
output: &mut BufWriter<Box<dyn OsWrite>>,
394404
) -> UResult<()> {
395-
let ctx = || translate!("shuf-error-write-failed");
405+
let sep = [opts.sep];
396406
if opts.repeat {
397407
if input.is_empty() {
398408
return Err(USimpleError::new(
@@ -402,20 +412,19 @@ fn shuf_exec(
402412
}
403413
for _ in 0..opts.head_count {
404414
let r = input.choose(rng)?;
405-
406-
r.write_all_to(output).map_err_context(ctx)?;
407-
output.write_all(&[opts.sep]).map_err_context(ctx)?;
415+
r.write_all_to(output).map_err(handle_write_error)?;
416+
output.write_all(&sep).map_err(handle_write_error)?;
408417
}
409418
} else {
410419
let shuffled = input.partial_shuffle(rng, opts.head_count)?;
411420

412421
for r in shuffled {
413422
let r = r?;
414-
r.write_all_to(output).map_err_context(ctx)?;
415-
output.write_all(&[opts.sep]).map_err_context(ctx)?;
423+
r.write_all_to(output).map_err(handle_write_error)?;
424+
output.write_all(&sep).map_err(handle_write_error)?;
416425
}
417426
}
418-
output.flush().map_err_context(ctx)?;
427+
output.flush().map_err(handle_write_error)?;
419428

420429
Ok(())
421430
}

0 commit comments

Comments
 (0)