Skip to content

Commit

Permalink
Auto merge of rust-lang#128441 - Bryanskiy:delegation-perf, r=petroch…
Browse files Browse the repository at this point in the history
…enkov

Delegation: second attempt to improve perf

Possible perf fix for rust-lang#125929

r? `@petrochenkov`
  • Loading branch information
bors committed Aug 3, 2024
2 parents 64ebd39 + 9b097b2 commit 8f63e9f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 27 deletions.
11 changes: 11 additions & 0 deletions compiler/rustc_hir/src/hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2952,6 +2952,17 @@ pub struct FnDecl<'hir> {
pub lifetime_elision_allowed: bool,
}

impl<'hir> FnDecl<'hir> {
pub fn opt_delegation_sig_id(&self) -> Option<DefId> {
if let FnRetTy::Return(ty) = self.output
&& let TyKind::InferDelegation(sig_id, _) = ty.kind
{
return Some(sig_id);
}
None
}
}

/// Represents what type of implicit self a function has, if any.
#[derive(Copy, Clone, PartialEq, Eq, Encodable, Decodable, Debug, HashStable_Generic)]
pub enum ImplicitSelfKind {
Expand Down
17 changes: 10 additions & 7 deletions compiler/rustc_hir_analysis/src/collect/generics_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,6 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
};
}

// For a delegation item inherit generics from callee.
if let Some(sig_id) = tcx.hir().opt_delegation_sig_id(def_id)
&& let Some(generics) = inherit_generics_for_delegation_item(tcx, def_id, sig_id)
{
return generics;
}

let hir_id = tcx.local_def_id_to_hir_id(def_id);

let node = tcx.hir_node(hir_id);
Expand Down Expand Up @@ -234,6 +227,16 @@ pub(super) fn generics_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Generics {
// inherit the generics of the item.
Some(parent.to_def_id())
}
ItemKind::Fn(sig, _, _) => {
// For a delegation item inherit generics from callee.
if let Some(sig_id) = sig.decl.opt_delegation_sig_id()
&& let Some(generics) =
inherit_generics_for_delegation_item(tcx, def_id, sig_id)
{
return generics;
}
None
}
_ => None,
},
_ => None,
Expand Down
17 changes: 10 additions & 7 deletions compiler/rustc_hir_analysis/src/collect/predicates_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,6 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
None => {}
}

// For a delegation item inherit predicates from callee.
if let Some(sig_id) = tcx.hir().opt_delegation_sig_id(def_id)
&& let Some(predicates) = inherit_predicates_for_delegation_item(tcx, def_id, sig_id)
{
return predicates;
}

let hir_id = tcx.local_def_id_to_hir_id(def_id);
let node = tcx.hir_node(hir_id);

Expand Down Expand Up @@ -151,6 +144,16 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
ItemKind::Trait(_, _, _, self_bounds, ..) | ItemKind::TraitAlias(_, self_bounds) => {
is_trait = Some(self_bounds);
}

ItemKind::Fn(sig, _, _) => {
// For a delegation item inherit predicates from callee.
if let Some(sig_id) = sig.decl.opt_delegation_sig_id()
&& let Some(predicates) =
inherit_predicates_for_delegation_item(tcx, def_id, sig_id)
{
return predicates;
}
}
_ => {}
}
};
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_hir_analysis/src/delegation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ pub(crate) fn inherit_sig_for_delegation_item<'tcx>(
tcx: TyCtxt<'tcx>,
def_id: LocalDefId,
) -> &'tcx [Ty<'tcx>] {
let sig_id = tcx.hir().delegation_sig_id(def_id);
let sig_id = tcx.hir().opt_delegation_sig_id(def_id).unwrap();
let caller_sig = tcx.fn_sig(sig_id);
if let Err(err) = check_constraints(tcx, def_id, sig_id) {
let sig_len = caller_sig.instantiate_identity().skip_binder().inputs().len() + 1;
Expand Down
13 changes: 1 addition & 12 deletions compiler/rustc_middle/src/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -747,18 +747,7 @@ impl<'hir> Map<'hir> {
}

pub fn opt_delegation_sig_id(self, def_id: LocalDefId) -> Option<DefId> {
if let Some(ret) = self.get_fn_output(def_id)
&& let FnRetTy::Return(ty) = ret
&& let TyKind::InferDelegation(sig_id, _) = ty.kind
{
return Some(sig_id);
}
None
}

#[inline]
pub fn delegation_sig_id(self, def_id: LocalDefId) -> DefId {
self.opt_delegation_sig_id(def_id).unwrap()
self.tcx.opt_hir_owner_node(def_id)?.fn_decl()?.opt_delegation_sig_id()
}

#[inline]
Expand Down

0 comments on commit 8f63e9f

Please sign in to comment.