-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Optimize unit_return_expecting_ord
#14905
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
rustbot has assigned @samueltardieu. Use |
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.
Nice, but you can use interned symbols instead of allocating strings, that can only help (if a little bit) with performances.
fn_mut_trait: DefId, | ||
ord_trait: Option<DefId>, | ||
partial_ord_trait: Option<DefId>, | ||
) -> Vec<(usize, String)> { |
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.
) -> Vec<(usize, String)> { | |
) -> Vec<(usize, Symbol)> { |
@@ -102,12 +116,18 @@ fn get_args_to_check<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) -> Ve | |||
.iter() | |||
.any(|ord| Some(ord.self_ty()) == return_ty_pred.term.as_type()) | |||
{ | |||
args_to_check.push((i, "Ord".to_string())); | |||
args_to_check.push((i, String::from("Ord"))); |
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.
args_to_check.push((i, String::from("Ord"))); | |
args_to_check.push((i, sym::Ord)); |
} else if partial_ord_preds | ||
.iter() | ||
.any(|pord| pord.self_ty() == return_ty_pred.term.expect_type()) | ||
{ | ||
args_to_check.push((i, "PartialOrd".to_string())); | ||
args_to_check.push((i, String::from("PartialOrd"))); |
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.
args_to_check.push((i, String::from("PartialOrd"))); | |
args_to_check.push((i, sym::PartialOrd)); |
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 did it initially and reverted it with some mental gymnastics. I'll re-revert it.
7d0b957
to
2635ced
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.
Except for the two nits (no need to call .as_str()
, and you can inline the trait_name
in format string), this LGTM. If you just fix those two places feel free to r=me.
format!( | ||
"this closure returns \ | ||
the unit type which also implements {}", | ||
trait_name.as_str() |
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.
format!( | |
"this closure returns \ | |
the unit type which also implements {}", | |
trait_name.as_str() | |
format!( | |
"this closure returns \ | |
the unit type which also implements {trait_name}" |
format!( | ||
"this closure returns \ | ||
the unit type which also implements {}", | ||
trait_name.as_str() | ||
), |
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.
format!( | |
"this closure returns \ | |
the unit type which also implements {}", | |
trait_name.as_str() | |
), | |
format!( | |
"this closure returns \ | |
the unit type which also implements {trait_name}" | |
), |
This lint was previously written very clumsily, not shortcircuiting and doing a lot of unnecessary work. Now it makes sure to do the cheaper functions earlier and in general, just be smarter. (I specifically focused on minimizing binder instantiation) Also, avoid allocating unnecessarily
2635ced
to
7e590de
Compare
Sadly we don't have bors anymore, so PRs can't be merged in behalf of another person 😢 |
I know, this is an inherited way of saying "feel free to press the merge button on my behalf as I take responsibility for it". But since I'm here I'll do it! |
@@ -36,21 +36,26 @@ declare_clippy_lint! { | |||
|
|||
declare_lint_pass!(UnitReturnExpectingOrd => [UNIT_RETURN_EXPECTING_ORD]); | |||
|
|||
fn get_trait_predicates_for_trait_id<'tcx>( | |||
// For each |
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.
Is this comment unfinished?
{ | ||
preds.push(trait_pred); | ||
.instantiate_bound_regions_with_erased(pred.kind().rebind(poly_trait_pred)); | ||
for (i, tid) in trait_ids.iter().enumerate() { |
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.
The slice/array approach is a bit hard to understand, you have to look at the callsite to understand it. I see you want to get the operations within the instantiate_bound_regions_with_erased
call. I think you could do that by passing in three separate arguments and handling them in sequence here, instead of using a for
loop? I think that would be easier to read.
This lint was previously written very clumsily, not short-circuiting and doing a lot of unnecessary work.
Now it makes sure to do the cheaper functions earlier and in general, is just smarter.
(I specifically focused on minimizing binder instantiation
Sadly, I'm not finding any relevant result in a benchmark. Still with the LLVM coverage instruments, the expensive bits are called lots of less times (The binder instantiation that I care about is reduced from 95k to 10k throughout our test suite).
changelog:[
unit_return_expecting_ord
]: Optimize the lint