aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorbors[bot] <bors[bot]@users.noreply.github.com>2019-01-08 12:53:55 +0000
committerbors[bot] <bors[bot]@users.noreply.github.com>2019-01-08 12:53:55 +0000
commit6ba4fa0bc77fddf11e4938c6d79e911a91054a45 (patch)
tree49ace1774d9ed6ebfa141af27368030fa4c3279d /crates
parent5603237c069c600963b8e25481af397b25e3b185 (diff)
parentd4c8310d059e76f28c81e1e404dfe79b982bc23b (diff)
Merge #457
457: switch interner to use arena r=matklad a=matklad Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_arena/src/lib.rs3
-rw-r--r--crates/ra_db/Cargo.toml1
-rw-r--r--crates/ra_db/src/lib.rs16
-rw-r--r--crates/ra_db/src/loc2id.rs34
-rw-r--r--crates/ra_hir/src/ids.rs8
5 files changed, 22 insertions, 40 deletions
diff --git a/crates/ra_arena/src/lib.rs b/crates/ra_arena/src/lib.rs
index 040977dc4..43bfa925a 100644
--- a/crates/ra_arena/src/lib.rs
+++ b/crates/ra_arena/src/lib.rs
@@ -61,6 +61,9 @@ pub trait ArenaId {
61} 61}
62 62
63impl<ID: ArenaId, T> Arena<ID, T> { 63impl<ID: ArenaId, T> Arena<ID, T> {
64 pub fn len(&self) -> usize {
65 self.data.len()
66 }
64 pub fn alloc(&mut self, value: T) -> ID { 67 pub fn alloc(&mut self, value: T) -> ID {
65 let id = RawId(self.data.len() as u32); 68 let id = RawId(self.data.len() as u32);
66 self.data.push(value); 69 self.data.push(value);
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"
9salsa = "0.9.1" 9salsa = "0.9.1"
10rustc-hash = "1.0" 10rustc-hash = "1.0"
11parking_lot = "0.7.0" 11parking_lot = "0.7.0"
12ra_arena = { path = "../ra_arena" }
12ra_syntax = { path = "../ra_syntax" } 13ra_syntax = { path = "../ra_syntax" }
13ra_editor = { path = "../ra_editor" } 14ra_editor = { path = "../ra_editor" }
14test_utils = { path = "../test_utils" } 15test_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]
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 {
diff --git a/crates/ra_hir/src/ids.rs b/crates/ra_hir/src/ids.rs
index 730a3e542..624ab808f 100644
--- a/crates/ra_hir/src/ids.rs
+++ b/crates/ra_hir/src/ids.rs
@@ -93,8 +93,8 @@ impl From<MacroCallId> for HirFileId {
93/// `MacroCallId` identifies a particular macro invocation, like 93/// `MacroCallId` identifies a particular macro invocation, like
94/// `println!("Hello, {}", world)`. 94/// `println!("Hello, {}", world)`.
95#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 95#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
96pub struct MacroCallId(u32); 96pub struct MacroCallId(RawId);
97ra_db::impl_numeric_id!(MacroCallId); 97impl_arena_id!(MacroCallId);
98 98
99#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 99#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
100pub struct MacroCallLoc { 100pub struct MacroCallLoc {
@@ -125,8 +125,8 @@ impl MacroCallLoc {
125/// Def's are a core concept of hir. A `Def` is an Item (function, module, etc) 125/// Def's are a core concept of hir. A `Def` is an Item (function, module, etc)
126/// in a specific module. 126/// in a specific module.
127#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 127#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
128pub struct DefId(u32); 128pub struct DefId(RawId);
129ra_db::impl_numeric_id!(DefId); 129impl_arena_id!(DefId);
130 130
131#[derive(Clone, Debug, PartialEq, Eq, Hash)] 131#[derive(Clone, Debug, PartialEq, Eq, Hash)]
132pub struct DefLoc { 132pub struct DefLoc {