Skip to content

Commit 23cf2eb

Browse files
jseyfriedalexcrichton
authored andcommitted
resolve: improve performance
1 parent ba43dba commit 23cf2eb

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

src/librustc_resolve/lib.rs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,9 @@ pub struct ModuleS<'a> {
850850
glob_importers: RefCell<Vec<(Module<'a>, &'a ImportDirective<'a>)>>,
851851
globs: RefCell<Vec<&'a ImportDirective<'a>>>,
852852

853+
// Used to memoize the traits in this module for faster searches through all traits in scope.
854+
traits: RefCell<Option<Box<[&'a NameBinding<'a>]>>>,
855+
853856
// Whether this module is populated. If not populated, any attempt to
854857
// access the children must be preceded with a
855858
// `populate_module_if_necessary` call.
@@ -877,6 +880,7 @@ impl<'a> ModuleS<'a> {
877880
prelude: RefCell::new(None),
878881
glob_importers: RefCell::new(Vec::new()),
879882
globs: RefCell::new((Vec::new())),
883+
traits: RefCell::new(None),
880884
populated: Cell::new(!external),
881885
arenas: arenas
882886
}
@@ -3233,18 +3237,28 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
32333237
let mut search_module = self.current_module;
32343238
loop {
32353239
// Look for trait children.
3236-
let mut search_in_module = |module: Module<'a>| module.for_each_child(|_, ns, binding| {
3237-
if ns != TypeNS { return }
3238-
let trait_def_id = match binding.def() {
3239-
Some(Def::Trait(trait_def_id)) => trait_def_id,
3240-
Some(..) | None => return,
3241-
};
3242-
if self.trait_item_map.contains_key(&(name, trait_def_id)) {
3243-
add_trait_info(&mut found_traits, trait_def_id, name);
3244-
let trait_name = self.get_trait_name(trait_def_id);
3245-
self.record_use(trait_name, TypeNS, binding);
3240+
let mut search_in_module = |module: Module<'a>| {
3241+
let mut traits = module.traits.borrow_mut();
3242+
if traits.is_none() {
3243+
let mut collected_traits = Vec::new();
3244+
module.for_each_child(|_, ns, binding| {
3245+
if ns != TypeNS { return }
3246+
if let Some(Def::Trait(_)) = binding.def() {
3247+
collected_traits.push(binding);
3248+
}
3249+
});
3250+
*traits = Some(collected_traits.into_boxed_slice());
32463251
}
3247-
});
3252+
3253+
for binding in traits.as_ref().unwrap().iter() {
3254+
let trait_def_id = binding.def().unwrap().def_id();
3255+
if self.trait_item_map.contains_key(&(name, trait_def_id)) {
3256+
add_trait_info(&mut found_traits, trait_def_id, name);
3257+
let trait_name = self.get_trait_name(trait_def_id);
3258+
self.record_use(trait_name, TypeNS, binding);
3259+
}
3260+
}
3261+
};
32483262
search_in_module(search_module);
32493263

32503264
match search_module.parent_link {

0 commit comments

Comments
 (0)