mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-12-24 12:03:31 +00:00
switch interner to use arena
This commit is contained in:
parent
5603237c06
commit
d4c8310d05
6 changed files with 23 additions and 40 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -678,6 +678,7 @@ name = "ra_db"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"parking_lot 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
|
"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_editor 0.1.0",
|
||||||
"ra_syntax 0.1.0",
|
"ra_syntax 0.1.0",
|
||||||
"relative-path 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
"relative-path 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
|
|
|
@ -61,6 +61,9 @@ pub trait ArenaId {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<ID: ArenaId, T> Arena<ID, T> {
|
impl<ID: ArenaId, T> Arena<ID, T> {
|
||||||
|
pub fn len(&self) -> usize {
|
||||||
|
self.data.len()
|
||||||
|
}
|
||||||
pub fn alloc(&mut self, value: T) -> ID {
|
pub fn alloc(&mut self, value: T) -> ID {
|
||||||
let id = RawId(self.data.len() as u32);
|
let id = RawId(self.data.len() as u32);
|
||||||
self.data.push(value);
|
self.data.push(value);
|
||||||
|
|
|
@ -9,6 +9,7 @@ relative-path = "0.4.0"
|
||||||
salsa = "0.9.1"
|
salsa = "0.9.1"
|
||||||
rustc-hash = "1.0"
|
rustc-hash = "1.0"
|
||||||
parking_lot = "0.7.0"
|
parking_lot = "0.7.0"
|
||||||
|
ra_arena = { path = "../ra_arena" }
|
||||||
ra_syntax = { path = "../ra_syntax" }
|
ra_syntax = { path = "../ra_syntax" }
|
||||||
ra_editor = { path = "../ra_editor" }
|
ra_editor = { path = "../ra_editor" }
|
||||||
test_utils = { path = "../test_utils" }
|
test_utils = { path = "../test_utils" }
|
||||||
|
|
|
@ -18,23 +18,9 @@ pub use crate::{
|
||||||
FileTextQuery, FileSourceRootQuery, SourceRootQuery, LocalRootsQuery, LibraryRootsQuery, CrateGraphQuery,
|
FileTextQuery, FileSourceRootQuery, SourceRootQuery, LocalRootsQuery, LibraryRootsQuery, CrateGraphQuery,
|
||||||
FileRelativePathQuery
|
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 {
|
pub trait BaseDatabase: salsa::Database {
|
||||||
fn check_canceled(&self) -> Cancelable<()> {
|
fn check_canceled(&self) -> Cancelable<()> {
|
||||||
if self.salsa_runtime().is_current_revision_canceled() {
|
if self.salsa_runtime().is_current_revision_canceled() {
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
use parking_lot::Mutex;
|
|
||||||
|
|
||||||
use std::hash::Hash;
|
use std::hash::Hash;
|
||||||
|
|
||||||
|
use parking_lot::Mutex;
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
|
use ra_arena::{Arena, ArenaId};
|
||||||
|
|
||||||
/// There are two principle ways to refer to things:
|
/// There are two principle ways to refer to things:
|
||||||
/// - by their locatinon (module in foo/bar/baz.rs at line 42)
|
/// - by their locatinon (module in foo/bar/baz.rs at line 42)
|
||||||
|
@ -17,33 +17,33 @@ use rustc_hash::FxHashMap;
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct Loc2IdMap<LOC, ID>
|
struct Loc2IdMap<LOC, ID>
|
||||||
where
|
where
|
||||||
ID: NumericId,
|
ID: ArenaId + Clone,
|
||||||
LOC: Clone + Eq + Hash,
|
LOC: Clone + Eq + Hash,
|
||||||
{
|
{
|
||||||
|
id2loc: Arena<ID, LOC>,
|
||||||
loc2id: FxHashMap<LOC, ID>,
|
loc2id: FxHashMap<LOC, ID>,
|
||||||
id2loc: FxHashMap<ID, LOC>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<LOC, ID> Default for Loc2IdMap<LOC, ID>
|
impl<LOC, ID> Default for Loc2IdMap<LOC, ID>
|
||||||
where
|
where
|
||||||
ID: NumericId,
|
ID: ArenaId + Clone,
|
||||||
LOC: Clone + Eq + Hash,
|
LOC: Clone + Eq + Hash,
|
||||||
{
|
{
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Loc2IdMap {
|
Loc2IdMap {
|
||||||
|
id2loc: Arena::default(),
|
||||||
loc2id: FxHashMap::default(),
|
loc2id: FxHashMap::default(),
|
||||||
id2loc: FxHashMap::default(),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<LOC, ID> Loc2IdMap<LOC, ID>
|
impl<LOC, ID> Loc2IdMap<LOC, ID>
|
||||||
where
|
where
|
||||||
ID: NumericId,
|
ID: ArenaId + Clone,
|
||||||
LOC: Clone + Eq + Hash,
|
LOC: Clone + Eq + Hash,
|
||||||
{
|
{
|
||||||
pub fn len(&self) -> usize {
|
pub fn len(&self) -> usize {
|
||||||
self.loc2id.len()
|
self.id2loc.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn loc2id(&mut self, loc: &LOC) -> ID {
|
pub fn loc2id(&mut self, loc: &LOC) -> ID {
|
||||||
|
@ -51,28 +51,20 @@ where
|
||||||
Some(id) => return id.clone(),
|
Some(id) => return id.clone(),
|
||||||
None => (),
|
None => (),
|
||||||
}
|
}
|
||||||
let id = self.loc2id.len();
|
let id = self.id2loc.alloc(loc.clone());
|
||||||
assert!(id < u32::max_value() as usize);
|
|
||||||
let id = ID::from_u32(id as u32);
|
|
||||||
self.loc2id.insert(loc.clone(), id.clone());
|
self.loc2id.insert(loc.clone(), id.clone());
|
||||||
self.id2loc.insert(id.clone(), loc.clone());
|
|
||||||
id
|
id
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn id2loc(&self, id: ID) -> LOC {
|
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)]
|
#[derive(Debug)]
|
||||||
pub struct LocationIntener<LOC, ID>
|
pub struct LocationIntener<LOC, ID>
|
||||||
where
|
where
|
||||||
ID: NumericId,
|
ID: ArenaId + Clone,
|
||||||
LOC: Clone + Eq + Hash,
|
LOC: Clone + Eq + Hash,
|
||||||
{
|
{
|
||||||
map: Mutex<Loc2IdMap<LOC, ID>>,
|
map: Mutex<Loc2IdMap<LOC, ID>>,
|
||||||
|
@ -80,7 +72,7 @@ where
|
||||||
|
|
||||||
impl<LOC, ID> Default for LocationIntener<LOC, ID>
|
impl<LOC, ID> Default for LocationIntener<LOC, ID>
|
||||||
where
|
where
|
||||||
ID: NumericId,
|
ID: ArenaId + Clone,
|
||||||
LOC: Clone + Eq + Hash,
|
LOC: Clone + Eq + Hash,
|
||||||
{
|
{
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
|
@ -92,7 +84,7 @@ where
|
||||||
|
|
||||||
impl<LOC, ID> LocationIntener<LOC, ID>
|
impl<LOC, ID> LocationIntener<LOC, ID>
|
||||||
where
|
where
|
||||||
ID: NumericId,
|
ID: ArenaId + Clone,
|
||||||
LOC: Clone + Eq + Hash,
|
LOC: Clone + Eq + Hash,
|
||||||
{
|
{
|
||||||
pub fn len(&self) -> usize {
|
pub fn len(&self) -> usize {
|
||||||
|
|
|
@ -93,8 +93,8 @@ impl From<MacroCallId> for HirFileId {
|
||||||
/// `MacroCallId` identifies a particular macro invocation, like
|
/// `MacroCallId` identifies a particular macro invocation, like
|
||||||
/// `println!("Hello, {}", world)`.
|
/// `println!("Hello, {}", world)`.
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
pub struct MacroCallId(u32);
|
pub struct MacroCallId(RawId);
|
||||||
ra_db::impl_numeric_id!(MacroCallId);
|
impl_arena_id!(MacroCallId);
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
pub struct MacroCallLoc {
|
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)
|
/// Def's are a core concept of hir. A `Def` is an Item (function, module, etc)
|
||||||
/// in a specific module.
|
/// in a specific module.
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
pub struct DefId(u32);
|
pub struct DefId(RawId);
|
||||||
ra_db::impl_numeric_id!(DefId);
|
impl_arena_id!(DefId);
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||||
pub struct DefLoc {
|
pub struct DefLoc {
|
||||||
|
|
Loading…
Reference in a new issue