Skip to content

Commit 9946def

Browse files
bors[bot]matklad
andauthored
Merge #10877
10877: feat: make hightlighting linear r=matklad a=matklad In https://youtu.be/qvIZZf5dmTE, we've noticed that AstIdMap does a linear lookup when going from SyntaxNode to Id. This leads to accidentally quadratic overall performance. Replace linear lookup with a O(1) hashmap lookup. Future work: don't duplicate `SyntaxNodePtr` in `AstIdMap` and switch to "call site dependency injection" style storage (eg, store a `HashSet<ErasedFileAstId>`). See the explanation of the work here on YouTube :-) As you can see from then benchmark results, this doesn't actually make analysis stats fastre. I am a bit mystified as to why this is happening to be honest. 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 ``` This PR: ``` 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 ``` I think we probably should merge the first commit here, but not the second. Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 3137327 + 278e7c3 commit 9946def

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-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+
hashbrown = { version = "0.11", features = ["inline-more"], default-features = false }
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: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@
88
use std::{
99
any::type_name,
1010
fmt,
11-
hash::{Hash, Hasher},
11+
hash::{BuildHasher, BuildHasherDefault, Hash, Hasher},
1212
marker::PhantomData,
1313
};
1414

1515
use la_arena::{Arena, Idx};
1616
use profile::Count;
17+
use rustc_hash::FxHasher;
1718
use syntax::{ast, match_ast, AstNode, AstPtr, SyntaxNode, SyntaxNodePtr};
1819

1920
/// `AstId` points to an AST node in a specific file.
@@ -60,12 +61,28 @@ impl<N: AstNode> FileAstId<N> {
6061
type ErasedFileAstId = Idx<SyntaxNodePtr>;
6162

6263
/// Maps items' `SyntaxNode`s to `ErasedFileAstId`s and back.
63-
#[derive(Debug, PartialEq, Eq, Default)]
64+
#[derive(Default)]
6465
pub struct AstIdMap {
66+
/// Maps stable id to unstable ptr.
6567
arena: Arena<SyntaxNodePtr>,
68+
/// Reverse: map ptr to id.
69+
map: hashbrown::HashMap<Idx<SyntaxNodePtr>, (), ()>,
6670
_c: Count<Self>,
6771
}
6872

73+
impl fmt::Debug for AstIdMap {
74+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75+
f.debug_struct("AstIdMap").field("arena", &self.arena).finish()
76+
}
77+
}
78+
79+
impl PartialEq for AstIdMap {
80+
fn eq(&self, other: &Self) -> bool {
81+
self.arena == other.arena
82+
}
83+
}
84+
impl Eq for AstIdMap {}
85+
6986
impl AstIdMap {
7087
pub(crate) fn from_source(node: &SyntaxNode) -> AstIdMap {
7188
assert!(node.parent().is_none());
@@ -89,6 +106,16 @@ impl AstIdMap {
89106
}
90107
}
91108
});
109+
res.map = hashbrown::HashMap::with_capacity_and_hasher(res.arena.len(), ());
110+
for (idx, ptr) in res.arena.iter() {
111+
let hash = hash_ptr(ptr);
112+
match res.map.raw_entry_mut().from_hash(hash, |idx2| *idx2 == idx) {
113+
hashbrown::hash_map::RawEntryMut::Occupied(_) => unreachable!(),
114+
hashbrown::hash_map::RawEntryMut::Vacant(entry) => {
115+
entry.insert_with_hasher(hash, idx, (), |&idx| hash_ptr(&res.arena[idx]));
116+
}
117+
}
118+
}
92119
res
93120
}
94121

@@ -98,8 +125,9 @@ impl AstIdMap {
98125
}
99126
fn erased_ast_id(&self, item: &SyntaxNode) -> ErasedFileAstId {
100127
let ptr = SyntaxNodePtr::new(item);
101-
match self.arena.iter().find(|(_id, i)| **i == ptr) {
102-
Some((it, _)) => it,
128+
let hash = hash_ptr(&ptr);
129+
match self.map.raw_entry().from_hash(hash, |&idx| self.arena[idx] == ptr) {
130+
Some((&idx, &())) => idx,
103131
None => panic!(
104132
"Can't find {:?} in AstIdMap:\n{:?}",
105133
item,
@@ -117,6 +145,12 @@ impl AstIdMap {
117145
}
118146
}
119147

148+
fn hash_ptr(ptr: &SyntaxNodePtr) -> u64 {
149+
let mut hasher = BuildHasherDefault::<FxHasher>::default().build_hasher();
150+
ptr.hash(&mut hasher);
151+
hasher.finish()
152+
}
153+
120154
/// Walks the subtree in bdfs order, calling `f` for each node. What is bdfs
121155
/// order? It is a mix of breadth-first and depth first orders. Nodes for which
122156
/// `f` returns true are visited breadth-first, all the other nodes are explored

0 commit comments

Comments
 (0)