Skip to content

Commit 76b60ef

Browse files
committed
internal: improve compile times a bit
before after for cargo llvm-lines -q --lib --release -p ide_ssr | head -n 24 Lines Copies Function name ----- ------ ------------- 297146 (100%) 12748 (100%) (TOTAL) 5970 (2.0%) 47 (0.4%) core::iter::traits::iterator::Iterator::try_fold 4750 (1.6%) 27 (0.2%) hashbrown::raw::RawTable<T,A>::resize 4080 (1.4%) 30 (0.2%) alloc::raw_vec::RawVec<T,A>::grow_amortized 3933 (1.3%) 69 (0.5%) alloc::raw_vec::RawVec<T,A>::current_memory 3668 (1.2%) 89 (0.7%) <core::result::Result<T,E> as core::ops::try_trait::Try>::branch 3500 (1.2%) 50 (0.4%) hashbrown::raw::RawTable<T,A>::drop_elements 3436 (1.2%) 33 (0.3%) hashbrown::raw::RawTable<T,A>::find 3415 (1.1%) 17 (0.1%) hashbrown::raw::RawTable<T,A>::rehash_in_place 3400 (1.1%) 50 (0.4%) <hashbrown::raw::RawIterRange<T> as core::iter::traits::iterator::Iterator>::next 2840 (1.0%) 20 (0.2%) alloc::raw_vec::RawVec<T,A>::allocate_in 2700 (0.9%) 30 (0.2%) core::alloc::layout::Layout::array 2666 (0.9%) 86 (0.7%) core::ptr::metadata::from_raw_parts_mut 2495 (0.8%) 50 (0.4%) core::option::Option<T>::map 2354 (0.8%) 38 (0.3%) alloc::alloc::box_free 2302 (0.8%) 7 (0.1%) ide_ssr::parsing::RuleBuilder::try_add 2146 (0.7%) 45 (0.4%) core::mem::replace 2070 (0.7%) 69 (0.5%) <alloc::raw_vec::RawVec<T,A> as core::ops::drop::Drop>::drop 1979 (0.7%) 16 (0.1%) hashbrown::map::HashMap<K,V,S,A>::insert 1926 (0.6%) 18 (0.1%) <core::iter::adapters::zip::Zip<A,B> as core::iter::adapters::zip::ZipImpl<A,B>>::next 1922 (0.6%) 62 (0.5%) core::fmt::ArgumentV1::new 1885 (0.6%) 13 (0.1%) alloc::raw_vec::RawVec<T,A>::shrink Lines Copies Function name ----- ------ ------------- 261717 (100%) 11666 (100%) (TOTAL) 5239 (2.0%) 42 (0.4%) core::iter::traits::iterator::Iterator::try_fold 4750 (1.8%) 27 (0.2%) hashbrown::raw::RawTable<T,A>::resize 3933 (1.5%) 69 (0.6%) alloc::raw_vec::RawVec<T,A>::current_memory 3536 (1.4%) 26 (0.2%) alloc::raw_vec::RawVec<T,A>::grow_amortized 3500 (1.3%) 50 (0.4%) hashbrown::raw::RawTable<T,A>::drop_elements 3400 (1.3%) 50 (0.4%) <hashbrown::raw::RawIterRange<T> as core::iter::traits::iterator::Iterator>::next 3124 (1.2%) 30 (0.3%) hashbrown::raw::RawTable<T,A>::find 2812 (1.1%) 14 (0.1%) hashbrown::raw::RawTable<T,A>::rehash_in_place 2604 (1.0%) 84 (0.7%) core::ptr::metadata::from_raw_parts_mut 2340 (0.9%) 26 (0.2%) core::alloc::layout::Layout::array 2302 (0.9%) 7 (0.1%) ide_ssr::parsing::RuleBuilder::try_add 2272 (0.9%) 16 (0.1%) alloc::raw_vec::RawVec<T,A>::allocate_in 2201 (0.8%) 35 (0.3%) alloc::alloc::box_free 2104 (0.8%) 44 (0.4%) core::mem::replace 2079 (0.8%) 42 (0.4%) <core::result::Result<T,E> as core::ops::try_trait::Try>::branch 2070 (0.8%) 69 (0.6%) <alloc::raw_vec::RawVec<T,A> as core::ops::drop::Drop>::drop 1926 (0.7%) 18 (0.2%) <core::iter::adapters::zip::Zip<A,B> as core::iter::adapters::zip::ZipImpl<A,B>>::next 1885 (0.7%) 13 (0.1%) alloc::raw_vec::RawVec<T,A>::shrink 1833 (0.7%) 13 (0.1%) hashbrown::raw::RawTable<T,A>::shrink_to 1771 (0.7%) 91 (0.8%) core::ptr::read 1701 (0.6%) 35 (0.3%) core::option::Option<T>::map
1 parent 35d9807 commit 76b60ef

File tree

2 files changed

+77
-16
lines changed

2 files changed

+77
-16
lines changed

crates/hir/src/lib.rs

Lines changed: 74 additions & 13 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> + '_ {
@@ -2550,6 +2567,31 @@ impl Type {
25502567
mut callback: impl FnMut(&Ty, 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(ty, 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>(
@@ -2584,12 +2623,34 @@ impl Type {
25842623
mut callback: impl FnMut(&Ty, 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(ty, 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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ pub enum LookupMode {
410410
// This would be nicer if it just returned an iterator, but that runs into
411411
// lifetime problems, because we need to borrow temp `CrateImplDefs`.
412412
// FIXME add a context type here?
413-
pub fn iterate_method_candidates<T>(
413+
pub fn iterate_method_candidates<T>(
414414
ty: &Canonical<Ty>,
415415
db: &dyn HirDatabase,
416416
env: Arc<TraitEnvironment>,
@@ -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)