-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Fastest way to initialize a vector is not documented #54628
Comments
Shouldn't |
I do not believe that's possible, since |
|
Clippy thread rust-lang/rust-clippy#3237 opened. |
Vec::new() then vec.resize(len, 0) can be fast though. |
I came across a similar issue today about the most efficient way to initialize a vector so I'm happy I ran into this. I'd be happy to submit a PR with a couple of doc comments in |
Add doc comments about safest way to initialize a vector of zeros This adds more information about the vec! macro as discussed in rust-lang#54628. I think this is a good starting point, but I think additional detail is needed so that we can explain why vec! is safer than the alternatives.
Add doc comments about safest way to initialize a vector of zeros This adds more information about the vec! macro as discussed in rust-lang#54628. I think this is a good starting point, but I think additional detail is needed so that we can explain why vec! is safer than the alternatives.
Add doc comments about safest way to initialize a vector of zeros This adds more information about the vec! macro as discussed in rust-lang#54628. I think this is a good starting point, but I think additional detail is needed so that we can explain why vec! is safer than the alternatives.
Add doc comments about safest way to initialize a vector of zeros This adds more information about the vec! macro as discussed in rust-lang#54628. I think this is a good starting point, but I think additional detail is needed so that we can explain why vec! is safer than the alternatives.
The linked PR has been merged. Should I close this issue? |
@Shnatsel How do I init vector with some zeroed newtype? For example, |
@Pzixel please ask questions on users.rust-lang.org, rather than on closed bugs. Thanks! |
I meant this one introduces special case or But now I see you think it's an inappropriate place to suggest this so I'm going to the urlo. Thanks for clarification. |
IMO asking for clarification in this case is fine, although you probably want to avoid mentioning someone particularly. The special case for zero is implemented with specialization of Line 1579 in 372be4f
|
@ishitatsuyuki great! Gonna create an issue (or even PR) then. Thanks. |
There are at least thee distinct ways to create a zero-filled vector with a certain capacity:
Despite the latter being the most concise one, other solutions also show up in real-world code. The performance characteristics of these solutions are not obvious, and not documented - at least on the Vec page in stdlib reference.
This has led to an actual security vulnerability in Claxon crate, now known as RUSTSEC-2018-0004. On some malformed inputs contents of uninitialized memory would show up in the output. See the original bug report or the security advisory for more details.
Like most binary format decoders, Claxon writes into a preallocated buffer. Memory unsafety that led to the vulnerability was introduced to speed up initialization of the vector. Initialization was originally performed like this:
buffer.extend(repeat(0).take(new_len - len))
, but was replaced withunsafe { buffer.set_len(new_len); }
for performance (see relevant commit).Anything that desugars into
RawVec::with_capacity_zeroed()
is dramatically more efficient than.extend()
, at least on Linux. One public wrapper for this function isvec!
macro. Replacingvec::with_capacity(new_len); unsafe { buffer.set_len(new_len); }
withvec![0; new_len];
created no measurable performance difference in Claxon, eliminating the need to unsafe code.I believe that clearly documenting that the
vec!
macro has special behavior when given 0 as argument and is dramatically more efficient than other means of initialization (at least on some platforms) would have prevented this vulnerability.The text was updated successfully, but these errors were encountered: