Don't allocate DefDatabase::crate_lang_items if its empty

This commit is contained in:
Lukas Wirth 2024-01-09 21:06:21 +01:00
parent 9673915045
commit 4e16e0f2c1
4 changed files with 15 additions and 12 deletions

View File

@ -355,7 +355,7 @@ fn parse_comma_sep<S>(subtree: &tt::Subtree<S>) -> Vec<SmolStr> {
}
impl AttrsWithOwner {
pub(crate) fn attrs_with_owner(db: &dyn DefDatabase, owner: AttrDefId) -> Self {
pub fn attrs_with_owner(db: &dyn DefDatabase, owner: AttrDefId) -> Self {
Self { attrs: db.attrs(owner), owner }
}

View File

@ -214,10 +214,6 @@ fn fields_attrs_source_map(
#[salsa::invoke(lang_item::lang_attr_query)]
fn lang_attr(&self, def: AttrDefId) -> Option<LangItem>;
#[salsa::transparent]
#[salsa::invoke(AttrsWithOwner::attrs_with_owner)]
fn attrs_with_owner(&self, def: AttrDefId) -> AttrsWithOwner;
// endregion:attrs
#[salsa::invoke(LangItems::lang_item_query)]
@ -241,7 +237,7 @@ fn fields_attrs_source_map(
// endregion:visibilities
#[salsa::invoke(LangItems::crate_lang_items_query)]
fn crate_lang_items(&self, krate: CrateId) -> Arc<LangItems>;
fn crate_lang_items(&self, krate: CrateId) -> Option<Arc<LangItems>>;
fn crate_supports_no_std(&self, crate_id: CrateId) -> bool;
}

View File

@ -87,7 +87,10 @@ pub fn target(&self, item: LangItem) -> Option<LangItemTarget> {
}
/// Salsa query. This will look for lang items in a specific crate.
pub(crate) fn crate_lang_items_query(db: &dyn DefDatabase, krate: CrateId) -> Arc<LangItems> {
pub(crate) fn crate_lang_items_query(
db: &dyn DefDatabase,
krate: CrateId,
) -> Option<Arc<LangItems>> {
let _p = profile::span("crate_lang_items_query");
let mut lang_items = LangItems::default();
@ -150,7 +153,11 @@ pub(crate) fn crate_lang_items_query(db: &dyn DefDatabase, krate: CrateId) -> Ar
}
}
Arc::new(lang_items)
if lang_items.items.is_empty() {
None
} else {
Some(Arc::new(lang_items))
}
}
/// Salsa query. Look for a lang item, starting from the specified crate and recursively
@ -161,9 +168,9 @@ pub(crate) fn lang_item_query(
item: LangItem,
) -> Option<LangItemTarget> {
let _p = profile::span("lang_item_query");
let lang_items = db.crate_lang_items(start_crate);
let start_crate_target = lang_items.items.get(&item);
if let Some(&target) = start_crate_target {
if let Some(target) =
db.crate_lang_items(start_crate).and_then(|it| it.items.get(&item).copied())
{
return Some(target);
}
db.crate_graph()[start_crate]

View File

@ -35,7 +35,7 @@ macro_rules! impl_has_attrs {
impl HasAttrs for $def {
fn attrs(self, db: &dyn HirDatabase) -> AttrsWithOwner {
let def = AttrDefId::$def_id(self.into());
db.attrs_with_owner(def)
AttrsWithOwner::attrs_with_owner(db.upcast(), def)
}
fn attr_id(self) -> AttrDefId {
AttrDefId::$def_id(self.into())