Skip to content

Commit

Permalink
Only calculate trait_ref.self_ty() once in get_blanket_impls
Browse files Browse the repository at this point in the history
On crates with many blanket impls, such as `stm32`, this inner closure
is executed many hundreds of thousands of times. This makes it very
slightly faster.

Unfortunately, I don't have a good test case for showing that it's
faster until rust-lang/rustc-perf#802 is merged.
  • Loading branch information
jyn514 committed Mar 7, 2021
1 parent edeee91 commit 8b5bc95
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/librustdoc/clean/blanket_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
trait_def_id, impl_def_id
);
let trait_ref = self.cx.tcx.impl_trait_ref(impl_def_id).unwrap();
let trait_ty = trait_ref.self_ty();
let may_apply = self.cx.tcx.infer_ctxt().enter(|infcx| {
match trait_ref.self_ty().kind() {
match trait_ty.kind() {
ty::Param(_) => {}
_ => return false,
}
Expand All @@ -48,7 +49,7 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
// Require the type the impl is implemented on to match
// our type, and ignore the impl if there was a mismatch.
let cause = traits::ObligationCause::dummy();
let eq_result = infcx.at(&cause, param_env).eq(trait_ref.self_ty(), ty);
let eq_result = infcx.at(&cause, param_env).eq(trait_ty, ty);
if let Ok(InferOk { value: (), obligations }) = eq_result {
// FIXME(eddyb) ignoring `obligations` might cause false positives.
drop(obligations);
Expand Down Expand Up @@ -128,7 +129,7 @@ impl<'a, 'tcx> BlanketImplFinder<'a, 'tcx> {
.clean(self.cx),
negative_polarity: false,
synthetic: false,
blanket_impl: Some(trait_ref.self_ty().clean(self.cx)),
blanket_impl: Some(trait_ty.clean(self.cx)),
}),
});
});
Expand Down

0 comments on commit 8b5bc95

Please sign in to comment.