Skip to content

Commit e507807

Browse files
committed
internal: Don't eagerly construct AstIdMaps
1 parent 8454413 commit e507807

File tree

4 files changed

+33
-25
lines changed

4 files changed

+33
-25
lines changed

crates/hir-def/src/body.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ pub struct Expander {
5050
cfg_expander: CfgExpander,
5151
def_map: Arc<DefMap>,
5252
current_file_id: HirFileId,
53-
ast_id_map: Arc<AstIdMap>,
53+
ast_id_map: Option<Arc<AstIdMap>>,
5454
module: LocalModuleId,
5555
recursion_limit: usize,
5656
}
@@ -80,12 +80,11 @@ impl Expander {
8080
pub fn new(db: &dyn DefDatabase, current_file_id: HirFileId, module: ModuleId) -> Expander {
8181
let cfg_expander = CfgExpander::new(db, current_file_id, module.krate);
8282
let def_map = module.def_map(db);
83-
let ast_id_map = db.ast_id_map(current_file_id);
8483
Expander {
8584
cfg_expander,
8685
def_map,
8786
current_file_id,
88-
ast_id_map,
87+
ast_id_map: None,
8988
module: module.local_id,
9089
recursion_limit: 0,
9190
}
@@ -175,7 +174,7 @@ impl Expander {
175174
};
176175
self.cfg_expander.hygiene = Hygiene::new(db.upcast(), file_id);
177176
self.current_file_id = file_id;
178-
self.ast_id_map = db.ast_id_map(file_id);
177+
self.ast_id_map = None;
179178

180179
ExpandResult { value: Some((mark, node)), err }
181180
}
@@ -213,8 +212,9 @@ impl Expander {
213212
self.def_map.resolve_path(db, self.module, path, BuiltinShadowMode::Other).0.take_macros()
214213
}
215214

216-
fn ast_id<N: AstNode>(&self, item: &N) -> AstId<N> {
217-
let file_local_id = self.ast_id_map.ast_id(item);
215+
fn ast_id<N: AstNode>(&mut self, db: &dyn DefDatabase, item: &N) -> AstId<N> {
216+
let file_local_id =
217+
self.ast_id_map.get_or_insert_with(|| db.ast_id_map(self.current_file_id)).ast_id(item);
218218
AstId::new(self.current_file_id, file_local_id)
219219
}
220220

@@ -233,7 +233,7 @@ impl Expander {
233233
#[derive(Debug)]
234234
pub struct Mark {
235235
file_id: HirFileId,
236-
ast_id_map: Arc<AstIdMap>,
236+
ast_id_map: Option<Arc<AstIdMap>>,
237237
bomb: DropBomb,
238238
}
239239

crates/hir-def/src/body/lower.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ use hir_expand::{
1111
ExpandError, HirFileId, InFile,
1212
};
1313
use la_arena::Arena;
14+
use once_cell::unsync::OnceCell;
1415
use profile::Count;
1516
use rustc_hash::FxHashMap;
1617
use syntax::{
@@ -41,38 +42,42 @@ use crate::{
4142
pub struct LowerCtx<'a> {
4243
pub db: &'a dyn DefDatabase,
4344
hygiene: Hygiene,
44-
file_id: Option<HirFileId>,
45-
source_ast_id_map: Option<Arc<AstIdMap>>,
45+
ast_id_map: Option<(HirFileId, OnceCell<Arc<AstIdMap>>)>,
4646
}
4747

4848
impl<'a> LowerCtx<'a> {
4949
pub fn new(db: &'a dyn DefDatabase, file_id: HirFileId) -> Self {
5050
LowerCtx {
5151
db,
5252
hygiene: Hygiene::new(db.upcast(), file_id),
53-
file_id: Some(file_id),
54-
source_ast_id_map: Some(db.ast_id_map(file_id)),
53+
ast_id_map: Some((file_id, OnceCell::new())),
5554
}
5655
}
5756

5857
pub fn with_hygiene(db: &'a dyn DefDatabase, hygiene: &Hygiene) -> Self {
59-
LowerCtx { db, hygiene: hygiene.clone(), file_id: None, source_ast_id_map: None }
58+
LowerCtx { db, hygiene: hygiene.clone(), ast_id_map: None }
6059
}
6160

6261
pub(crate) fn hygiene(&self) -> &Hygiene {
6362
&self.hygiene
6463
}
6564

6665
pub(crate) fn file_id(&self) -> HirFileId {
67-
self.file_id.unwrap()
66+
self.ast_id_map.as_ref().unwrap().0
6867
}
6968

7069
pub(crate) fn lower_path(&self, ast: ast::Path) -> Option<Path> {
7170
Path::from_src(ast, self)
7271
}
7372

74-
pub(crate) fn ast_id<N: AstNode>(&self, item: &N) -> Option<FileAstId<N>> {
75-
self.source_ast_id_map.as_ref().map(|ast_id_map| ast_id_map.ast_id(item))
73+
pub(crate) fn ast_id<N: AstNode>(
74+
&self,
75+
db: &dyn DefDatabase,
76+
item: &N,
77+
) -> Option<FileAstId<N>> {
78+
let (file_id, ast_id_map) = self.ast_id_map.as_ref()?;
79+
let ast_id_map = ast_id_map.get_or_init(|| db.ast_id_map(*file_id));
80+
Some(ast_id_map.ast_id(item))
7681
}
7782
}
7883

@@ -675,7 +680,7 @@ impl ExprCollector<'_> {
675680
}
676681

677682
fn collect_block(&mut self, block: ast::BlockExpr) -> ExprId {
678-
let ast_id = self.expander.ast_id(&block);
683+
let ast_id = self.expander.ast_id(self.db, &block);
679684
let block_loc =
680685
BlockLoc { ast_id, module: self.expander.def_map.module_id(self.expander.module) };
681686
let block_id = self.db.intern_block(block_loc);

crates/hir-def/src/data.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -550,14 +550,17 @@ impl<'a> AssocItemCollector<'a> {
550550
AssocItem::MacroCall(call) => {
551551
let call = &item_tree[call];
552552
let ast_id_map = self.db.ast_id_map(self.expander.current_file_id());
553-
let root = self.db.parse_or_expand(self.expander.current_file_id()).unwrap();
554-
let call = ast_id_map.get(call.ast_id).to_node(&root);
555-
let _cx =
556-
stdx::panic_context::enter(format!("collect_items MacroCall: {}", call));
557-
let res = self.expander.enter_expand(self.db, call);
558-
559-
if let Ok(ExpandResult { value: Some((mark, mac)), .. }) = res {
560-
self.collect_macro_items(mark, mac);
553+
if let Some(root) = self.db.parse_or_expand(self.expander.current_file_id()) {
554+
let call = ast_id_map.get(call.ast_id).to_node(&root);
555+
let _cx = stdx::panic_context::enter(format!(
556+
"collect_items MacroCall: {}",
557+
call
558+
));
559+
let res = self.expander.enter_expand(self.db, call);
560+
561+
if let Ok(ExpandResult { value: Some((mark, mac)), .. }) = res {
562+
self.collect_macro_items(mark, mac);
563+
}
561564
}
562565
}
563566
}

crates/hir-def/src/type_ref.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ impl TypeRef {
237237
}
238238
ast::Type::MacroType(mt) => match mt.macro_call() {
239239
Some(mc) => ctx
240-
.ast_id(&mc)
240+
.ast_id(ctx.db, &mc)
241241
.map(|mc| TypeRef::Macro(InFile::new(ctx.file_id(), mc)))
242242
.unwrap_or(TypeRef::Error),
243243
None => TypeRef::Error,

0 commit comments

Comments
 (0)