librustc: Optimize metadata::decoder::item_name.

This commit is contained in:
Patrick Walton 2013-04-02 16:20:02 -07:00
parent 53f54dda60
commit 4c29b4cb93
4 changed files with 48 additions and 2 deletions

View File

@ -67,6 +67,15 @@ pub fn from_bytes_with_null<'a>(vv: &'a [u8]) -> &'a str {
return unsafe { raw::from_bytes_with_null(vv) };
}
pub fn from_bytes_slice<'a>(vector: &'a [u8]) -> &'a str {
unsafe {
assert!(is_utf8(vector));
let (ptr, len): (*u8, uint) = ::cast::transmute(vector);
let string: &'a str = ::cast::transmute((ptr, len + 1));
string
}
}
/// Copy a slice into a new unique str
pub fn from_slice(s: &str) -> ~str {
unsafe { raw::slice_bytes_owned(s, 0, len(s)) }

View File

@ -37,7 +37,7 @@
use syntax::ast_map;
use syntax::attr;
use syntax::diagnostic::span_handler;
use syntax::parse::token::{ident_interner, special_idents};
use syntax::parse::token::{StringRef, ident_interner, special_idents};
use syntax::print::pprust;
use syntax::{ast, ast_util};
use syntax::codemap;
@ -322,7 +322,13 @@ fn item_path(intr: @ident_interner, item_doc: ebml::Doc) -> ast_map::path {
fn item_name(intr: @ident_interner, item: ebml::Doc) -> ast::ident {
let name = reader::get_doc(item, tag_paths_data_name);
intr.intern(@str::from_bytes(reader::doc_data(name)))
do reader::with_doc_data(name) |data| {
let string = str::from_bytes_slice(data);
match intr.find_equiv(&StringRef(string)) {
None => intr.intern(@(string.to_owned())),
Some(val) => val,
}
}
}
fn item_to_def_like(item: ebml::Doc, did: ast::def_id, cnum: ast::crate_num)

View File

@ -18,9 +18,11 @@
use core::cast;
use core::char;
use core::cmp::Equiv;
use core::hashmap::HashSet;
use core::str;
use core::task;
use core::to_bytes;
#[auto_encode]
#[auto_decode]
@ -355,6 +357,19 @@ pub mod special_idents {
pub static type_self: ident = ident { repr: 36u, ctxt: 0}; // `Self`
}
pub struct StringRef<'self>(&'self str);
impl<'self> Equiv<@~str> for StringRef<'self> {
#[inline(always)]
fn equiv(&self, other: &@~str) -> bool { str::eq_slice(**self, **other) }
}
impl<'self> to_bytes::IterBytes for StringRef<'self> {
fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) {
(**self).iter_bytes(lsb0, f);
}
}
pub struct ident_interner {
priv interner: Interner<@~str>,
}
@ -372,6 +387,13 @@ fn get(&self, idx: ast::ident) -> @~str {
fn len(&self) -> uint {
self.interner.len()
}
fn find_equiv<Q:Hash + IterBytes + Equiv<@~str>>(&self, val: &Q)
-> Option<ast::ident> {
match self.interner.find_equiv(val) {
Some(v) => Some(ast::ident { repr: v }),
None => None,
}
}
}
pub fn mk_ident_interner() -> @ident_interner {

View File

@ -16,6 +16,7 @@
#[macro_escape];
use core::prelude::*;
use core::cmp::Equiv;
use core::hashmap::HashMap;
pub struct Interner<T> {
@ -67,6 +68,14 @@ fn gensym(&self, val: T) -> uint {
fn get(&self, idx: uint) -> T { self.vect[idx] }
fn len(&self) -> uint { let vect = &*self.vect; vect.len() }
fn find_equiv<Q:Hash + IterBytes + Equiv<T>>(&self, val: &Q)
-> Option<uint> {
match self.map.find_equiv(val) {
Some(v) => Some(*v),
None => None,
}
}
}
/* Key for thread-local data for sneaking interner information to the