Skip to content

Commit 4f3fc6f

Browse files
committed
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 ```
1 parent c603b90 commit 4f3fc6f

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/hir_expand/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ either = "1.5.3"
1616
rustc-hash = "1.0.0"
1717
la-arena = { version = "0.3.0", path = "../../lib/arena" }
1818
itertools = "0.10.0"
19+
once_cell = "1"
1920

2021
base_db = { path = "../base_db", version = "0.0.0" }
2122
cfg = { path = "../cfg", version = "0.0.0" }

crates/hir_expand/src/ast_id_map.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,13 +61,29 @@ impl<N: AstNode> FileAstId<N> {
6161
type ErasedFileAstId = Idx<SyntaxNodePtr>;
6262

6363
/// Maps items' `SyntaxNode`s to `ErasedFileAstId`s and back.
64-
#[derive(Debug, PartialEq, Eq, Default)]
64+
#[derive(Default)]
6565
pub struct AstIdMap {
6666
arena: Arena<SyntaxNodePtr>,
67-
map: FxHashMap<SyntaxNodePtr, ErasedFileAstId>,
67+
/// Reversed mapping lazily derived from [`self.arena`].
68+
///
69+
/// FIXE: Do not store `SyntaxNodePtr` twice.
70+
map: once_cell::sync::OnceCell<FxHashMap<SyntaxNodePtr, ErasedFileAstId>>,
6871
_c: Count<Self>,
6972
}
7073

74+
impl fmt::Debug for AstIdMap {
75+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
76+
f.debug_struct("AstIdMap").field("arena", &self.arena).finish()
77+
}
78+
}
79+
80+
impl PartialEq for AstIdMap {
81+
fn eq(&self, other: &Self) -> bool {
82+
self.arena == other.arena
83+
}
84+
}
85+
impl Eq for AstIdMap {}
86+
7187
impl AstIdMap {
7288
pub(crate) fn from_source(node: &SyntaxNode) -> AstIdMap {
7389
assert!(node.parent().is_none());
@@ -91,9 +107,11 @@ impl AstIdMap {
91107
}
92108
}
93109
});
94-
res.map.extend(res.arena.iter().map(|(idx, ptr)| (ptr.clone(), idx)));
95110
res
96111
}
112+
fn map(&self) -> &FxHashMap<SyntaxNodePtr, ErasedFileAstId> {
113+
self.map.get_or_init(|| self.arena.iter().map(|(idx, ptr)| (ptr.clone(), idx)).collect())
114+
}
97115

98116
pub fn ast_id<N: AstNode>(&self, item: &N) -> FileAstId<N> {
99117
let raw = self.erased_ast_id(item.syntax());
@@ -102,7 +120,7 @@ impl AstIdMap {
102120

103121
fn erased_ast_id(&self, item: &SyntaxNode) -> ErasedFileAstId {
104122
let ptr = SyntaxNodePtr::new(item);
105-
*self.map.get(&ptr).unwrap_or_else(|| {
123+
*self.map().get(&ptr).unwrap_or_else(|| {
106124
panic!(
107125
"Can't find {:?} in AstIdMap:\n{:?}",
108126
item,

0 commit comments

Comments
 (0)