Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 4667379

Browse files
committed
Auto merge of rust-lang#14575 - Veykril:attrs, r=Veykril
internal: Put Attrs behind a query instead of AttsWithOwner We call this for pretty much every item, so this is unnecessary wasted memory
2 parents 1a33168 + ed3a5ea commit 4667379

File tree

4 files changed

+23
-9
lines changed

4 files changed

+23
-9
lines changed

crates/hir-def/src/attr.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,11 @@ fn parse_comma_sep<S>(subtree: &tt::Subtree<S>) -> Vec<SmolStr> {
377377
}
378378

379379
impl AttrsWithOwner {
380-
pub(crate) fn attrs_query(db: &dyn DefDatabase, def: AttrDefId) -> Self {
380+
pub(crate) fn attrs_with_owner(db: &dyn DefDatabase, owner: AttrDefId) -> Self {
381+
Self { attrs: db.attrs(owner), owner }
382+
}
383+
384+
pub(crate) fn attrs_query(db: &dyn DefDatabase, def: AttrDefId) -> Attrs {
381385
let _p = profile::span("attrs_query");
382386
// FIXME: this should use `Trace` to avoid duplication in `source_map` below
383387
let raw_attrs = match def {
@@ -412,13 +416,10 @@ impl AttrsWithOwner {
412416
}
413417
}
414418
AttrDefId::FieldId(it) => {
415-
return Self { attrs: db.fields_attrs(it.parent)[it.local_id].clone(), owner: def };
419+
return db.fields_attrs(it.parent)[it.local_id].clone();
416420
}
417421
AttrDefId::EnumVariantId(it) => {
418-
return Self {
419-
attrs: db.variants_attrs(it.parent)[it.local_id].clone(),
420-
owner: def,
421-
};
422+
return db.variants_attrs(it.parent)[it.local_id].clone();
422423
}
423424
AttrDefId::AdtId(it) => match it {
424425
AdtId::StructId(it) => attrs_from_item_tree(it.lookup(db).id, db),
@@ -461,7 +462,7 @@ impl AttrsWithOwner {
461462
};
462463

463464
let attrs = raw_attrs.filter(db.upcast(), def.krate(db));
464-
Self { attrs: Attrs(attrs), owner: def }
465+
Attrs(attrs)
465466
}
466467

467468
pub fn source_map(&self, db: &dyn DefDatabase) -> AttrSourceMap {

crates/hir-def/src/db.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ pub trait DefDatabase: InternDatabase + ExpandDatabase + Upcast<dyn ExpandDataba
9898
#[salsa::invoke(DefMap::block_def_map_query)]
9999
fn block_def_map(&self, block: BlockId) -> Option<Arc<DefMap>>;
100100

101+
// region:data
102+
101103
#[salsa::invoke(StructData::struct_data_query)]
102104
fn struct_data(&self, id: StructId) -> Arc<StructData>;
103105

@@ -153,6 +155,8 @@ pub trait DefDatabase: InternDatabase + ExpandDatabase + Upcast<dyn ExpandDataba
153155
#[salsa::invoke(ProcMacroData::proc_macro_data_query)]
154156
fn proc_macro_data(&self, makro: ProcMacroId) -> Arc<ProcMacroData>;
155157

158+
// endregion:data
159+
156160
#[salsa::invoke(Body::body_with_source_map_query)]
157161
fn body_with_source_map(&self, def: DefWithBodyId) -> (Arc<Body>, Arc<BodySourceMap>);
158162

@@ -165,6 +169,8 @@ pub trait DefDatabase: InternDatabase + ExpandDatabase + Upcast<dyn ExpandDataba
165169
#[salsa::invoke(GenericParams::generic_params_query)]
166170
fn generic_params(&self, def: GenericDefId) -> Interned<GenericParams>;
167171

172+
// region:attrs
173+
168174
#[salsa::invoke(Attrs::variants_attrs_query)]
169175
fn variants_attrs(&self, def: EnumId) -> Arc<ArenaMap<LocalEnumVariantId, Attrs>>;
170176

@@ -184,7 +190,13 @@ pub trait DefDatabase: InternDatabase + ExpandDatabase + Upcast<dyn ExpandDataba
184190
) -> Arc<ArenaMap<LocalFieldId, Either<AstPtr<ast::TupleField>, AstPtr<ast::RecordField>>>>;
185191

186192
#[salsa::invoke(AttrsWithOwner::attrs_query)]
187-
fn attrs(&self, def: AttrDefId) -> AttrsWithOwner;
193+
fn attrs(&self, def: AttrDefId) -> Attrs;
194+
195+
#[salsa::transparent]
196+
#[salsa::invoke(AttrsWithOwner::attrs_with_owner)]
197+
fn attrs_with_owner(&self, def: AttrDefId) -> AttrsWithOwner;
198+
199+
// endregion:attrs
188200

189201
#[salsa::invoke(LangItems::crate_lang_items_query)]
190202
fn crate_lang_items(&self, krate: CrateId) -> Arc<LangItems>;

crates/hir-expand/src/attrs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use crate::{
2121
/// Syntactical attributes, without filtering of `cfg_attr`s.
2222
#[derive(Default, Debug, Clone, PartialEq, Eq)]
2323
pub struct RawAttrs {
24+
// FIXME: Make this a ThinArc
2425
entries: Option<Arc<[Attr]>>,
2526
}
2627

crates/hir/src/attrs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ macro_rules! impl_has_attrs {
4141
impl HasAttrs for $def {
4242
fn attrs(self, db: &dyn HirDatabase) -> AttrsWithOwner {
4343
let def = AttrDefId::$def_id(self.into());
44-
db.attrs(def)
44+
db.attrs_with_owner(def)
4545
}
4646
fn docs(self, db: &dyn HirDatabase) -> Option<Documentation> {
4747
let def = AttrDefId::$def_id(self.into());

0 commit comments

Comments
 (0)