Skip to content

Commit d15f646

Browse files
bors[bot]matklad
andauthored
Merge #10074
10074: internal: improve compile times a bit r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 35d9807 + cfa3f67 commit d15f646

File tree

2 files changed

+78
-17
lines changed

2 files changed

+78
-17
lines changed

crates/hir/src/lib.rs

Lines changed: 76 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2517,18 +2517,35 @@ impl Type {
25172517
krate: Crate,
25182518
mut callback: impl FnMut(AssocItem) -> Option<T>,
25192519
) -> Option<T> {
2520-
for krate in method_resolution::def_crates(db, &self.ty, krate.id)? {
2520+
let mut slot = None;
2521+
self.iterate_assoc_items_dyn(db, krate, &mut |assoc_item_id| {
2522+
slot = callback(assoc_item_id.into());
2523+
slot.is_some()
2524+
});
2525+
slot
2526+
}
2527+
2528+
fn iterate_assoc_items_dyn(
2529+
self,
2530+
db: &dyn HirDatabase,
2531+
krate: Crate,
2532+
callback: &mut dyn FnMut(AssocItemId) -> bool,
2533+
) {
2534+
let def_crates = match method_resolution::def_crates(db, &self.ty, krate.id) {
2535+
Some(it) => it,
2536+
None => return,
2537+
};
2538+
for krate in def_crates {
25212539
let impls = db.inherent_impls_in_crate(krate);
25222540

25232541
for impl_def in impls.for_self_ty(&self.ty) {
25242542
for &item in db.impl_data(*impl_def).items.iter() {
2525-
if let Some(result) = callback(item.into()) {
2526-
return Some(result);
2543+
if callback(item) {
2544+
return;
25272545
}
25282546
}
25292547
}
25302548
}
2531-
None
25322549
}
25332550

25342551
pub fn type_arguments(&self) -> impl Iterator<Item = Type> + '_ {
@@ -2547,9 +2564,34 @@ impl Type {
25472564
krate: Crate,
25482565
traits_in_scope: &FxHashSet<TraitId>,
25492566
name: Option<&Name>,
2550-
mut callback: impl FnMut(&Ty, Function) -> Option<T>,
2567+
mut callback: impl FnMut(Type, Function) -> Option<T>,
25512568
) -> Option<T> {
25522569
let _p = profile::span("iterate_method_candidates");
2570+
let mut slot = None;
2571+
self.iterate_method_candidates_dyn(
2572+
db,
2573+
krate,
2574+
traits_in_scope,
2575+
name,
2576+
&mut |ty, assoc_item_id| match assoc_item_id {
2577+
AssocItemId::FunctionId(it) => {
2578+
slot = callback(self.derived(ty.clone()), it.into());
2579+
slot.is_some()
2580+
}
2581+
AssocItemId::ConstId(_) | AssocItemId::TypeAliasId(_) => false,
2582+
},
2583+
);
2584+
slot
2585+
}
2586+
2587+
fn iterate_method_candidates_dyn(
2588+
&self,
2589+
db: &dyn HirDatabase,
2590+
krate: Crate,
2591+
traits_in_scope: &FxHashSet<TraitId>,
2592+
name: Option<&Name>,
2593+
callback: &mut dyn FnMut(&Ty, AssocItemId) -> bool,
2594+
) {
25532595
// There should be no inference vars in types passed here
25542596
// FIXME check that?
25552597
// FIXME replace Unknown by bound vars here
@@ -2559,7 +2601,7 @@ impl Type {
25592601
let env = self.env.clone();
25602602
let krate = krate.id;
25612603

2562-
method_resolution::iterate_method_candidates(
2604+
method_resolution::iterate_method_candidates_dyn(
25632605
&canonical,
25642606
db,
25652607
env,
@@ -2568,11 +2610,8 @@ impl Type {
25682610
None,
25692611
name,
25702612
method_resolution::LookupMode::MethodCall,
2571-
|ty, it| match it {
2572-
AssocItemId::FunctionId(f) => callback(ty, f.into()),
2573-
_ => None,
2574-
},
2575-
)
2613+
callback,
2614+
);
25762615
}
25772616

25782617
pub fn iterate_path_candidates<T>(
@@ -2581,15 +2620,37 @@ impl Type {
25812620
krate: Crate,
25822621
traits_in_scope: &FxHashSet<TraitId>,
25832622
name: Option<&Name>,
2584-
mut callback: impl FnMut(&Ty, AssocItem) -> Option<T>,
2623+
mut callback: impl FnMut(Type, AssocItem) -> Option<T>,
25852624
) -> Option<T> {
25862625
let _p = profile::span("iterate_path_candidates");
2626+
let mut slot = None;
2627+
self.iterate_path_candidates_dyn(
2628+
db,
2629+
krate,
2630+
traits_in_scope,
2631+
name,
2632+
&mut |ty, assoc_item_id| {
2633+
slot = callback(self.derived(ty.clone()), assoc_item_id.into());
2634+
slot.is_some()
2635+
},
2636+
);
2637+
slot
2638+
}
2639+
2640+
fn iterate_path_candidates_dyn(
2641+
&self,
2642+
db: &dyn HirDatabase,
2643+
krate: Crate,
2644+
traits_in_scope: &FxHashSet<TraitId>,
2645+
name: Option<&Name>,
2646+
callback: &mut dyn FnMut(&Ty, AssocItemId) -> bool,
2647+
) {
25872648
let canonical = hir_ty::replace_errors_with_variables(&self.ty);
25882649

25892650
let env = self.env.clone();
25902651
let krate = krate.id;
25912652

2592-
method_resolution::iterate_method_candidates(
2653+
method_resolution::iterate_method_candidates_dyn(
25932654
&canonical,
25942655
db,
25952656
env,
@@ -2598,8 +2659,8 @@ impl Type {
25982659
None,
25992660
name,
26002661
method_resolution::LookupMode::Path,
2601-
|ty, it| callback(ty, it.into()),
2602-
)
2662+
callback,
2663+
);
26032664
}
26042665

26052666
pub fn as_adt(&self) -> Option<Adt> {

crates/hir_ty/src/method_resolution.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ pub fn iterate_method_candidates<T>(
422422
mut callback: impl FnMut(&Ty, AssocItemId) -> Option<T>,
423423
) -> Option<T> {
424424
let mut slot = None;
425-
iterate_method_candidates_impl(
425+
iterate_method_candidates_dyn(
426426
ty,
427427
db,
428428
env,
@@ -440,7 +440,7 @@ pub fn iterate_method_candidates<T>(
440440
slot
441441
}
442442

443-
fn iterate_method_candidates_impl(
443+
pub fn iterate_method_candidates_dyn(
444444
ty: &Canonical<Ty>,
445445
db: &dyn HirDatabase,
446446
env: Arc<TraitEnvironment>,

0 commit comments

Comments
 (0)