Skip to content

Commit d9de764

Browse files
committed
shuf: Prepare dropping release-fast profile
1 parent 61dd826 commit d9de764

File tree

2 files changed

+75
-51
lines changed

2 files changed

+75
-51
lines changed

src/uu/shuf/src/shuf.rs

Lines changed: 20 additions & 5 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,23 @@ impl Writable for u64 {
386387
}
387388
}
388389

390+
#[cold]
391+
#[inline(never)]
392+
fn crash(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<()> {
395405
let ctx = || translate!("shuf-error-write-failed");
406+
let sep = [opts.sep];
396407
if opts.repeat {
397408
if input.is_empty() {
398409
return Err(USimpleError::new(
@@ -402,17 +413,21 @@ fn shuf_exec(
402413
}
403414
for _ in 0..opts.head_count {
404415
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)?;
416+
if let Err(e) = r.write_all_to(output) {
417+
return Err(crash(e));
418+
}
419+
if let Err(e) = output.write_all(&sep) {
420+
return Err(crash(e));
421+
}
408422
}
409423
} else {
410424
let shuffled = input.partial_shuffle(rng, opts.head_count)?;
425+
let sep = [opts.sep];
411426

412427
for r in shuffled {
413428
let r = r?;
414-
r.write_all_to(output).map_err_context(ctx)?;
415-
output.write_all(&[opts.sep]).map_err_context(ctx)?;
429+
r.write_all_to(output).map_err(crash)?;
430+
output.write_all(&sep).map_err(crash)?;
416431
}
417432
}
418433
output.flush().map_err_context(ctx)?;

src/uucore/src/lib/mods/error.rs

Lines changed: 55 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -407,56 +407,65 @@ impl Error for UIoError {}
407407

408408
impl Display for UIoError {
409409
fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
410-
use std::io::ErrorKind::*;
411-
412-
let message;
413-
let message = if self.inner.raw_os_error().is_some() {
414-
// These are errors that come directly from the OS.
415-
// We want to normalize their messages across systems,
416-
// and we want to strip the "(os error X)" suffix.
417-
match self.inner.kind() {
418-
NotFound => "No such file or directory",
419-
PermissionDenied => "Permission denied",
420-
ConnectionRefused => "Connection refused",
421-
ConnectionReset => "Connection reset",
422-
ConnectionAborted => "Connection aborted",
423-
NotConnected => "Not connected",
424-
AddrInUse => "Address in use",
425-
AddrNotAvailable => "Address not available",
426-
BrokenPipe => "Broken pipe",
427-
AlreadyExists => "Already exists",
428-
WouldBlock => "Would block",
429-
InvalidInput => "Invalid input",
430-
InvalidData => "Invalid data",
431-
TimedOut => "Timed out",
432-
WriteZero => "Write zero",
433-
Interrupted => "Interrupted",
434-
UnexpectedEof => "Unexpected end of file",
435-
_ => {
436-
// TODO: When the new error variants
437-
// (https://github.com/rust-lang/rust/issues/86442)
438-
// are stabilized, we should add them to the match statement.
439-
message = strip_errno(&self.inner);
440-
&message
441-
}
410+
cold_io_error(f, self.context.as_ref(), &self.inner)
411+
}
412+
}
413+
414+
#[cold] //for codegen-units=1
415+
#[inline(never)]
416+
fn cold_io_error(
417+
f: &mut Formatter<'_>,
418+
ctx: Option<&String>,
419+
inner: &std::io::Error,
420+
) -> Result<(), std::fmt::Error> {
421+
use std::io::ErrorKind::*;
422+
423+
let message_hold;
424+
let message = if inner.raw_os_error().is_some() {
425+
// These are errors that come directly from the OS.
426+
// We want to normalize their messages across systems,
427+
// and we want to strip the "(os error X)" suffix.
428+
match inner.kind() {
429+
NotFound => "No such file or directory",
430+
PermissionDenied => "Permission denied",
431+
ConnectionRefused => "Connection refused",
432+
ConnectionReset => "Connection reset",
433+
ConnectionAborted => "Connection aborted",
434+
NotConnected => "Not connected",
435+
AddrInUse => "Address in use",
436+
AddrNotAvailable => "Address not available",
437+
BrokenPipe => "Broken pipe",
438+
AlreadyExists => "Already exists",
439+
WouldBlock => "Would block",
440+
InvalidInput => "Invalid input",
441+
InvalidData => "Invalid data",
442+
TimedOut => "Timed out",
443+
WriteZero => "Write zero",
444+
Interrupted => "Interrupted",
445+
UnexpectedEof => "Unexpected end of file",
446+
_ => {
447+
// TODO: When the new error variants
448+
// (https://github.com/rust-lang/rust/issues/86442)
449+
// are stabilized, we should add them to the match statement.
450+
message_hold = strip_errno(inner);
451+
&message_hold
442452
}
443-
} else {
444-
// These messages don't need as much normalization, and the above
445-
// messages wouldn't always be a good substitute.
446-
// For example, ErrorKind::NotFound doesn't necessarily mean it was
447-
// a file that was not found.
448-
// There are also errors with entirely custom messages.
449-
message = self.inner.to_string();
450-
&message
451-
};
452-
if let Some(ctx) = &self.context {
453-
write!(f, "{ctx}: {message}")
454-
} else {
455-
write!(f, "{message}")
456453
}
454+
} else {
455+
// These messages don't need as much normalization, and the above
456+
// messages wouldn't always be a good substitute.
457+
// For example, ErrorKind::NotFound doesn't necessarily mean it was
458+
// a file that was not found.
459+
// There are also errors with entirely custom messages.
460+
message_hold = inner.to_string();
461+
&message_hold
462+
};
463+
if let Some(c) = ctx {
464+
write!(f, "{c}: {message}")
465+
} else {
466+
write!(f, "{message}")
457467
}
458468
}
459-
460469
/// Strip the trailing " (os error XX)" from io error strings.
461470
pub fn strip_errno(err: &std::io::Error) -> String {
462471
let mut msg = err.to_string();

0 commit comments

Comments
 (0)