-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
io::Error::new(_, string literal) allocates Box<Box<String>> and copies the string #83352
Comments
m-ou-se
added
T-libs
Relevant to the library team, which will review and decide on the PR/issue.
A-io
Area: `std::io`, `std::fs`, `std::net` and `std::path`
labels
Mar 21, 2021
I think this is a duplicate of #82812 (or vice versa). |
Dylan-DPC-zz
pushed a commit
to Dylan-DPC-zz/rust
that referenced
this issue
Mar 23, 2021
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 rust-lang#83352
Dylan-DPC-zz
pushed a commit
to Dylan-DPC-zz/rust
that referenced
this issue
Mar 23, 2021
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 rust-lang#83352
Dylan-DPC-zz
pushed a commit
to Dylan-DPC-zz/rust
that referenced
this issue
Mar 24, 2021
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 rust-lang#83352
Should we consider providing a stable |
It'd have to be |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It's very common to use
io::Error::new(kind, "some message")
, including in many places instd
. However, that will create aio::Error
containing aRepr::Custom
containing aBox<Custom>
containing aBox<dyn Error + ..>
containing aStringError
containing aString
containing a copy of the message. 🙃Three allocations to just return an error message which is in some cases not even more helpful than just the ErrorKind, is not great. We should find a way to improve this.
The text was updated successfully, but these errors were encountered: