Get rid of double indirection in string interner by using Rc<str>

This commit is contained in:
Vadim Petrochenkov 2016-10-12 20:54:41 +03:00
parent ef3a6a8ee6
commit 6d062809cb
3 changed files with 21 additions and 38 deletions

View File

@ -128,7 +128,7 @@ impl<'tcx> TypeMap<'tcx> {
// Get the string representation of a UniqueTypeId. This method will fail if
// the id is unknown.
fn get_unique_type_id_as_string(&self, unique_type_id: UniqueTypeId) -> Rc<String> {
fn get_unique_type_id_as_string(&self, unique_type_id: UniqueTypeId) -> Rc<str> {
let UniqueTypeId(interner_key) = unique_type_id;
self.unique_id_interner.get(interner_key)
}
@ -299,7 +299,7 @@ impl<'tcx> TypeMap<'tcx> {
// Trim to size before storing permanently
unique_type_id.shrink_to_fit();
let key = self.unique_id_interner.intern(unique_type_id);
let key = self.unique_id_interner.intern(&unique_type_id);
self.type_to_unique_id.insert(type_, UniqueTypeId(key));
return UniqueTypeId(key);
@ -367,7 +367,7 @@ impl<'tcx> TypeMap<'tcx> {
let enum_variant_type_id = format!("{}::{}",
&self.get_unique_type_id_as_string(enum_type_id),
variant_name);
let interner_key = self.unique_id_interner.intern(enum_variant_type_id);
let interner_key = self.unique_id_interner.intern(&enum_variant_type_id);
UniqueTypeId(interner_key)
}
}

View File

@ -478,27 +478,20 @@ pub fn clear_ident_interner() {
/// somehow.
#[derive(Clone, PartialEq, Hash, PartialOrd, Eq, Ord)]
pub struct InternedString {
string: Rc<String>,
string: Rc<str>,
}
impl InternedString {
#[inline]
pub fn new(string: &'static str) -> InternedString {
InternedString {
string: Rc::new(string.to_owned()),
}
}
#[inline]
fn new_from_rc_str(string: Rc<String>) -> InternedString {
InternedString {
string: string,
string: Rc::__from_str(string),
}
}
#[inline]
pub fn new_from_name(name: ast::Name) -> InternedString {
with_ident_interner(|interner| InternedString::new_from_rc_str(interner.get(name)))
with_ident_interner(|interner| InternedString { string: interner.get(name) })
}
}

View File

@ -14,23 +14,13 @@
use ast::Name;
use std::borrow::Borrow;
use std::collections::HashMap;
use std::rc::Rc;
#[derive(PartialEq, Eq, Hash)]
struct RcStr(Rc<String>);
impl Borrow<str> for RcStr {
fn borrow(&self) -> &str {
&self.0
}
}
#[derive(Default)]
pub struct Interner {
names: HashMap<RcStr, Name>,
strings: Vec<Rc<String>>,
names: HashMap<Rc<str>, Name>,
strings: Vec<Rc<str>>,
}
/// When traits can extend traits, we should extend index<Name,T> to get []
@ -47,22 +37,22 @@ impl Interner {
this
}
pub fn intern<T: Borrow<str> + Into<String>>(&mut self, string: T) -> Name {
if let Some(&name) = self.names.get(string.borrow()) {
pub fn intern(&mut self, string: &str) -> Name {
if let Some(&name) = self.names.get(string) {
return name;
}
let name = Name(self.strings.len() as u32);
let string = Rc::new(string.into());
let string = Rc::__from_str(string);
self.strings.push(string.clone());
self.names.insert(RcStr(string), name);
self.names.insert(string, name);
name
}
pub fn gensym(&mut self, string: &str) -> Name {
let gensym = Name(self.strings.len() as u32);
// leave out of `names` to avoid colliding
self.strings.push(Rc::new(string.to_owned()));
self.strings.push(Rc::__from_str(string));
gensym
}
@ -75,7 +65,7 @@ impl Interner {
gensym
}
pub fn get(&self, name: Name) -> Rc<String> {
pub fn get(&self, name: Name) -> Rc<str> {
self.strings[name.0 as usize].clone()
}
@ -109,13 +99,13 @@ mod tests {
assert_eq!(i.gensym("dog"), Name(4));
// gensym tests again with gensym_copy:
assert_eq!(i.gensym_copy(Name(2)), Name(5));
assert_eq!(*i.get(Name(5)), "zebra");
assert_eq!(&*i.get(Name(5)), "zebra");
assert_eq!(i.gensym_copy(Name(2)), Name(6));
assert_eq!(*i.get(Name(6)), "zebra");
assert_eq!(*i.get(Name(0)), "dog");
assert_eq!(*i.get(Name(1)), "cat");
assert_eq!(*i.get(Name(2)), "zebra");
assert_eq!(*i.get(Name(3)), "zebra");
assert_eq!(*i.get(Name(4)), "dog");
assert_eq!(&*i.get(Name(6)), "zebra");
assert_eq!(&*i.get(Name(0)), "dog");
assert_eq!(&*i.get(Name(1)), "cat");
assert_eq!(&*i.get(Name(2)), "zebra");
assert_eq!(&*i.get(Name(3)), "zebra");
assert_eq!(&*i.get(Name(4)), "dog");
}
}