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

Commit 7cf4cc7

Browse files
committed
Auto merge of rust-lang#17487 - Veykril:ty-perf-stuff, r=Veykril
internal: Some more small memory optimizations Not a big impact on metrics, though there are some more savings in queries mainly used by the IDE layer from this
2 parents 1cd8bc0 + 0b69c60 commit 7cf4cc7

39 files changed

+340
-260
lines changed

src/tools/rust-analyzer/Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,6 +1046,7 @@ checksum = "75761162ae2b0e580d7e7c390558127e5f01b4194debd6221fd8c207fc80e3f5"
10461046
name = "mbe"
10471047
version = "0.0.0"
10481048
dependencies = [
1049+
"arrayvec",
10491050
"cov-mark",
10501051
"parser",
10511052
"rustc-hash",

src/tools/rust-analyzer/crates/hir-def/src/data.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ impl TraitAliasData {
323323
pub struct ImplData {
324324
pub target_trait: Option<Interned<TraitRef>>,
325325
pub self_ty: Interned<TypeRef>,
326-
pub items: Vec<AssocItemId>,
326+
pub items: Box<[AssocItemId]>,
327327
pub is_negative: bool,
328328
pub is_unsafe: bool,
329329
// box it as the vec is usually empty anyways

src/tools/rust-analyzer/crates/hir-def/src/db.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,11 @@ pub trait InternDatabase: SourceDatabase {
8080

8181
#[salsa::query_group(DefDatabaseStorage)]
8282
pub trait DefDatabase: InternDatabase + ExpandDatabase + Upcast<dyn ExpandDatabase> {
83+
/// Whether to expand procedural macros during name resolution.
8384
#[salsa::input]
8485
fn expand_proc_attr_macros(&self) -> bool;
8586

87+
/// Computes an [`ItemTree`] for the given file or macro expansion.
8688
#[salsa::invoke(ItemTree::file_item_tree_query)]
8789
fn file_item_tree(&self, file_id: HirFileId) -> Arc<ItemTree>;
8890

@@ -96,6 +98,7 @@ pub trait DefDatabase: InternDatabase + ExpandDatabase + Upcast<dyn ExpandDataba
9698
#[salsa::invoke(DefMap::block_def_map_query)]
9799
fn block_def_map(&self, block: BlockId) -> Arc<DefMap>;
98100

101+
/// Turns a MacroId into a MacroDefId, describing the macro's definition post name resolution.
99102
fn macro_def(&self, m: MacroId) -> MacroDefId;
100103

101104
// region:data
@@ -190,6 +193,7 @@ pub trait DefDatabase: InternDatabase + ExpandDatabase + Upcast<dyn ExpandDataba
190193
#[salsa::invoke(Attrs::fields_attrs_query)]
191194
fn fields_attrs(&self, def: VariantId) -> Arc<ArenaMap<LocalFieldId, Attrs>>;
192195

196+
// should this really be a query?
193197
#[salsa::invoke(crate::attr::fields_attrs_source_map)]
194198
fn fields_attrs_source_map(
195199
&self,

src/tools/rust-analyzer/crates/hir-def/src/generics.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -582,13 +582,11 @@ impl GenericParams {
582582
GenericDefId::TraitAliasId(id) => id_to_generics(db, id, enabled_params),
583583
GenericDefId::TypeAliasId(id) => id_to_generics(db, id, enabled_params),
584584
GenericDefId::ImplId(id) => id_to_generics(db, id, enabled_params),
585-
GenericDefId::EnumVariantId(_) | GenericDefId::ConstId(_) => {
586-
Interned::new(GenericParams {
587-
type_or_consts: Default::default(),
588-
lifetimes: Default::default(),
589-
where_predicates: Default::default(),
590-
})
591-
}
585+
GenericDefId::ConstId(_) => Interned::new(GenericParams {
586+
type_or_consts: Default::default(),
587+
lifetimes: Default::default(),
588+
where_predicates: Default::default(),
589+
}),
592590
}
593591
}
594592

src/tools/rust-analyzer/crates/hir-def/src/item_tree.rs

Lines changed: 56 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ use either::Either;
4848
use hir_expand::{attrs::RawAttrs, name::Name, ExpandTo, HirFileId, InFile};
4949
use intern::Interned;
5050
use la_arena::{Arena, Idx, IdxRange, RawIdx};
51+
use once_cell::sync::OnceCell;
5152
use rustc_hash::FxHashMap;
5253
use smallvec::SmallVec;
5354
use span::{AstIdNode, FileAstId, SyntaxContextId};
@@ -100,6 +101,7 @@ pub struct ItemTree {
100101
impl ItemTree {
101102
pub(crate) fn file_item_tree_query(db: &dyn DefDatabase, file_id: HirFileId) -> Arc<ItemTree> {
102103
let _p = tracing::info_span!("file_item_tree_query", ?file_id).entered();
104+
static EMPTY: OnceCell<Arc<ItemTree>> = OnceCell::new();
103105

104106
let syntax = db.parse_or_expand(file_id);
105107

@@ -131,18 +133,47 @@ impl ItemTree {
131133
if let Some(attrs) = top_attrs {
132134
item_tree.attrs.insert(AttrOwner::TopLevel, attrs);
133135
}
134-
item_tree.shrink_to_fit();
135-
Arc::new(item_tree)
136+
if item_tree.data.is_none() && item_tree.top_level.is_empty() && item_tree.attrs.is_empty()
137+
{
138+
EMPTY
139+
.get_or_init(|| {
140+
Arc::new(ItemTree {
141+
top_level: SmallVec::new_const(),
142+
attrs: FxHashMap::default(),
143+
data: None,
144+
})
145+
})
146+
.clone()
147+
} else {
148+
item_tree.shrink_to_fit();
149+
Arc::new(item_tree)
150+
}
136151
}
137152

138153
pub(crate) fn block_item_tree_query(db: &dyn DefDatabase, block: BlockId) -> Arc<ItemTree> {
154+
let _p = tracing::info_span!("block_item_tree_query", ?block).entered();
155+
static EMPTY: OnceCell<Arc<ItemTree>> = OnceCell::new();
156+
139157
let loc = block.lookup(db);
140158
let block = loc.ast_id.to_node(db.upcast());
141159

142160
let ctx = lower::Ctx::new(db, loc.ast_id.file_id);
143161
let mut item_tree = ctx.lower_block(&block);
144-
item_tree.shrink_to_fit();
145-
Arc::new(item_tree)
162+
if item_tree.data.is_none() && item_tree.top_level.is_empty() && item_tree.attrs.is_empty()
163+
{
164+
EMPTY
165+
.get_or_init(|| {
166+
Arc::new(ItemTree {
167+
top_level: SmallVec::new_const(),
168+
attrs: FxHashMap::default(),
169+
data: None,
170+
})
171+
})
172+
.clone()
173+
} else {
174+
item_tree.shrink_to_fit();
175+
Arc::new(item_tree)
176+
}
146177
}
147178

148179
/// Returns an iterator over all items located at the top level of the `HirFileId` this
@@ -585,24 +616,30 @@ impl Index<RawVisibilityId> for ItemTree {
585616
type Output = RawVisibility;
586617
fn index(&self, index: RawVisibilityId) -> &Self::Output {
587618
static VIS_PUB: RawVisibility = RawVisibility::Public;
588-
static VIS_PRIV_IMPLICIT: RawVisibility = RawVisibility::Module(
589-
ModPath::from_kind(PathKind::SELF),
590-
VisibilityExplicitness::Implicit,
591-
);
592-
static VIS_PRIV_EXPLICIT: RawVisibility = RawVisibility::Module(
593-
ModPath::from_kind(PathKind::SELF),
594-
VisibilityExplicitness::Explicit,
595-
);
596-
static VIS_PUB_CRATE: RawVisibility = RawVisibility::Module(
597-
ModPath::from_kind(PathKind::Crate),
598-
VisibilityExplicitness::Explicit,
599-
);
619+
static VIS_PRIV_IMPLICIT: OnceCell<RawVisibility> = OnceCell::new();
620+
static VIS_PRIV_EXPLICIT: OnceCell<RawVisibility> = OnceCell::new();
621+
static VIS_PUB_CRATE: OnceCell<RawVisibility> = OnceCell::new();
600622

601623
match index {
602-
RawVisibilityId::PRIV_IMPLICIT => &VIS_PRIV_IMPLICIT,
603-
RawVisibilityId::PRIV_EXPLICIT => &VIS_PRIV_EXPLICIT,
624+
RawVisibilityId::PRIV_IMPLICIT => VIS_PRIV_IMPLICIT.get_or_init(|| {
625+
RawVisibility::Module(
626+
Interned::new(ModPath::from_kind(PathKind::SELF)),
627+
VisibilityExplicitness::Implicit,
628+
)
629+
}),
630+
RawVisibilityId::PRIV_EXPLICIT => VIS_PRIV_EXPLICIT.get_or_init(|| {
631+
RawVisibility::Module(
632+
Interned::new(ModPath::from_kind(PathKind::SELF)),
633+
VisibilityExplicitness::Explicit,
634+
)
635+
}),
604636
RawVisibilityId::PUB => &VIS_PUB,
605-
RawVisibilityId::PUB_CRATE => &VIS_PUB_CRATE,
637+
RawVisibilityId::PUB_CRATE => VIS_PUB_CRATE.get_or_init(|| {
638+
RawVisibility::Module(
639+
Interned::new(ModPath::from_kind(PathKind::Crate)),
640+
VisibilityExplicitness::Explicit,
641+
)
642+
}),
606643
_ => &self.data().vis.arena[Idx::from_raw(index.0.into())],
607644
}
608645
}

src/tools/rust-analyzer/crates/hir-def/src/lib.rs

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,7 @@ pub enum TypeOwnerId {
689689
}
690690

691691
impl TypeOwnerId {
692-
fn as_generic_def_id(self) -> Option<GenericDefId> {
692+
fn as_generic_def_id(self, db: &dyn DefDatabase) -> Option<GenericDefId> {
693693
Some(match self {
694694
TypeOwnerId::FunctionId(it) => GenericDefId::FunctionId(it),
695695
TypeOwnerId::ConstId(it) => GenericDefId::ConstId(it),
@@ -698,7 +698,9 @@ impl TypeOwnerId {
698698
TypeOwnerId::TraitAliasId(it) => GenericDefId::TraitAliasId(it),
699699
TypeOwnerId::TypeAliasId(it) => GenericDefId::TypeAliasId(it),
700700
TypeOwnerId::ImplId(it) => GenericDefId::ImplId(it),
701-
TypeOwnerId::EnumVariantId(it) => GenericDefId::EnumVariantId(it),
701+
TypeOwnerId::EnumVariantId(it) => {
702+
GenericDefId::AdtId(AdtId::EnumId(it.lookup(db).parent))
703+
}
702704
TypeOwnerId::InTypeConstId(_) | TypeOwnerId::StaticId(_) => return None,
703705
})
704706
}
@@ -740,7 +742,6 @@ impl From<GenericDefId> for TypeOwnerId {
740742
GenericDefId::TraitAliasId(it) => it.into(),
741743
GenericDefId::TypeAliasId(it) => it.into(),
742744
GenericDefId::ImplId(it) => it.into(),
743-
GenericDefId::EnumVariantId(it) => it.into(),
744745
GenericDefId::ConstId(it) => it.into(),
745746
}
746747
}
@@ -849,8 +850,8 @@ impl GeneralConstId {
849850
pub fn generic_def(self, db: &dyn DefDatabase) -> Option<GenericDefId> {
850851
match self {
851852
GeneralConstId::ConstId(it) => Some(it.into()),
852-
GeneralConstId::ConstBlockId(it) => it.lookup(db).parent.as_generic_def_id(),
853-
GeneralConstId::InTypeConstId(it) => it.lookup(db).owner.as_generic_def_id(),
853+
GeneralConstId::ConstBlockId(it) => it.lookup(db).parent.as_generic_def_id(db),
854+
GeneralConstId::InTypeConstId(it) => it.lookup(db).owner.as_generic_def_id(db),
854855
}
855856
}
856857

@@ -888,12 +889,12 @@ impl From<EnumVariantId> for DefWithBodyId {
888889
}
889890

890891
impl DefWithBodyId {
891-
pub fn as_generic_def_id(self) -> Option<GenericDefId> {
892+
pub fn as_generic_def_id(self, db: &dyn DefDatabase) -> Option<GenericDefId> {
892893
match self {
893894
DefWithBodyId::FunctionId(f) => Some(f.into()),
894895
DefWithBodyId::StaticId(_) => None,
895896
DefWithBodyId::ConstId(c) => Some(c.into()),
896-
DefWithBodyId::VariantId(c) => Some(c.into()),
897+
DefWithBodyId::VariantId(c) => Some(c.lookup(db).parent.into()),
897898
// FIXME: stable rust doesn't allow generics in constants, but we should
898899
// use `TypeOwnerId::as_generic_def_id` when it does.
899900
DefWithBodyId::InTypeConstId(_) => None,
@@ -921,10 +922,6 @@ pub enum GenericDefId {
921922
TraitAliasId(TraitAliasId),
922923
TypeAliasId(TypeAliasId),
923924
ImplId(ImplId),
924-
// enum variants cannot have generics themselves, but their parent enums
925-
// can, and this makes some code easier to write
926-
// FIXME: Try to remove this as that will reduce the amount of query slots generated per enum?
927-
EnumVariantId(EnumVariantId),
928925
// consts can have type parameters from their parents (i.e. associated consts of traits)
929926
ConstId(ConstId),
930927
}
@@ -935,7 +932,6 @@ impl_from!(
935932
TraitAliasId,
936933
TypeAliasId,
937934
ImplId,
938-
EnumVariantId,
939935
ConstId
940936
for GenericDefId
941937
);
@@ -967,7 +963,6 @@ impl GenericDefId {
967963
GenericDefId::TraitAliasId(it) => file_id_and_params_of_item_loc(db, it),
968964
GenericDefId::ImplId(it) => file_id_and_params_of_item_loc(db, it),
969965
GenericDefId::ConstId(it) => (it.lookup(db).id.file_id(), None),
970-
GenericDefId::EnumVariantId(it) => (it.lookup(db).id.file_id(), None),
971966
}
972967
}
973968

@@ -982,6 +977,14 @@ impl GenericDefId {
982977
_ => None,
983978
}
984979
}
980+
981+
pub fn from_callable(db: &dyn DefDatabase, def: CallableDefId) -> GenericDefId {
982+
match def {
983+
CallableDefId::FunctionId(f) => f.into(),
984+
CallableDefId::StructId(s) => s.into(),
985+
CallableDefId::EnumVariantId(e) => e.lookup(db).parent.into(),
986+
}
987+
}
985988
}
986989

987990
impl From<AssocItemId> for GenericDefId {
@@ -994,6 +997,36 @@ impl From<AssocItemId> for GenericDefId {
994997
}
995998
}
996999

1000+
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
1001+
pub enum CallableDefId {
1002+
FunctionId(FunctionId),
1003+
StructId(StructId),
1004+
EnumVariantId(EnumVariantId),
1005+
}
1006+
1007+
impl InternValueTrivial for CallableDefId {}
1008+
1009+
impl_from!(FunctionId, StructId, EnumVariantId for CallableDefId);
1010+
impl From<CallableDefId> for ModuleDefId {
1011+
fn from(def: CallableDefId) -> ModuleDefId {
1012+
match def {
1013+
CallableDefId::FunctionId(f) => ModuleDefId::FunctionId(f),
1014+
CallableDefId::StructId(s) => ModuleDefId::AdtId(AdtId::StructId(s)),
1015+
CallableDefId::EnumVariantId(e) => ModuleDefId::EnumVariantId(e),
1016+
}
1017+
}
1018+
}
1019+
1020+
impl CallableDefId {
1021+
pub fn krate(self, db: &dyn DefDatabase) -> CrateId {
1022+
match self {
1023+
CallableDefId::FunctionId(f) => f.krate(db),
1024+
CallableDefId::StructId(s) => s.krate(db),
1025+
CallableDefId::EnumVariantId(e) => e.krate(db),
1026+
}
1027+
}
1028+
}
1029+
9971030
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
9981031
pub enum AttrDefId {
9991032
ModuleId(ModuleId),
@@ -1310,7 +1343,6 @@ impl HasModule for GenericDefId {
13101343
GenericDefId::TraitAliasId(it) => it.module(db),
13111344
GenericDefId::TypeAliasId(it) => it.module(db),
13121345
GenericDefId::ImplId(it) => it.module(db),
1313-
GenericDefId::EnumVariantId(it) => it.module(db),
13141346
GenericDefId::ConstId(it) => it.module(db),
13151347
}
13161348
}

src/tools/rust-analyzer/crates/hir-def/src/nameres.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,13 @@ const PREDEFINED_TOOLS: &[SmolStr] = &[
103103
/// is computed by the `block_def_map` query.
104104
#[derive(Debug, PartialEq, Eq)]
105105
pub struct DefMap {
106+
/// The crate this `DefMap` belongs to.
107+
krate: CrateId,
106108
/// When this is a block def map, this will hold the block id of the block and module that
107109
/// contains this block.
108110
block: Option<BlockInfo>,
109111
/// The modules and their data declared in this crate.
110112
pub modules: Arena<ModuleData>,
111-
krate: CrateId,
112113
/// The prelude module for this crate. This either comes from an import
113114
/// marked with the `prelude_import` attribute, or (in the normal case) from
114115
/// a dependency (`std` or `core`).
@@ -124,6 +125,7 @@ pub struct DefMap {
124125

125126
/// Tracks which custom derives are in scope for an item, to allow resolution of derive helper
126127
/// attributes.
128+
// FIXME: Figure out a better way for the IDE layer to resolve these?
127129
derive_helpers_in_scope: FxHashMap<AstId<ast::Item>, Vec<(Name, MacroId, MacroCallId)>>,
128130

129131
/// The diagnostics that need to be emitted for this crate.

src/tools/rust-analyzer/crates/hir-def/src/resolver.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1164,7 +1164,6 @@ impl HasResolver for GenericDefId {
11641164
GenericDefId::TraitAliasId(inner) => inner.resolver(db),
11651165
GenericDefId::TypeAliasId(inner) => inner.resolver(db),
11661166
GenericDefId::ImplId(inner) => inner.resolver(db),
1167-
GenericDefId::EnumVariantId(inner) => inner.resolver(db),
11681167
GenericDefId::ConstId(inner) => inner.resolver(db),
11691168
}
11701169
}

src/tools/rust-analyzer/crates/hir-def/src/visibility.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
33
use std::iter;
44

5+
use intern::Interned;
56
use la_arena::ArenaMap;
67
use span::SyntaxContextId;
78
use syntax::ast;
@@ -20,14 +21,17 @@ use crate::{
2021
pub enum RawVisibility {
2122
/// `pub(in module)`, `pub(crate)` or `pub(super)`. Also private, which is
2223
/// equivalent to `pub(self)`.
23-
Module(ModPath, VisibilityExplicitness),
24+
Module(Interned<ModPath>, VisibilityExplicitness),
2425
/// `pub`.
2526
Public,
2627
}
2728

2829
impl RawVisibility {
29-
pub(crate) const fn private() -> RawVisibility {
30-
RawVisibility::Module(ModPath::from_kind(PathKind::SELF), VisibilityExplicitness::Implicit)
30+
pub(crate) fn private() -> RawVisibility {
31+
RawVisibility::Module(
32+
Interned::new(ModPath::from_kind(PathKind::SELF)),
33+
VisibilityExplicitness::Implicit,
34+
)
3135
}
3236

3337
pub(crate) fn from_ast(
@@ -60,7 +64,7 @@ impl RawVisibility {
6064
ast::VisibilityKind::PubSelf => ModPath::from_kind(PathKind::SELF),
6165
ast::VisibilityKind::Pub => return RawVisibility::Public,
6266
};
63-
RawVisibility::Module(path, VisibilityExplicitness::Explicit)
67+
RawVisibility::Module(Interned::new(path), VisibilityExplicitness::Explicit)
6468
}
6569

6670
pub fn resolve(

src/tools/rust-analyzer/crates/hir-expand/src/change.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ impl ChangeWithProcMacros {
2525

2626
pub fn apply(self, db: &mut (impl ExpandDatabase + SourceDatabaseExt)) {
2727
self.source_change.apply(db);
28-
if let Some(proc_macros) = self.proc_macros {
28+
if let Some(mut proc_macros) = self.proc_macros {
29+
proc_macros.shrink_to_fit();
2930
db.set_proc_macros_with_durability(Arc::new(proc_macros), Durability::HIGH);
3031
}
3132
if let Some(target_data_layouts) = self.target_data_layouts {

0 commit comments

Comments
 (0)