Skip to content

Commit 89920fb

Browse files
Provide better incrementality for modules
Previously module ID reuse was based on order of creation solely. Now we store the parent module and name, and so Salsa will reuse the ID for modules that have the same name and parent. This is important as many interned IDs contain modules, so if the module is invalidated they too are.
1 parent 7c3fc86 commit 89920fb

4 files changed

Lines changed: 33 additions & 10 deletions

File tree

crates/hir-def/src/expr_store/tests/body/block.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,11 @@ fn f() {
199199
4401,
200200
),
201201
),
202+
containing_module_inside_def_map: None,
203+
name_or_empty: Name {
204+
symbol: "",
205+
ctx: (),
206+
},
202207
}"#]],
203208
);
204209
}

crates/hir-def/src/lib.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub mod find_path;
4848
pub mod import_map;
4949
pub mod visibility;
5050

51-
use intern::Interned;
51+
use intern::{Interned, sym};
5252
use rustc_abi::ExternAbi;
5353
use thin_vec::ThinVec;
5454

@@ -457,6 +457,11 @@ pub struct ModuleIdLt<'db> {
457457
/// `BlockId` of that block expression. If `None`, this module is part of the crate-level
458458
/// `DefMap` of `krate`.
459459
pub block: Option<BlockId>,
460+
/// The parent module of this module, or `None` if this is the root module inside the def
461+
/// map (including for block def maps).
462+
pub containing_module_inside_def_map: Option<ModuleIdLt<'db>>,
463+
/// The name of this module, or [`sym::__empty`] for the root module.
464+
name_or_empty: Name,
460465
}
461466
pub type ModuleId = ModuleIdLt<'static>;
462467

@@ -502,21 +507,23 @@ impl ModuleId {
502507
}
503508

504509
pub fn name(self, db: &dyn DefDatabase) -> Option<Name> {
505-
let def_map = self.def_map(db);
506-
let parent = def_map[self].parent?;
507-
def_map[parent].children.iter().find_map(|(name, module_id)| {
508-
if *module_id == self { Some(name.clone()) } else { None }
509-
})
510+
let name = self.name_or_empty(db);
511+
if *name.symbol() == sym::__empty { None } else { Some(name) }
510512
}
511513

512514
/// Returns the module containing `self`, either the parent `mod`, or the module (or block) containing
513515
/// the block, if `self` corresponds to a block expression.
514516
pub fn containing_module(self, db: &dyn DefDatabase) -> Option<ModuleId> {
515-
self.def_map(db).containing_module(self)
517+
self.containing_module_inside_def_map(db)
518+
.or_else(|| self.block(db).map(|block| block.loc(db).module))
519+
.map(|module| {
520+
// SAFETY: Not sure.
521+
unsafe { module.to_static() }
522+
})
516523
}
517524

518525
pub fn is_block_module(self, db: &dyn DefDatabase) -> bool {
519-
self.block(db).is_some() && self.def_map(db).root_module_id() == self
526+
self.block(db).is_some() && self.containing_module_inside_def_map(db).is_none()
520527
}
521528
}
522529

crates/hir-def/src/nameres.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ use hir_expand::{
6666
EditionedFileId, ErasedAstId, HirFileId, InFile, MacroCallId, mod_path::ModPath, name::Name,
6767
proc_macro::ProcMacroKind,
6868
};
69-
use intern::Symbol;
69+
use intern::{Symbol, sym};
7070
use itertools::Itertools;
7171
use rustc_hash::FxHashMap;
7272
use span::{Edition, FileAstId, FileId, ROOT_ERASED_FILE_AST_ID};
@@ -465,7 +465,16 @@ impl DefMap {
465465
block: Option<BlockInfo>,
466466
) -> DefMap {
467467
let mut modules = ModulesMap::new();
468-
let root = unsafe { ModuleIdLt::new(db, krate, block.map(|it| it.block)).to_static() };
468+
let root = unsafe {
469+
ModuleIdLt::new(
470+
db,
471+
krate,
472+
block.map(|it| it.block),
473+
None,
474+
Name::new_symbol_root(sym::__empty),
475+
)
476+
.to_static()
477+
};
469478
modules.insert(root, module_data);
470479

471480
DefMap {

crates/hir-def/src/nameres/collector.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2428,6 +2428,8 @@ impl ModCollector<'_, '_> {
24282428
self.def_collector.db,
24292429
self.def_collector.def_map.krate,
24302430
self.def_collector.def_map.block_id(),
2431+
Some(self.module_id),
2432+
name.clone(),
24312433
)
24322434
.to_static()
24332435
};

0 commit comments

Comments
 (0)