Skip to content

Commit 124e11d

Browse files
authored
Merge pull request #22322 from ChayimFriedman2/module-inc
perf: Provide better incrementality for modules
2 parents f4bd646 + 89920fb commit 124e11d

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

@@ -472,6 +472,11 @@ pub struct ModuleIdLt<'db> {
472472
/// `BlockId` of that block expression. If `None`, this module is part of the crate-level
473473
/// `DefMap` of `krate`.
474474
pub block: Option<BlockId>,
475+
/// The parent module of this module, or `None` if this is the root module inside the def
476+
/// map (including for block def maps).
477+
pub containing_module_inside_def_map: Option<ModuleIdLt<'db>>,
478+
/// The name of this module, or [`sym::__empty`] for the root module.
479+
name_or_empty: Name,
475480
}
476481
pub type ModuleId = ModuleIdLt<'static>;
477482

@@ -517,21 +522,23 @@ impl ModuleId {
517522
}
518523

519524
pub fn name(self, db: &dyn DefDatabase) -> Option<Name> {
520-
let def_map = self.def_map(db);
521-
let parent = def_map[self].parent?;
522-
def_map[parent].children.iter().find_map(|(name, module_id)| {
523-
if *module_id == self { Some(name.clone()) } else { None }
524-
})
525+
let name = self.name_or_empty(db);
526+
if *name.symbol() == sym::__empty { None } else { Some(name) }
525527
}
526528

527529
/// Returns the module containing `self`, either the parent `mod`, or the module (or block) containing
528530
/// the block, if `self` corresponds to a block expression.
529531
pub fn containing_module(self, db: &dyn DefDatabase) -> Option<ModuleId> {
530-
self.def_map(db).containing_module(self)
532+
self.containing_module_inside_def_map(db)
533+
.or_else(|| self.block(db).map(|block| block.loc(db).module))
534+
.map(|module| {
535+
// SAFETY: Not sure.
536+
unsafe { module.to_static() }
537+
})
531538
}
532539

533540
pub fn is_block_module(self, db: &dyn DefDatabase) -> bool {
534-
self.block(db).is_some() && self.def_map(db).root_module_id() == self
541+
self.block(db).is_some() && self.containing_module_inside_def_map(db).is_none()
535542
}
536543
}
537544

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)