-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Add internal io::Error::new_const to avoid allocations. #83353
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Conversation
r? @kennytm (rust-highfive has picked a reviewer for you, use r? to override) |
There also is a typo in the PR title. |
This seems like a good use case for specialization. |
We spent quite a while with several people trying to use specialization here. If you find a way to do this in a nicer way with specialization, let me know. ^^ |
Unfortunately the signature of |
@bors r+ |
📌 Commit 6bbcc5b has been approved by |
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
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
Rollup of 9 pull requests Successful merges: - rust-lang#83051 (Sidebar trait items order) - rust-lang#83313 (Only enable assert_dep_graph when query-dep-graph is enabled.) - rust-lang#83353 (Add internal io::Error::new_const to avoid allocations.) - rust-lang#83391 (Allow not emitting `uwtable` on Android) - rust-lang#83392 (Change `-W help` to display edition level.) - rust-lang#83393 (Codeblock tooltip position) - rust-lang#83399 (rustdoc: Record crate name instead of using `None`) - rust-lang#83405 (Slight visual improvements to warning boxes in the docs) - rust-lang#83415 (Remove unnecessary `Option` wrapping around `Crate.module`) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
Another problem is that some crates still depends on pub enum ThinBox<'a, T> {
Borrowed(&'a T),
Owned(Box<T>),
}
fn main() {
dbg!(std::mem::size_of::<ThinBox<'static, &'static str>>()); //16
} However, now it is not clear how not to break impl Error {
pub fn into_inner(self) -> Option<Box<dyn Error + Send + Sync>> { ... }
} cc @m-ou-se |
@Frago9876543210 We can't fix The only way to provide this to other crates is by adding a new function (or new type), which requires crates to update their code. We could make the |
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 apub(crate)
function. Later, we'll be able to usefn 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 #83352