-
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
Performance degrades if stdlib function uses io::Error::new() with &'static str #82812
Comments
Note that the implementation of
The "std_err" bench is still pretty slow though |
@SkiFire13 Yes, I probably made a mistake while copying the code for the benchmark of |
This is fixed by #83353 |
Turns out it was three times, if you also count the |
Running your benchmark (thanks!): Before:
After:
Looks like it isn't completely solved yet. Adding |
That fixed it.
|
…r=jackh726 Add #[inline] to io::Error methods Fixes rust-lang#82812
I am facing a problem while reading different kinds of binary formats. If you are going to read primitives like
i32
too often, then performance degrades. This is mainly due to the fact thatError::new(kind, &'static str)
returns box. Also it does boxing TWICE!doc link: https://doc.rust-lang.org/std/io/struct.Error.html#method.new
I wrote simple bench to show pref problem.
read.rs:
Results
Use
cargo bench
to get itWe can clearly see that performance is degrading. The more calls it takes to read, the worse the results will be.
Affected crates and stdlib itself
The first very popular crate that comes to mind is byteorder.
https://github.com/BurntSushi/byteorder/blob/5d9d0386488d0763954f14a5cb02b1c0afccbdcb/src/io.rs#L84-L89
It mainly depends on the data format. But what if you need to receive data over the network? The attacker may abuse wrong data to make ton of pointless memory allocations.
stdlib is also use
Error::new()
often enough. Let's run a simple bash to make sure of thisIn many cases, you will see that it is used
&'static str
. stdlib sometimes createio::Error
withformat!()
macro, however it doesn't happen very often.Possible solutions
io::ErrorKind::with_message(_: &'static str)
and new internalRepr
member of that's enum.Cow
, but with static lifetime?Meta
rustc --version --verbose
:Update 07.03.2021: Benchmarks have been improved as per the comment. This should show the problem better.
The text was updated successfully, but these errors were encountered: