-
Notifications
You must be signed in to change notification settings - Fork 1.8k
new lint: manual_contains
#13817
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
new lint: manual_contains
#13817
Conversation
|
r? @Manishearth rustbot has assigned @Manishearth. Use |
acb27cb to
8eb9d35
Compare
|
On second thought, I thought it would be more appropriate to place the lint under the |
8eb9d35 to
14694b1
Compare
14694b1 to
c52ebf2
Compare
149e701 to
c1d5108
Compare
clippy_lints/src/slice_iter_any.rs
Outdated
| /// Checks for usage of `iter().any()` on slices of `u8` or `i8` and suggests using `contains()` instead. | ||
| /// | ||
| /// ### Why is this bad? | ||
| /// `iter().any()` on slices of `u8` or `i8` is optimized to use `memchr`. |
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.
Don't you mean .contains()? Anyway, I think this is too specific, on my machine it seems to use compiler intrinsics and make no calls to memchr.
You're right about performances though: I benchmarked both .iter().any(==) and .contains() and the latter runs more than 10 times faster on large areas.
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.
Oh, I had assumed from the implementation that memchr would be used in any environment (including my local environment). Also, this godbolt does so as far as the assembly is concerned: https://rust.godbolt.org/z/Kxrzr81Me
However, if it may differ depending on the environment, the description should be indeed modified, so I'll make a change. Could you please tell me for reference, what's the environment you have checked that?
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 think the memchr itself has been replaced by an intrinsic. I'm using the latest nightly compiler on x86_64.
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.
Thank you! In any case, it looks like this lint description should be modified.
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 think the memchr() exists for larger integer types too, yes?
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.
oh, nope, it doesn't. It could be extended, I think.
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 found that contains() is now faster for certain types (u16, u32, u64, i16, i32, i64, f32, f64, usize, isize) compared to before (see: rust-lang/rust#130991). Therefore, this performance lint should be extended to cover these types starting from Rust 1.84.0 and I'll make a change for this.
2b9d742 to
b3837dc
Compare
contains() instead of iter().any() for u8 and i8 slicesiter().any()
b3837dc to
a956f34
Compare
a956f34 to
19357ca
Compare
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.
A thing I'm unsure about is the mixing of two lints like this. I'm going to ask on Zulip if we should be doing two lints here.
|
I'm traveling so I'll wait for @samueltardieu to finish approving |
8a4f434 to
e72699f
Compare
slow_manual_containsmanual_contains
e72699f to
0c25c9f
Compare
0c25c9f to
f68b292
Compare
samueltardieu
left a comment
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.
Looks good to me, thanks, approving on my side, provided you fix the latest nit on if/match collapse.
f68b292 to
97cae27
Compare
97cae27 to
2622c44
Compare
|
@Manishearth All good for me, hits from lintcheck are legit. |
|
I'm not sure if that requires a FCP given that it has been discussed in Zulip already. |
2622c44 to
1bb7976
Compare
|
Rebased. |
1bb7976 to
1c0e120
Compare
|
Rebased. |
|
Thanks for your patience! |
|
@lapla-cogito thanks for implementing! Two questions:
|
|
|
@lapla-cogito thx for the link! So if I read this right, the general agreement is that we should always promote |
close #13353
Using
contains()for slices are more efficient than usingiter().any().changelog: [
manual_contains]: new lint