Skip to content

Commit 1efe8b6

Browse files
authored
Merge pull request #20994 from Veykril/push-npvyklkuxnlr
perf: Reduce memory usage of symbol index
2 parents 4cea939 + abf2e3e commit 1efe8b6

File tree

7 files changed

+88
-104
lines changed

7 files changed

+88
-104
lines changed

crates/hir/src/symbols.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use hir_ty::{
1818
};
1919
use intern::Symbol;
2020
use rustc_hash::FxHashMap;
21-
use syntax::{AstNode, AstPtr, SmolStr, SyntaxNode, SyntaxNodePtr, ToSmolStr, ast::HasName};
21+
use syntax::{AstNode, AstPtr, SyntaxNode, SyntaxNodePtr, ToSmolStr, ast::HasName};
2222

2323
use crate::{HasCrate, Module, ModuleDef, Semantics};
2424

@@ -29,7 +29,7 @@ pub struct FileSymbol {
2929
pub name: Symbol,
3030
pub def: ModuleDef,
3131
pub loc: DeclarationLocation,
32-
pub container_name: Option<SmolStr>,
32+
pub container_name: Option<Symbol>,
3333
/// Whether this symbol is a doc alias for the original symbol.
3434
pub is_alias: bool,
3535
pub is_assoc: bool,
@@ -65,7 +65,7 @@ pub struct SymbolCollector<'a> {
6565
db: &'a dyn HirDatabase,
6666
symbols: FxIndexSet<FileSymbol>,
6767
work: Vec<SymbolCollectorWork>,
68-
current_container_name: Option<SmolStr>,
68+
current_container_name: Option<Symbol>,
6969
}
7070

7171
/// Given a [`ModuleId`] and a [`HirDatabase`], use the DefMap for the module's crate to collect
@@ -108,7 +108,7 @@ impl<'a> SymbolCollector<'a> {
108108
tracing::info!(?work, "SymbolCollector::do_work");
109109
self.db.unwind_if_revision_cancelled();
110110

111-
let parent_name = work.parent.map(|name| name.as_str().to_smolstr());
111+
let parent_name = work.parent.map(|name| Symbol::intern(name.as_str()));
112112
self.with_container_name(parent_name, |s| s.collect_from_module(work.module_id));
113113
}
114114

@@ -125,7 +125,7 @@ impl<'a> SymbolCollector<'a> {
125125
}
126126
ModuleDefId::AdtId(AdtId::EnumId(id)) => {
127127
this.push_decl(id, name, false, None);
128-
let enum_name = this.db.enum_signature(id).name.as_str().to_smolstr();
128+
let enum_name = Symbol::intern(this.db.enum_signature(id).name.as_str());
129129
this.with_container_name(Some(enum_name), |this| {
130130
let variants = id.enum_variants(this.db);
131131
for (variant_id, variant_name, _) in &variants.variants {
@@ -328,7 +328,7 @@ impl<'a> SymbolCollector<'a> {
328328
)
329329
.to_smolstr(),
330330
);
331-
self.with_container_name(impl_name, |s| {
331+
self.with_container_name(impl_name.as_deref().map(Symbol::intern), |s| {
332332
for &(ref name, assoc_item_id) in &impl_id.impl_items(self.db).items {
333333
s.push_assoc_item(assoc_item_id, name, None)
334334
}
@@ -337,14 +337,14 @@ impl<'a> SymbolCollector<'a> {
337337

338338
fn collect_from_trait(&mut self, trait_id: TraitId, trait_do_not_complete: Complete) {
339339
let trait_data = self.db.trait_signature(trait_id);
340-
self.with_container_name(Some(trait_data.name.as_str().into()), |s| {
340+
self.with_container_name(Some(Symbol::intern(trait_data.name.as_str())), |s| {
341341
for &(ref name, assoc_item_id) in &trait_id.trait_items(self.db).items {
342342
s.push_assoc_item(assoc_item_id, name, Some(trait_do_not_complete));
343343
}
344344
});
345345
}
346346

347-
fn with_container_name(&mut self, container_name: Option<SmolStr>, f: impl FnOnce(&mut Self)) {
347+
fn with_container_name(&mut self, container_name: Option<Symbol>, f: impl FnOnce(&mut Self)) {
348348
if let Some(container_name) = container_name {
349349
let prev = self.current_container_name.replace(container_name);
350350
f(self);

crates/ide-db/src/ra_fixture.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use std::hash::{BuildHasher, Hash};
44

5-
use hir::{CfgExpr, FilePositionWrapper, FileRangeWrapper, Semantics};
5+
use hir::{CfgExpr, FilePositionWrapper, FileRangeWrapper, Semantics, Symbol};
66
use smallvec::SmallVec;
77
use span::{TextRange, TextSize};
88
use syntax::{
@@ -524,6 +524,7 @@ impl_empty_upmap_from_ra_fixture!(
524524
f64,
525525
&str,
526526
String,
527+
Symbol,
527528
SmolStr,
528529
Documentation,
529530
SymbolKind,

crates/ide/src/goto_definition.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ fn try_lookup_include_path(
245245
Some(NavigationTarget {
246246
file_id,
247247
full_range: TextRange::new(0.into(), size),
248-
name: path.into(),
248+
name: hir::Symbol::intern(&path),
249249
alias: None,
250250
focus_range: None,
251251
kind: None,
@@ -598,7 +598,13 @@ fn expr_to_nav(
598598
let value_range = value.syntax().text_range();
599599
let navs = navigation_target::orig_range_with_focus_r(db, file_id, value_range, focus_range);
600600
navs.map(|(hir::FileRangeWrapper { file_id, range }, focus_range)| {
601-
NavigationTarget::from_syntax(file_id, "<expr>".into(), focus_range, range, kind)
601+
NavigationTarget::from_syntax(
602+
file_id,
603+
hir::Symbol::intern("<expr>"),
604+
focus_range,
605+
range,
606+
kind,
607+
)
602608
})
603609
}
604610

@@ -607,7 +613,6 @@ mod tests {
607613
use crate::{GotoDefinitionConfig, fixture};
608614
use ide_db::{FileRange, MiniCore};
609615
use itertools::Itertools;
610-
use syntax::SmolStr;
611616

612617
const TEST_CONFIG: GotoDefinitionConfig<'_> =
613618
GotoDefinitionConfig { minicore: MiniCore::default() };
@@ -658,7 +663,7 @@ mod tests {
658663
let Some(target) = navs.into_iter().next() else {
659664
panic!("expected single navigation target but encountered none");
660665
};
661-
assert_eq!(target.name, SmolStr::new_inline(expected_name));
666+
assert_eq!(target.name, hir::Symbol::intern(expected_name));
662667
}
663668

664669
#[test]

0 commit comments

Comments
 (0)