Skip to content

Commit 2eea0f8

Browse files
committed
Use ThinVec in ItemScope in a couple places
1 parent dd7f93e commit 2eea0f8

File tree

1 file changed

+17
-19
lines changed

1 file changed

+17
-19
lines changed

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

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use smallvec::{SmallVec, smallvec};
1313
use span::Edition;
1414
use stdx::format_to;
1515
use syntax::ast;
16+
use thin_vec::ThinVec;
1617

1718
use crate::{
1819
AdtId, BuiltinType, ConstId, ExternBlockId, ExternCrateId, FxIndexMap, HasModule, ImplId,
@@ -155,22 +156,21 @@ pub struct ItemScope {
155156

156157
/// The defs declared in this scope. Each def has a single scope where it is
157158
/// declared.
158-
declarations: Vec<ModuleDefId>,
159+
declarations: ThinVec<ModuleDefId>,
159160

160-
impls: Vec<ImplId>,
161-
#[allow(clippy::box_collection)]
162-
extern_blocks: Option<Box<Vec<ExternBlockId>>>,
163-
unnamed_consts: Vec<ConstId>,
161+
impls: ThinVec<ImplId>,
162+
extern_blocks: ThinVec<ExternBlockId>,
163+
unnamed_consts: ThinVec<ConstId>,
164164
/// Traits imported via `use Trait as _;`.
165-
unnamed_trait_imports: FxHashMap<TraitId, Item<()>>,
165+
unnamed_trait_imports: ThinVec<(TraitId, Item<()>)>,
166166

167167
// the resolutions of the imports of this scope
168168
use_imports_types: FxHashMap<ImportOrExternCrate, ImportOrDef>,
169169
use_imports_values: FxHashMap<ImportOrGlob, ImportOrDef>,
170170
use_imports_macros: FxHashMap<ImportOrExternCrate, ImportOrDef>,
171171

172-
use_decls: Vec<UseId>,
173-
extern_crate_decls: Vec<ExternCrateId>,
172+
use_decls: ThinVec<UseId>,
173+
extern_crate_decls: ThinVec<ExternCrateId>,
174174
/// Macros visible in current module in legacy textual scope
175175
///
176176
/// For macros invoked by an unqualified identifier like `bar!()`, `legacy_macros` will be searched in first.
@@ -183,7 +183,7 @@ pub struct ItemScope {
183183
/// Module scoped macros will be inserted into `items` instead of here.
184184
// FIXME: Macro shadowing in one module is not properly handled. Non-item place macros will
185185
// be all resolved to the last one defined if shadowing happens.
186-
legacy_macros: FxHashMap<Name, SmallVec<[MacroId; 1]>>,
186+
legacy_macros: FxHashMap<Name, SmallVec<[MacroId; 2]>>,
187187
/// The attribute macro invocations in this scope.
188188
attr_macros: FxHashMap<AstId<ast::Item>, MacroCallId>,
189189
/// The macro invocations in this scope.
@@ -198,7 +198,7 @@ struct DeriveMacroInvocation {
198198
attr_id: AttrId,
199199
/// The `#[derive]` call
200200
attr_call_id: MacroCallId,
201-
derive_call_ids: SmallVec<[Option<MacroCallId>; 1]>,
201+
derive_call_ids: SmallVec<[Option<MacroCallId>; 4]>,
202202
}
203203

204204
pub(crate) static BUILTIN_SCOPE: LazyLock<FxIndexMap<Name, PerNs>> = LazyLock::new(|| {
@@ -322,7 +322,7 @@ impl ItemScope {
322322
}
323323

324324
pub fn extern_blocks(&self) -> impl Iterator<Item = ExternBlockId> + '_ {
325-
self.extern_blocks.iter().flat_map(|it| it.iter()).copied()
325+
self.extern_blocks.iter().copied()
326326
}
327327

328328
pub fn use_decls(&self) -> impl ExactSizeIterator<Item = UseId> + '_ {
@@ -435,7 +435,7 @@ impl ItemScope {
435435
ModuleDefId::TraitId(t) => Some(t),
436436
_ => None,
437437
})
438-
.chain(self.unnamed_trait_imports.keys().copied())
438+
.chain(self.unnamed_trait_imports.iter().map(|&(t, _)| t))
439439
}
440440

441441
pub(crate) fn resolutions(&self) -> impl Iterator<Item = (Option<Name>, PerNs)> + '_ {
@@ -476,7 +476,7 @@ impl ItemScope {
476476
}
477477

478478
pub(crate) fn define_extern_block(&mut self, extern_block: ExternBlockId) {
479-
self.extern_blocks.get_or_insert_default().push(extern_block);
479+
self.extern_blocks.push(extern_block);
480480
}
481481

482482
pub(crate) fn define_extern_crate_decl(&mut self, extern_crate: ExternCrateId) {
@@ -564,7 +564,7 @@ impl ItemScope {
564564

565565
// FIXME: This is only used in collection, we should move the relevant parts of it out of ItemScope
566566
pub(crate) fn unnamed_trait_vis(&self, tr: TraitId) -> Option<Visibility> {
567-
self.unnamed_trait_imports.get(&tr).map(|trait_| trait_.vis)
567+
self.unnamed_trait_imports.iter().find(|&&(t, _)| t == tr).map(|(_, trait_)| trait_.vis)
568568
}
569569

570570
pub(crate) fn push_unnamed_trait(
@@ -573,7 +573,7 @@ impl ItemScope {
573573
vis: Visibility,
574574
import: Option<ImportId>,
575575
) {
576-
self.unnamed_trait_imports.insert(tr, Item { def: (), vis, import });
576+
self.unnamed_trait_imports.push((tr, Item { def: (), vis, import }));
577577
}
578578

579579
pub(crate) fn push_res_with_import(
@@ -725,7 +725,7 @@ impl ItemScope {
725725
.values_mut()
726726
.map(|def| &mut def.vis)
727727
.chain(self.values.values_mut().map(|def| &mut def.vis))
728-
.chain(self.unnamed_trait_imports.values_mut().map(|def| &mut def.vis))
728+
.chain(self.unnamed_trait_imports.iter_mut().map(|(_, def)| &mut def.vis))
729729
.for_each(|vis| match vis {
730730
&mut Visibility::Module(_, visibility_explicitness) => {
731731
*vis = Visibility::Module(this_module, visibility_explicitness)
@@ -817,9 +817,7 @@ impl ItemScope {
817817
macro_invocations,
818818
extern_blocks,
819819
} = self;
820-
if let Some(it) = extern_blocks {
821-
it.shrink_to_fit();
822-
}
820+
extern_blocks.shrink_to_fit();
823821
types.shrink_to_fit();
824822
values.shrink_to_fit();
825823
macros.shrink_to_fit();

0 commit comments

Comments
 (0)