diff options
author | Aleksey Kladov <[email protected]> | 2019-01-08 12:53:32 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-01-08 12:53:32 +0000 |
commit | d4c8310d059e76f28c81e1e404dfe79b982bc23b (patch) | |
tree | 49ace1774d9ed6ebfa141af27368030fa4c3279d /crates/ra_db | |
parent | 5603237c069c600963b8e25481af397b25e3b185 (diff) |
switch interner to use arena
Diffstat (limited to 'crates/ra_db')
-rw-r--r-- | crates/ra_db/Cargo.toml | 1 | ||||
-rw-r--r-- | crates/ra_db/src/lib.rs | 16 | ||||
-rw-r--r-- | crates/ra_db/src/loc2id.rs | 34 |
3 files changed, 15 insertions, 36 deletions
diff --git a/crates/ra_db/Cargo.toml b/crates/ra_db/Cargo.toml index c0e83a140..c43e65051 100644 --- a/crates/ra_db/Cargo.toml +++ b/crates/ra_db/Cargo.toml | |||
@@ -9,6 +9,7 @@ relative-path = "0.4.0" | |||
9 | salsa = "0.9.1" | 9 | salsa = "0.9.1" |
10 | rustc-hash = "1.0" | 10 | rustc-hash = "1.0" |
11 | parking_lot = "0.7.0" | 11 | parking_lot = "0.7.0" |
12 | ra_arena = { path = "../ra_arena" } | ||
12 | ra_syntax = { path = "../ra_syntax" } | 13 | ra_syntax = { path = "../ra_syntax" } |
13 | ra_editor = { path = "../ra_editor" } | 14 | ra_editor = { path = "../ra_editor" } |
14 | test_utils = { path = "../test_utils" } | 15 | test_utils = { path = "../test_utils" } |
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] | ||
25 | macro_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 | |||
38 | pub trait BaseDatabase: salsa::Database { | 24 | pub 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 @@ | |||
1 | use parking_lot::Mutex; | ||
2 | |||
3 | use std::hash::Hash; | 1 | use std::hash::Hash; |
4 | 2 | ||
3 | use parking_lot::Mutex; | ||
5 | use rustc_hash::FxHashMap; | 4 | use rustc_hash::FxHashMap; |
5 | use 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)] |
18 | struct Loc2IdMap<LOC, ID> | 18 | struct Loc2IdMap<LOC, ID> |
19 | where | 19 | where |
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 | ||
27 | impl<LOC, ID> Default for Loc2IdMap<LOC, ID> | 27 | impl<LOC, ID> Default for Loc2IdMap<LOC, ID> |
28 | where | 28 | where |
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 | ||
40 | impl<LOC, ID> Loc2IdMap<LOC, ID> | 40 | impl<LOC, ID> Loc2IdMap<LOC, ID> |
41 | where | 41 | where |
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 | ||
67 | pub 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)] |
73 | pub struct LocationIntener<LOC, ID> | 65 | pub struct LocationIntener<LOC, ID> |
74 | where | 66 | where |
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 | ||
81 | impl<LOC, ID> Default for LocationIntener<LOC, ID> | 73 | impl<LOC, ID> Default for LocationIntener<LOC, ID> |
82 | where | 74 | where |
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 | ||
93 | impl<LOC, ID> LocationIntener<LOC, ID> | 85 | impl<LOC, ID> LocationIntener<LOC, ID> |
94 | where | 86 | where |
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 { |