From 4f3fc6fa1a0b76d120626dcf1e2e0a31cc10a54c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 27 Nov 2021 21:13:07 +0300 Subject: [PATCH] try to optimize things unsuccessfully Baseline ``` Database loaded: 598.40ms, 304minstr, 118mb (metadata 390.57ms, 21minstr, 841kb; build 111.31ms, 8764kinstr, -214kb) crates: 39, mods: 824, decls: 18647, fns: 13910 Item Collection: 9.70s, 75ginstr, 377mb exprs: 382426, ??ty: 387 (0%), ?ty: 285 (0%), !ty: 145 Inference: 43.16s, 342ginstr, 641mb Total: 52.86s, 417ginstr, 1018mb ``` Eager ``` Database loaded: 625.86ms, 304minstr, 118mb (metadata 414.52ms, 21minstr, 841kb; build 113.81ms, 8764kinstr, -230kb) crates: 39, mods: 824, decls: 18647, fns: 13910 Item Collection: 10.09s, 75ginstr, 389mb exprs: 382426, ??ty: 387 (0%), ?ty: 285 (0%), !ty: 145 Inference: 43.27s, 341ginstr, 644mb Total: 53.37s, 417ginstr, 1034mb ``` Lazy ``` Database loaded: 626.34ms, 304minstr, 118mb (metadata 416.26ms, 21minstr, 841kb; build 113.67ms, 8750kinstr, -209kb) crates: 39, mods: 824, decls: 18647, fns: 13910 Item Collection: 10.16s, 75ginstr, 389mb exprs: 382426, ??ty: 387 (0%), ?ty: 285 (0%), !ty: 145 Inference: 44.51s, 342ginstr, 644mb Total: 54.67s, 417ginstr, 1034mb ``` --- Cargo.lock | 1 + crates/hir_expand/Cargo.toml | 1 + crates/hir_expand/src/ast_id_map.rs | 26 ++++++++++++++++++++++---- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8a0d809a1b..4d22ebdc18 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -529,6 +529,7 @@ dependencies = [ "la-arena", "limit", "mbe", + "once_cell", "profile", "rustc-hash", "syntax", diff --git a/crates/hir_expand/Cargo.toml b/crates/hir_expand/Cargo.toml index 380b7ef877..7a61036530 100644 --- a/crates/hir_expand/Cargo.toml +++ b/crates/hir_expand/Cargo.toml @@ -16,6 +16,7 @@ either = "1.5.3" rustc-hash = "1.0.0" la-arena = { version = "0.3.0", path = "../../lib/arena" } itertools = "0.10.0" +once_cell = "1" base_db = { path = "../base_db", version = "0.0.0" } cfg = { path = "../cfg", version = "0.0.0" } diff --git a/crates/hir_expand/src/ast_id_map.rs b/crates/hir_expand/src/ast_id_map.rs index 64387b8148..9db2bc6410 100644 --- a/crates/hir_expand/src/ast_id_map.rs +++ b/crates/hir_expand/src/ast_id_map.rs @@ -61,13 +61,29 @@ impl FileAstId { type ErasedFileAstId = Idx; /// Maps items' `SyntaxNode`s to `ErasedFileAstId`s and back. -#[derive(Debug, PartialEq, Eq, Default)] +#[derive(Default)] pub struct AstIdMap { arena: Arena, - map: FxHashMap, + /// Reversed mapping lazily derived from [`self.arena`]. + /// + /// FIXE: Do not store `SyntaxNodePtr` twice. + map: once_cell::sync::OnceCell>, _c: Count, } +impl fmt::Debug for AstIdMap { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("AstIdMap").field("arena", &self.arena).finish() + } +} + +impl PartialEq for AstIdMap { + fn eq(&self, other: &Self) -> bool { + self.arena == other.arena + } +} +impl Eq for AstIdMap {} + impl AstIdMap { pub(crate) fn from_source(node: &SyntaxNode) -> AstIdMap { assert!(node.parent().is_none()); @@ -91,9 +107,11 @@ impl AstIdMap { } } }); - res.map.extend(res.arena.iter().map(|(idx, ptr)| (ptr.clone(), idx))); res } + fn map(&self) -> &FxHashMap { + self.map.get_or_init(|| self.arena.iter().map(|(idx, ptr)| (ptr.clone(), idx)).collect()) + } pub fn ast_id(&self, item: &N) -> FileAstId { let raw = self.erased_ast_id(item.syntax()); @@ -102,7 +120,7 @@ impl AstIdMap { fn erased_ast_id(&self, item: &SyntaxNode) -> ErasedFileAstId { let ptr = SyntaxNodePtr::new(item); - *self.map.get(&ptr).unwrap_or_else(|| { + *self.map().get(&ptr).unwrap_or_else(|| { panic!( "Can't find {:?} in AstIdMap:\n{:?}", item,