Do less allocs in trait_data and impl_data queries

This commit is contained in:
Lukas Wirth 2022-01-08 11:04:16 +01:00
parent 6746ba5839
commit 8d4f40e881

View file

@ -184,6 +184,7 @@ pub struct TraitData {
/// method calls to this trait's methods when the receiver is an array and the crate edition is /// method calls to this trait's methods when the receiver is an array and the crate edition is
/// 2015 or 2018. /// 2015 or 2018.
pub skip_array_during_method_dispatch: bool, pub skip_array_during_method_dispatch: bool,
// box it as the vec is usually empty anyways
pub attribute_calls: Option<Box<Vec<(AstId<ast::Item>, MacroCallId)>>>, pub attribute_calls: Option<Box<Vec<(AstId<ast::Item>, MacroCallId)>>>,
} }
@ -208,17 +209,8 @@ impl TraitData {
.by_key("rustc_skip_array_during_method_dispatch") .by_key("rustc_skip_array_during_method_dispatch")
.exists(); .exists();
let (attribute_calls, items) = collect_items( let (items, attribute_calls) =
db, do_collect(db, module_id, &mut expander, &tr_def.items, tr_loc.id.tree_id(), container);
module_id,
&mut expander,
tr_def.items.iter().copied(),
tr_loc.id.tree_id(),
container,
100,
);
let attribute_calls =
if attribute_calls.is_empty() { None } else { Some(Box::new(attribute_calls)) };
Arc::new(TraitData { Arc::new(TraitData {
name, name,
@ -263,6 +255,7 @@ pub struct ImplData {
pub self_ty: Interned<TypeRef>, pub self_ty: Interned<TypeRef>,
pub items: Vec<AssocItemId>, pub items: Vec<AssocItemId>,
pub is_negative: bool, pub is_negative: bool,
// box it as the vec is usually empty anyways
pub attribute_calls: Option<Box<Vec<(AstId<ast::Item>, MacroCallId)>>>, pub attribute_calls: Option<Box<Vec<(AstId<ast::Item>, MacroCallId)>>>,
} }
@ -280,18 +273,15 @@ impl ImplData {
let container = ItemContainerId::ImplId(id); let container = ItemContainerId::ImplId(id);
let mut expander = Expander::new(db, impl_loc.id.file_id(), module_id); let mut expander = Expander::new(db, impl_loc.id.file_id(), module_id);
let (attribute_calls, items) = collect_items( let (items, attribute_calls) = do_collect(
db, db,
module_id, module_id,
&mut expander, &mut expander,
impl_def.items.iter().copied(), &impl_def.items,
impl_loc.id.tree_id(), impl_loc.id.tree_id(),
container, container,
100,
); );
let items = items.into_iter().map(|(_, item)| item).collect(); let items = items.into_iter().map(|(_, item)| item).collect();
let attribute_calls =
if attribute_calls.is_empty() { None } else { Some(Box::new(attribute_calls)) };
Arc::new(ImplData { target_trait, self_ty, items, is_negative, attribute_calls }) Arc::new(ImplData { target_trait, self_ty, items, is_negative, attribute_calls })
} }
@ -348,17 +338,47 @@ impl StaticData {
} }
} }
fn do_collect(
db: &dyn DefDatabase,
module_id: ModuleId,
expander: &mut Expander,
assoc_items: &[AssocItem],
tree_id: item_tree::TreeId,
container: ItemContainerId,
) -> (Vec<(Name, AssocItemId)>, Option<Box<Vec<(AstId<ast::Item>, MacroCallId)>>>) {
let mut items = Vec::new();
let mut attribute_calls = Vec::new();
collect_items(
db,
&mut items,
&mut attribute_calls,
module_id,
expander,
assoc_items.iter().copied(),
tree_id,
container,
100,
);
let attribute_calls =
if attribute_calls.is_empty() { None } else { Some(Box::new(attribute_calls)) };
(items, attribute_calls)
}
fn collect_items( fn collect_items(
db: &dyn DefDatabase, db: &dyn DefDatabase,
items: &mut Vec<(Name, AssocItemId)>,
attr_calls: &mut Vec<(AstId<ast::Item>, MacroCallId)>,
module: ModuleId, module: ModuleId,
expander: &mut Expander, expander: &mut Expander,
assoc_items: impl Iterator<Item = AssocItem>, assoc_items: impl Iterator<Item = AssocItem>,
tree_id: item_tree::TreeId, tree_id: item_tree::TreeId,
container: ItemContainerId, container: ItemContainerId,
limit: usize, limit: usize,
) -> (Vec<(AstId<ast::Item>, MacroCallId)>, Vec<(Name, AssocItemId)>) { ) {
if limit == 0 { if limit == 0 {
return Default::default(); return;
} }
let item_tree = tree_id.item_tree(db); let item_tree = tree_id.item_tree(db);
@ -366,26 +386,22 @@ fn collect_items(
let cfg_options = &crate_graph[module.krate].cfg_options; let cfg_options = &crate_graph[module.krate].cfg_options;
let def_map = module.def_map(db); let def_map = module.def_map(db);
let mut items = Vec::new();
let mut attribute_calls = Vec::new();
'items: for item in assoc_items { 'items: for item in assoc_items {
let attrs = item_tree.attrs(db, module.krate, ModItem::from(item).into()); let attrs = item_tree.attrs(db, module.krate, ModItem::from(item).into());
if !attrs.is_cfg_enabled(cfg_options) { if !attrs.is_cfg_enabled(cfg_options) {
continue; continue;
} }
for attr in &*attrs { for attr in &*attrs {
let ast_id = AstId::new(expander.current_file_id(), item.ast_id(&item_tree).upcast()); let ast_id = AstId::new(expander.current_file_id(), item.ast_id(&item_tree).upcast());
let ast_id_with_path = AstIdWithPath { path: (*attr.path).clone(), ast_id }; let ast_id_with_path = AstIdWithPath { path: (*attr.path).clone(), ast_id };
if let Ok(ResolvedAttr::Macro(call_id)) = if let Ok(ResolvedAttr::Macro(call_id)) =
def_map.resolve_attr_macro(db, module.local_id, ast_id_with_path, attr) def_map.resolve_attr_macro(db, module.local_id, ast_id_with_path, attr)
{ {
attribute_calls.push((ast_id, call_id)); attr_calls.push((ast_id, call_id));
let res = expander.enter_expand_id(db, call_id); let res = expander.enter_expand_id(db, call_id);
let (mac_attrs, mac_items) = collect_macro_items(db, items, attr_calls, module, expander, container, limit, res);
collect_macro_items(db, module, expander, container, limit, res);
items.extend(mac_items);
attribute_calls.extend(mac_attrs);
continue 'items; continue 'items;
} }
} }
@ -419,37 +435,32 @@ fn collect_items(
let res = expander.enter_expand(db, call); let res = expander.enter_expand(db, call);
if let Ok(res) = res { if let Ok(res) = res {
let (mac_attrs, mac_items) = collect_macro_items(
collect_macro_items(db, module, expander, container, limit, res); db, items, attr_calls, module, expander, container, limit, res,
items.extend(mac_items); );
attribute_calls.extend(mac_attrs);
} }
} }
} }
} }
(attribute_calls, items)
} }
fn collect_macro_items( fn collect_macro_items(
db: &dyn DefDatabase, db: &dyn DefDatabase,
items: &mut Vec<(Name, AssocItemId)>,
attr_calls: &mut Vec<(AstId<ast::Item>, MacroCallId)>,
module: ModuleId, module: ModuleId,
expander: &mut Expander, expander: &mut Expander,
container: ItemContainerId, container: ItemContainerId,
limit: usize, limit: usize,
res: ExpandResult<Option<(Mark, ast::MacroItems)>>, res: ExpandResult<Option<(Mark, ast::MacroItems)>>,
) -> (Vec<(AstId<ast::Item>, MacroCallId)>, Vec<(Name, AssocItemId)>) { ) {
if let Some((mark, mac)) = res.value { if let Some((mark, mac)) = res.value {
let src: InFile<ast::MacroItems> = expander.to_source(mac); let src: InFile<ast::MacroItems> = expander.to_source(mac);
let tree_id = item_tree::TreeId::new(src.file_id, None); let tree_id = item_tree::TreeId::new(src.file_id, None);
let item_tree = tree_id.item_tree(db); let item_tree = tree_id.item_tree(db);
let iter = item_tree.top_level_items().iter().filter_map(ModItem::as_assoc_item); let iter = item_tree.top_level_items().iter().filter_map(ModItem::as_assoc_item);
let items = collect_items(db, module, expander, iter, tree_id, container, limit - 1); collect_items(db, items, attr_calls, module, expander, iter, tree_id, container, limit - 1);
expander.exit(db, mark); expander.exit(db, mark);
return items;
} }
Default::default()
} }