Skip to content

Commit e39c1e9

Browse files
committed
Split mod items into small and big variants
1 parent d052450 commit e39c1e9

File tree

3 files changed

+81
-66
lines changed

3 files changed

+81
-66
lines changed

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

Lines changed: 59 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,8 @@ pub(crate) fn block_item_tree_query(db: &dyn DefDatabase, block: BlockId) -> Arc
133133

134134
let ctx = lower::Ctx::new(db, loc.ast_id.file_id);
135135
let mut item_tree = ctx.lower_block(&block);
136-
if item_tree.data.is_empty()
136+
if item_tree.small_data.is_empty()
137+
&& item_tree.big_data.is_empty()
137138
&& item_tree.top_level.is_empty()
138139
&& item_tree.attrs.is_empty()
139140
&& item_tree.top_attrs.is_empty()
@@ -143,7 +144,8 @@ pub(crate) fn block_item_tree_query(db: &dyn DefDatabase, block: BlockId) -> Arc
143144
Arc::new(ItemTree {
144145
top_level: Box::new([]),
145146
attrs: FxHashMap::default(),
146-
data: FxHashMap::default(),
147+
small_data: FxHashMap::default(),
148+
big_data: FxHashMap::default(),
147149
top_attrs: RawAttrs::EMPTY,
148150
vis: ItemVisibilities { arena: ThinVec::new() },
149151
})
@@ -162,7 +164,8 @@ pub struct ItemTree {
162164
attrs: FxHashMap<FileAstId<ast::Item>, RawAttrs>,
163165
vis: ItemVisibilities,
164166
// FIXME: They values store the key, turn this into a FxHashSet<ModItem> instead?
165-
data: FxHashMap<FileAstId<ast::Item>, ModItem>,
167+
big_data: FxHashMap<FileAstId<ast::Item>, BigModItem>,
168+
small_data: FxHashMap<FileAstId<ast::Item>, SmallModItem>,
166169
}
167170

168171
impl ItemTree {
@@ -204,13 +207,18 @@ impl ItemTree {
204207
let mut mods = 0;
205208
let mut macro_calls = 0;
206209
let mut macro_rules = 0;
207-
for item in self.data.values() {
210+
for item in self.small_data.values() {
208211
match item {
209-
ModItem::Trait(_) => traits += 1,
210-
ModItem::Impl(_) => impls += 1,
211-
ModItem::Mod(_) => mods += 1,
212-
ModItem::MacroCall(_) => macro_calls += 1,
213-
ModItem::MacroRules(_) => macro_rules += 1,
212+
SmallModItem::Trait(_) => traits += 1,
213+
SmallModItem::Impl(_) => impls += 1,
214+
SmallModItem::MacroRules(_) => macro_rules += 1,
215+
_ => {}
216+
}
217+
}
218+
for item in self.big_data.values() {
219+
match item {
220+
BigModItem::Mod(_) => mods += 1,
221+
BigModItem::MacroCall(_) => macro_calls += 1,
214222
_ => {}
215223
}
216224
}
@@ -222,9 +230,10 @@ impl ItemTree {
222230
}
223231

224232
fn shrink_to_fit(&mut self) {
225-
let ItemTree { top_level: _, attrs, data, vis: _, top_attrs: _ } = self;
233+
let ItemTree { top_level: _, attrs, big_data, small_data, vis: _, top_attrs: _ } = self;
226234
attrs.shrink_to_fit();
227-
data.shrink_to_fit();
235+
big_data.shrink_to_fit();
236+
small_data.shrink_to_fit();
228237
}
229238
}
230239

@@ -234,35 +243,37 @@ struct ItemVisibilities {
234243
}
235244

236245
#[derive(Debug, Clone, Eq, PartialEq)]
237-
enum ModItem {
246+
enum SmallModItem {
238247
Const(Const),
239248
Enum(Enum),
240-
// 32
241-
ExternBlock(ExternBlock),
242-
// 40
243-
ExternCrate(ExternCrate),
244249
Function(Function),
245250
Impl(Impl),
246251
Macro2(Macro2),
247-
// 32
248-
MacroCall(MacroCall),
249252
MacroRules(MacroRules),
250-
// 40
251-
Mod(Mod),
252253
Static(Static),
253-
// 32
254-
Struct(Struct),
255254
Trait(Trait),
256255
TraitAlias(TraitAlias),
257256
TypeAlias(TypeAlias),
258257
Union(Union),
259-
// 40
258+
}
259+
260+
#[derive(Debug, Clone, Eq, PartialEq)]
261+
enum BigModItem {
262+
ExternBlock(ExternBlock),
263+
ExternCrate(ExternCrate),
264+
MacroCall(MacroCall),
265+
Mod(Mod),
266+
Struct(Struct),
260267
Use(Use),
261268
}
262269

263-
// `ModItem` is stored a bunch in `ItemTree`'s so we pay the max for each item. It should stay as small as possible.
270+
// `ModItem` is stored a bunch in `ItemTree`'s so we pay the max for each item. It should stay as
271+
// small as possible which is why we split them in two, most common ones are 3 usize but some rarer
272+
// ones are 5.
273+
#[cfg(target_pointer_width = "64")]
274+
const _: [(); std::mem::size_of::<BigModItem>()] = [(); std::mem::size_of::<[usize; 5]>()];
264275
#[cfg(target_pointer_width = "64")]
265-
const _: [(); std::mem::size_of::<ModItem>()] = [(); std::mem::size_of::<[usize; 5]>()];
276+
const _: [(); std::mem::size_of::<SmallModItem>()] = [(); std::mem::size_of::<[usize; 3]>()];
266277

267278
#[derive(Default, Debug, Eq, PartialEq)]
268279
pub struct ItemTreeDataStats {
@@ -343,9 +354,12 @@ macro_rules! mod_items {
343354
impl Index<FileAstId<$ast>> for ItemTree {
344355
type Output = $typ;
345356

357+
#[allow(unused_imports)]
346358
fn index(&self, index: FileAstId<$ast>) -> &Self::Output {
347-
match &self.data[&index.upcast()] {
348-
ModItem::$typ(item) => item,
359+
use BigModItem::*;
360+
use SmallModItem::*;
361+
match &self.$fld[&index.upcast()] {
362+
$typ(item) => item,
349363
_ => panic!("expected item of type `{}` at index `{:?}`", stringify!($typ), index),
350364
}
351365
}
@@ -356,23 +370,23 @@ macro_rules! mod_items {
356370

357371
mod_items! {
358372
ModItemId ->
359-
Use in uses -> ast::Use,
360-
ExternCrate in extern_crates -> ast::ExternCrate,
361-
ExternBlock in extern_blocks -> ast::ExternBlock,
362-
Function in functions -> ast::Fn,
363-
Struct in structs -> ast::Struct,
364-
Union in unions -> ast::Union,
365-
Enum in enums -> ast::Enum,
366-
Const in consts -> ast::Const,
367-
Static in statics -> ast::Static,
368-
Trait in traits -> ast::Trait,
369-
TraitAlias in trait_aliases -> ast::TraitAlias,
370-
Impl in impls -> ast::Impl,
371-
TypeAlias in type_aliases -> ast::TypeAlias,
372-
Mod in mods -> ast::Module,
373-
MacroCall in macro_calls -> ast::MacroCall,
374-
MacroRules in macro_rules -> ast::MacroRules,
375-
Macro2 in macro_defs -> ast::MacroDef,
373+
Use in big_data -> ast::Use,
374+
ExternCrate in big_data -> ast::ExternCrate,
375+
ExternBlock in big_data -> ast::ExternBlock,
376+
Function in small_data -> ast::Fn,
377+
Struct in big_data -> ast::Struct,
378+
Union in small_data -> ast::Union,
379+
Enum in small_data -> ast::Enum,
380+
Const in small_data -> ast::Const,
381+
Static in small_data -> ast::Static,
382+
Trait in small_data -> ast::Trait,
383+
TraitAlias in small_data -> ast::TraitAlias,
384+
Impl in small_data -> ast::Impl,
385+
TypeAlias in small_data -> ast::TypeAlias,
386+
Mod in big_data -> ast::Module,
387+
MacroCall in big_data -> ast::MacroCall,
388+
MacroRules in small_data -> ast::MacroRules,
389+
Macro2 in small_data -> ast::MacroDef,
376390
}
377391

378392
impl Index<RawVisibilityId> for ItemTree {
@@ -421,6 +435,7 @@ pub struct UseTree {
421435
}
422436

423437
// FIXME: Would be nice to encode `None` into this
438+
// We could just use a `Name` where `_` well means `_` ..
424439
#[derive(Debug, Clone, PartialEq, Eq)]
425440
pub enum ImportAlias {
426441
/// Unnamed alias, as in `use Foo as _;`

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

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ use triomphe::Arc;
2020
use crate::{
2121
db::DefDatabase,
2222
item_tree::{
23-
Const, Enum, ExternBlock, ExternCrate, FieldsShape, Function, Impl, ImportAlias, Interned,
24-
ItemTree, ItemTreeAstId, Macro2, MacroCall, MacroRules, Mod, ModItem, ModItemId, ModKind,
25-
ModPath, RawAttrs, RawVisibility, RawVisibilityId, Static, Struct, StructKind, Trait,
26-
TraitAlias, TypeAlias, Union, Use, UseTree, UseTreeKind, VisibilityExplicitness,
23+
BigModItem, Const, Enum, ExternBlock, ExternCrate, FieldsShape, Function, Impl,
24+
ImportAlias, Interned, ItemTree, ItemTreeAstId, Macro2, MacroCall, MacroRules, Mod,
25+
ModItemId, ModKind, ModPath, RawAttrs, RawVisibility, RawVisibilityId, SmallModItem,
26+
Static, Struct, StructKind, Trait, TraitAlias, TypeAlias, Union, Use, UseTree, UseTreeKind,
27+
VisibilityExplicitness,
2728
},
2829
};
2930

@@ -168,7 +169,7 @@ impl<'a> Ctx<'a> {
168169
let ast_id = self.source_ast_id_map.ast_id(strukt);
169170
let shape = adt_shape(strukt.kind());
170171
let res = Struct { name, visibility, shape, ast_id };
171-
self.tree.data.insert(ast_id.upcast(), ModItem::Struct(res));
172+
self.tree.big_data.insert(ast_id.upcast(), BigModItem::Struct(res));
172173

173174
Some(ast_id)
174175
}
@@ -178,7 +179,7 @@ impl<'a> Ctx<'a> {
178179
let name = union.name()?.as_name();
179180
let ast_id = self.source_ast_id_map.ast_id(union);
180181
let res = Union { name, visibility, ast_id };
181-
self.tree.data.insert(ast_id.upcast(), ModItem::Union(res));
182+
self.tree.small_data.insert(ast_id.upcast(), SmallModItem::Union(res));
182183
Some(ast_id)
183184
}
184185

@@ -187,7 +188,7 @@ impl<'a> Ctx<'a> {
187188
let name = enum_.name()?.as_name();
188189
let ast_id = self.source_ast_id_map.ast_id(enum_);
189190
let res = Enum { name, visibility, ast_id };
190-
self.tree.data.insert(ast_id.upcast(), ModItem::Enum(res));
191+
self.tree.small_data.insert(ast_id.upcast(), SmallModItem::Enum(res));
191192
Some(ast_id)
192193
}
193194

@@ -199,7 +200,7 @@ impl<'a> Ctx<'a> {
199200

200201
let res = Function { name, visibility, ast_id };
201202

202-
self.tree.data.insert(ast_id.upcast(), ModItem::Function(res));
203+
self.tree.small_data.insert(ast_id.upcast(), SmallModItem::Function(res));
203204
Some(ast_id)
204205
}
205206

@@ -211,7 +212,7 @@ impl<'a> Ctx<'a> {
211212
let visibility = self.lower_visibility(type_alias);
212213
let ast_id = self.source_ast_id_map.ast_id(type_alias);
213214
let res = TypeAlias { name, visibility, ast_id };
214-
self.tree.data.insert(ast_id.upcast(), ModItem::TypeAlias(res));
215+
self.tree.small_data.insert(ast_id.upcast(), SmallModItem::TypeAlias(res));
215216
Some(ast_id)
216217
}
217218

@@ -220,7 +221,7 @@ impl<'a> Ctx<'a> {
220221
let visibility = self.lower_visibility(static_);
221222
let ast_id = self.source_ast_id_map.ast_id(static_);
222223
let res = Static { name, visibility, ast_id };
223-
self.tree.data.insert(ast_id.upcast(), ModItem::Static(res));
224+
self.tree.small_data.insert(ast_id.upcast(), SmallModItem::Static(res));
224225
Some(ast_id)
225226
}
226227

@@ -229,7 +230,7 @@ impl<'a> Ctx<'a> {
229230
let visibility = self.lower_visibility(konst);
230231
let ast_id = self.source_ast_id_map.ast_id(konst);
231232
let res = Const { name, visibility, ast_id };
232-
self.tree.data.insert(ast_id.upcast(), ModItem::Const(res));
233+
self.tree.small_data.insert(ast_id.upcast(), SmallModItem::Const(res));
233234
ast_id
234235
}
235236

@@ -251,7 +252,7 @@ impl<'a> Ctx<'a> {
251252
};
252253
let ast_id = self.source_ast_id_map.ast_id(module);
253254
let res = Mod { name, visibility, kind, ast_id };
254-
self.tree.data.insert(ast_id.upcast(), ModItem::Mod(res));
255+
self.tree.big_data.insert(ast_id.upcast(), BigModItem::Mod(res));
255256
Some(ast_id)
256257
}
257258

@@ -261,7 +262,7 @@ impl<'a> Ctx<'a> {
261262
let ast_id = self.source_ast_id_map.ast_id(trait_def);
262263

263264
let def = Trait { name, visibility, ast_id };
264-
self.tree.data.insert(ast_id.upcast(), ModItem::Trait(def));
265+
self.tree.small_data.insert(ast_id.upcast(), SmallModItem::Trait(def));
265266
Some(ast_id)
266267
}
267268

@@ -274,7 +275,7 @@ impl<'a> Ctx<'a> {
274275
let ast_id = self.source_ast_id_map.ast_id(trait_alias_def);
275276

276277
let alias = TraitAlias { name, visibility, ast_id };
277-
self.tree.data.insert(ast_id.upcast(), ModItem::TraitAlias(alias));
278+
self.tree.small_data.insert(ast_id.upcast(), SmallModItem::TraitAlias(alias));
278279
Some(ast_id)
279280
}
280281

@@ -283,7 +284,7 @@ impl<'a> Ctx<'a> {
283284
// Note that trait impls don't get implicit `Self` unlike traits, because here they are a
284285
// type alias rather than a type parameter, so this is handled by the resolver.
285286
let res = Impl { ast_id };
286-
self.tree.data.insert(ast_id.upcast(), ModItem::Impl(res));
287+
self.tree.small_data.insert(ast_id.upcast(), SmallModItem::Impl(res));
287288
ast_id
288289
}
289290

@@ -295,7 +296,7 @@ impl<'a> Ctx<'a> {
295296
})?;
296297

297298
let res = Use { visibility, ast_id, use_tree };
298-
self.tree.data.insert(ast_id.upcast(), ModItem::Use(res));
299+
self.tree.big_data.insert(ast_id.upcast(), BigModItem::Use(res));
299300
Some(ast_id)
300301
}
301302

@@ -311,7 +312,7 @@ impl<'a> Ctx<'a> {
311312
let ast_id = self.source_ast_id_map.ast_id(extern_crate);
312313

313314
let res = ExternCrate { name, alias, visibility, ast_id };
314-
self.tree.data.insert(ast_id.upcast(), ModItem::ExternCrate(res));
315+
self.tree.big_data.insert(ast_id.upcast(), BigModItem::ExternCrate(res));
315316
Some(ast_id)
316317
}
317318

@@ -325,7 +326,7 @@ impl<'a> Ctx<'a> {
325326
let ast_id = self.source_ast_id_map.ast_id(m);
326327
let expand_to = hir_expand::ExpandTo::from_call_site(m);
327328
let res = MacroCall { path, ast_id, expand_to, ctxt: span_map.span_for_range(range).ctx };
328-
self.tree.data.insert(ast_id.upcast(), ModItem::MacroCall(res));
329+
self.tree.big_data.insert(ast_id.upcast(), BigModItem::MacroCall(res));
329330
Some(ast_id)
330331
}
331332

@@ -334,7 +335,7 @@ impl<'a> Ctx<'a> {
334335
let ast_id = self.source_ast_id_map.ast_id(m);
335336

336337
let res = MacroRules { name: name.as_name(), ast_id };
337-
self.tree.data.insert(ast_id.upcast(), ModItem::MacroRules(res));
338+
self.tree.small_data.insert(ast_id.upcast(), SmallModItem::MacroRules(res));
338339
Some(ast_id)
339340
}
340341

@@ -345,7 +346,7 @@ impl<'a> Ctx<'a> {
345346
let visibility = self.lower_visibility(m);
346347

347348
let res = Macro2 { name: name.as_name(), ast_id, visibility };
348-
self.tree.data.insert(ast_id.upcast(), ModItem::Macro2(res));
349+
self.tree.small_data.insert(ast_id.upcast(), SmallModItem::Macro2(res));
349350
Some(ast_id)
350351
}
351352

@@ -372,7 +373,7 @@ impl<'a> Ctx<'a> {
372373
});
373374

374375
let res = ExternBlock { ast_id, children };
375-
self.tree.data.insert(ast_id.upcast(), ModItem::ExternBlock(res));
376+
self.tree.big_data.insert(ast_id.upcast(), BigModItem::ExternBlock(res));
376377
ast_id
377378
}
378379

src/tools/rust-analyzer/crates/intern/src/symbol.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,6 @@ impl TaggedArcPtr {
112112
}
113113
}
114114

115-
// FIXME: This should have more than one niche
116115
#[derive(PartialEq, Eq, Hash)]
117116
pub struct Symbol {
118117
repr: TaggedArcPtr,

0 commit comments

Comments
 (0)