-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Add slow vector initializations lint #3365
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
Conversation
63b5bab to
b38ed6b
Compare
| error: detected slow zero-filling initialization | ||
| --> $DIR/slow_vector_initialization.rs:67:5 | ||
| | | ||
| 67 | let mut unsafe_vec: Vec<u8> = Vec::with_capacity(200); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Calling this code "zero-filling initialization" may be misleading. with_capacity ends up in Alloc::alloc which does not ensure that the allocated memory is zero filled (unlike alloc_zeroed), right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you are right, is misleading. I'm working on splitting the current lint into two: One for slow initialization and another for the unsafe.
I'll push the change tomorrow.
Thanks!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've pushed a change which splits the previous lint into two: One for slow and one for slow initializations.
7cd3f4d to
7c79a8a
Compare
|
retriggering CI |
f5fac7c to
acc23ac
Compare
|
This looks good but seems like the it doesn't account for statements between initialization and extension. For example, I'm pretty sure that will lead to false positives. |
|
Thanks @mikerite for the review. I'll change the behavior and only check consecutive statements. |
8527beb to
692078b
Compare
|
@mikerite I've added a new commit which only checks the following (relevant) expression. I've tried to mimic the style on |
5e82e2d to
946dbb6
Compare
|
I wanted to merge this now but I found that don't have write access any more. Hopefully, someone with access will pick this up. |
|
This needs a rebase and maybe another run of |
Add lint to detect slow zero-filled vector initialization. It detects when a vector is zero-filled with extended with `repeat(0).take(len)` or `resize(len, 0)`. This zero-fillings are usually slower than simply using `vec![0; len]`.
Renamed some symbols in order to make them a little bit more accurate.
Instead of searching for all the successive expressions after a vector allocation, check only the first expression. This is done to minimize the amount of false positives of the lint.
946dbb6 to
dc35841
Compare
|
Rebased and ran again |
Closes #3237
Add lint to detect slow zero-filled vector initialization. It detects
when a vector is zero-filled with extended with
repeat(0).take(len)or
resize(len, 0).This zero-fillings are usually slower than simply using
vec![0; len]