aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_db/src
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-01-08 12:53:32 +0000
committerAleksey Kladov <[email protected]>2019-01-08 12:53:32 +0000
commitd4c8310d059e76f28c81e1e404dfe79b982bc23b (patch)
tree49ace1774d9ed6ebfa141af27368030fa4c3279d /crates/ra_db/src
parent5603237c069c600963b8e25481af397b25e3b185 (diff)
switch interner to use arena
Diffstat (limited to 'crates/ra_db/src')
-rw-r--r--crates/ra_db/src/lib.rs16
-rw-r--r--crates/ra_db/src/loc2id.rs34
2 files changed, 14 insertions, 36 deletions
diff --git a/crates/ra_db/src/lib.rs b/crates/ra_db/src/lib.rs
index 3c41ee56d..732899718 100644
--- a/crates/ra_db/src/lib.rs
+++ b/crates/ra_db/src/lib.rs
@@ -18,23 +18,9 @@ pub use crate::{
18 FileTextQuery, FileSourceRootQuery, SourceRootQuery, LocalRootsQuery, LibraryRootsQuery, CrateGraphQuery, 18 FileTextQuery, FileSourceRootQuery, SourceRootQuery, LocalRootsQuery, LibraryRootsQuery, CrateGraphQuery,
19 FileRelativePathQuery 19 FileRelativePathQuery
20 }, 20 },
21 loc2id::{LocationIntener, NumericId}, 21 loc2id::LocationIntener,
22}; 22};
23 23
24#[macro_export]
25macro_rules! impl_numeric_id {
26 ($id:ident) => {
27 impl $crate::NumericId for $id {
28 fn from_u32(id: u32) -> Self {
29 $id(id)
30 }
31 fn to_u32(self) -> u32 {
32 self.0
33 }
34 }
35 };
36}
37
38pub trait BaseDatabase: salsa::Database { 24pub trait BaseDatabase: salsa::Database {
39 fn check_canceled(&self) -> Cancelable<()> { 25 fn check_canceled(&self) -> Cancelable<()> {
40 if self.salsa_runtime().is_current_revision_canceled() { 26 if self.salsa_runtime().is_current_revision_canceled() {
diff --git a/crates/ra_db/src/loc2id.rs b/crates/ra_db/src/loc2id.rs
index 2dc7930d8..1d6761897 100644
--- a/crates/ra_db/src/loc2id.rs
+++ b/crates/ra_db/src/loc2id.rs
@@ -1,8 +1,8 @@
1use parking_lot::Mutex;
2
3use std::hash::Hash; 1use std::hash::Hash;
4 2
3use parking_lot::Mutex;
5use rustc_hash::FxHashMap; 4use rustc_hash::FxHashMap;
5use ra_arena::{Arena, ArenaId};
6 6
7/// There are two principle ways to refer to things: 7/// There are two principle ways to refer to things:
8/// - by their locatinon (module in foo/bar/baz.rs at line 42) 8/// - by their locatinon (module in foo/bar/baz.rs at line 42)
@@ -17,33 +17,33 @@ use rustc_hash::FxHashMap;
17#[derive(Debug)] 17#[derive(Debug)]
18struct Loc2IdMap<LOC, ID> 18struct Loc2IdMap<LOC, ID>
19where 19where
20 ID: NumericId, 20 ID: ArenaId + Clone,
21 LOC: Clone + Eq + Hash, 21 LOC: Clone + Eq + Hash,
22{ 22{
23 id2loc: Arena<ID, LOC>,
23 loc2id: FxHashMap<LOC, ID>, 24 loc2id: FxHashMap<LOC, ID>,
24 id2loc: FxHashMap<ID, LOC>,
25} 25}
26 26
27impl<LOC, ID> Default for Loc2IdMap<LOC, ID> 27impl<LOC, ID> Default for Loc2IdMap<LOC, ID>
28where 28where
29 ID: NumericId, 29 ID: ArenaId + Clone,
30 LOC: Clone + Eq + Hash, 30 LOC: Clone + Eq + Hash,
31{ 31{
32 fn default() -> Self { 32 fn default() -> Self {
33 Loc2IdMap { 33 Loc2IdMap {
34 id2loc: Arena::default(),
34 loc2id: FxHashMap::default(), 35 loc2id: FxHashMap::default(),
35 id2loc: FxHashMap::default(),
36 } 36 }
37 } 37 }
38} 38}
39 39
40impl<LOC, ID> Loc2IdMap<LOC, ID> 40impl<LOC, ID> Loc2IdMap<LOC, ID>
41where 41where
42 ID: NumericId, 42 ID: ArenaId + Clone,
43 LOC: Clone + Eq + Hash, 43 LOC: Clone + Eq + Hash,
44{ 44{
45 pub fn len(&self) -> usize { 45 pub fn len(&self) -> usize {
46 self.loc2id.len() 46 self.id2loc.len()
47 } 47 }
48 48
49 pub fn loc2id(&mut self, loc: &LOC) -> ID { 49 pub fn loc2id(&mut self, loc: &LOC) -> ID {
@@ -51,28 +51,20 @@ where
51 Some(id) => return id.clone(), 51 Some(id) => return id.clone(),
52 None => (), 52 None => (),
53 } 53 }
54 let id = self.loc2id.len(); 54 let id = self.id2loc.alloc(loc.clone());
55 assert!(id < u32::max_value() as usize);
56 let id = ID::from_u32(id as u32);
57 self.loc2id.insert(loc.clone(), id.clone()); 55 self.loc2id.insert(loc.clone(), id.clone());
58 self.id2loc.insert(id.clone(), loc.clone());
59 id 56 id
60 } 57 }
61 58
62 pub fn id2loc(&self, id: ID) -> LOC { 59 pub fn id2loc(&self, id: ID) -> LOC {
63 self.id2loc[&id].clone() 60 self.id2loc[id].clone()
64 } 61 }
65} 62}
66 63
67pub trait NumericId: Clone + Eq + Hash {
68 fn from_u32(id: u32) -> Self;
69 fn to_u32(self) -> u32;
70}
71
72#[derive(Debug)] 64#[derive(Debug)]
73pub struct LocationIntener<LOC, ID> 65pub struct LocationIntener<LOC, ID>
74where 66where
75 ID: NumericId, 67 ID: ArenaId + Clone,
76 LOC: Clone + Eq + Hash, 68 LOC: Clone + Eq + Hash,
77{ 69{
78 map: Mutex<Loc2IdMap<LOC, ID>>, 70 map: Mutex<Loc2IdMap<LOC, ID>>,
@@ -80,7 +72,7 @@ where
80 72
81impl<LOC, ID> Default for LocationIntener<LOC, ID> 73impl<LOC, ID> Default for LocationIntener<LOC, ID>
82where 74where
83 ID: NumericId, 75 ID: ArenaId + Clone,
84 LOC: Clone + Eq + Hash, 76 LOC: Clone + Eq + Hash,
85{ 77{
86 fn default() -> Self { 78 fn default() -> Self {
@@ -92,7 +84,7 @@ where
92 84
93impl<LOC, ID> LocationIntener<LOC, ID> 85impl<LOC, ID> LocationIntener<LOC, ID>
94where 86where
95 ID: NumericId, 87 ID: ArenaId + Clone,
96 LOC: Clone + Eq + Hash, 88 LOC: Clone + Eq + Hash,
97{ 89{
98 pub fn len(&self) -> usize { 90 pub fn len(&self) -> usize {