Rollup merge of #83353 - m-ou-se:io-error-avoid-alloc, r=nagisa

Add internal io::Error::new_const to avoid allocations.

This makes it possible to have a io::Error containing a message with zero allocations, and uses that everywhere to avoid the *three* allocations involved in `io::Error::new(kind, "message")`.

The function signature isn't perfect, because it needs a reference to the `&str`. So for now, this is just a `pub(crate)` function. Later, we'll be able to use `fn new_const<MSG: &'static str>(kind: ErrorKind)` to make that a bit better. (Then we'll also be able to use some ZST trickery if that would result in more efficient code.)

See https://github.com/rust-lang/rust/issues/83352
This commit is contained in:
Dylan DPC
2021-03-24 01:52:29 +01:00
committed by GitHub
41 changed files with 272 additions and 189 deletions

View File

@@ -28,7 +28,10 @@ impl Command {
let envp = self.capture_env();
if self.saw_nul() {
return Err(io::Error::new(ErrorKind::InvalidInput, "nul byte found in provided data"));
return Err(io::Error::new_const(
ErrorKind::InvalidInput,
&"nul byte found in provided data",
));
}
let (ours, theirs) = self.setup_io(default, needs_stdin)?;
@@ -118,7 +121,10 @@ impl Command {
let envp = self.capture_env();
if self.saw_nul() {
return io::Error::new(ErrorKind::InvalidInput, "nul byte found in provided data");
return io::Error::new_const(
ErrorKind::InvalidInput,
&"nul byte found in provided data",
);
}
match self.setup_io(default, true) {
@@ -442,9 +448,9 @@ impl Process {
// and used for another process, and we probably shouldn't be killing
// random processes, so just return an error.
if self.status.is_some() {
Err(Error::new(
Err(Error::new_const(
ErrorKind::InvalidInput,
"invalid argument: can't kill an exited process",
&"invalid argument: can't kill an exited process",
))
} else {
cvt(unsafe { libc::kill(self.pid, libc::SIGKILL) }).map(drop)