From d4c8310d059e76f28c81e1e404dfe79b982bc23b Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 8 Jan 2019 15:53:32 +0300 Subject: switch interner to use arena --- Cargo.lock | 1 + crates/ra_arena/src/lib.rs | 3 +++ crates/ra_db/Cargo.toml | 1 + crates/ra_db/src/lib.rs | 16 +--------------- crates/ra_db/src/loc2id.rs | 34 +++++++++++++--------------------- crates/ra_hir/src/ids.rs | 8 ++++---- 6 files changed, 23 insertions(+), 40 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6ef51fed0..c84fa0c02 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -678,6 +678,7 @@ name = "ra_db" version = "0.1.0" dependencies = [ "parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)", + "ra_arena 0.1.0", "ra_editor 0.1.0", "ra_syntax 0.1.0", "relative-path 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", 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 { } impl Arena { + pub fn len(&self) -> usize { + self.data.len() + } pub fn alloc(&mut self, value: T) -> ID { let id = RawId(self.data.len() as u32); 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" salsa = "0.9.1" rustc-hash = "1.0" parking_lot = "0.7.0" +ra_arena = { path = "../ra_arena" } ra_syntax = { path = "../ra_syntax" } ra_editor = { path = "../ra_editor" } 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::{ FileTextQuery, FileSourceRootQuery, SourceRootQuery, LocalRootsQuery, LibraryRootsQuery, CrateGraphQuery, FileRelativePathQuery }, - loc2id::{LocationIntener, NumericId}, + loc2id::LocationIntener, }; -#[macro_export] -macro_rules! impl_numeric_id { - ($id:ident) => { - impl $crate::NumericId for $id { - fn from_u32(id: u32) -> Self { - $id(id) - } - fn to_u32(self) -> u32 { - self.0 - } - } - }; -} - pub trait BaseDatabase: salsa::Database { fn check_canceled(&self) -> Cancelable<()> { 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 @@ -use parking_lot::Mutex; - use std::hash::Hash; +use parking_lot::Mutex; use rustc_hash::FxHashMap; +use ra_arena::{Arena, ArenaId}; /// There are two principle ways to refer to things: /// - by their locatinon (module in foo/bar/baz.rs at line 42) @@ -17,33 +17,33 @@ use rustc_hash::FxHashMap; #[derive(Debug)] struct Loc2IdMap where - ID: NumericId, + ID: ArenaId + Clone, LOC: Clone + Eq + Hash, { + id2loc: Arena, loc2id: FxHashMap, - id2loc: FxHashMap, } impl Default for Loc2IdMap where - ID: NumericId, + ID: ArenaId + Clone, LOC: Clone + Eq + Hash, { fn default() -> Self { Loc2IdMap { + id2loc: Arena::default(), loc2id: FxHashMap::default(), - id2loc: FxHashMap::default(), } } } impl Loc2IdMap where - ID: NumericId, + ID: ArenaId + Clone, LOC: Clone + Eq + Hash, { pub fn len(&self) -> usize { - self.loc2id.len() + self.id2loc.len() } pub fn loc2id(&mut self, loc: &LOC) -> ID { @@ -51,28 +51,20 @@ where Some(id) => return id.clone(), None => (), } - let id = self.loc2id.len(); - assert!(id < u32::max_value() as usize); - let id = ID::from_u32(id as u32); + let id = self.id2loc.alloc(loc.clone()); self.loc2id.insert(loc.clone(), id.clone()); - self.id2loc.insert(id.clone(), loc.clone()); id } pub fn id2loc(&self, id: ID) -> LOC { - self.id2loc[&id].clone() + self.id2loc[id].clone() } } -pub trait NumericId: Clone + Eq + Hash { - fn from_u32(id: u32) -> Self; - fn to_u32(self) -> u32; -} - #[derive(Debug)] pub struct LocationIntener where - ID: NumericId, + ID: ArenaId + Clone, LOC: Clone + Eq + Hash, { map: Mutex>, @@ -80,7 +72,7 @@ where impl Default for LocationIntener where - ID: NumericId, + ID: ArenaId + Clone, LOC: Clone + Eq + Hash, { fn default() -> Self { @@ -92,7 +84,7 @@ where impl LocationIntener where - ID: NumericId, + ID: ArenaId + Clone, LOC: Clone + Eq + Hash, { 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 for HirFileId { /// `MacroCallId` identifies a particular macro invocation, like /// `println!("Hello, {}", world)`. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct MacroCallId(u32); -ra_db::impl_numeric_id!(MacroCallId); +pub struct MacroCallId(RawId); +impl_arena_id!(MacroCallId); #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] pub struct MacroCallLoc { @@ -125,8 +125,8 @@ impl MacroCallLoc { /// Def's are a core concept of hir. A `Def` is an Item (function, module, etc) /// in a specific module. #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -pub struct DefId(u32); -ra_db::impl_numeric_id!(DefId); +pub struct DefId(RawId); +impl_arena_id!(DefId); #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub struct DefLoc { -- cgit v1.2.3