Skip to content

Commit 4c29b4c

Browse files
committed
librustc: Optimize metadata::decoder::item_name.
1 parent 53f54dd commit 4c29b4c

File tree

4 files changed

+48
-2
lines changed

4 files changed

+48
-2
lines changed

src/libcore/str.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ pub fn from_bytes_with_null<'a>(vv: &'a [u8]) -> &'a str {
6767
return unsafe { raw::from_bytes_with_null(vv) };
6868
}
6969

70+
pub fn from_bytes_slice<'a>(vector: &'a [u8]) -> &'a str {
71+
unsafe {
72+
assert!(is_utf8(vector));
73+
let (ptr, len): (*u8, uint) = ::cast::transmute(vector);
74+
let string: &'a str = ::cast::transmute((ptr, len + 1));
75+
string
76+
}
77+
}
78+
7079
/// Copy a slice into a new unique str
7180
pub fn from_slice(s: &str) -> ~str {
7281
unsafe { raw::slice_bytes_owned(s, 0, len(s)) }

src/librustc/metadata/decoder.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use std::serialize::Decodable;
3737
use syntax::ast_map;
3838
use syntax::attr;
3939
use syntax::diagnostic::span_handler;
40-
use syntax::parse::token::{ident_interner, special_idents};
40+
use syntax::parse::token::{StringRef, ident_interner, special_idents};
4141
use syntax::print::pprust;
4242
use syntax::{ast, ast_util};
4343
use syntax::codemap;
@@ -322,7 +322,13 @@ fn item_path(intr: @ident_interner, item_doc: ebml::Doc) -> ast_map::path {
322322
323323
fn item_name(intr: @ident_interner, item: ebml::Doc) -> ast::ident {
324324
let name = reader::get_doc(item, tag_paths_data_name);
325-
intr.intern(@str::from_bytes(reader::doc_data(name)))
325+
do reader::with_doc_data(name) |data| {
326+
let string = str::from_bytes_slice(data);
327+
match intr.find_equiv(&StringRef(string)) {
328+
None => intr.intern(@(string.to_owned())),
329+
Some(val) => val,
330+
}
331+
}
326332
}
327333
328334
fn item_to_def_like(item: ebml::Doc, did: ast::def_id, cnum: ast::crate_num)

src/libsyntax/parse/token.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ use util::interner;
1818

1919
use core::cast;
2020
use core::char;
21+
use core::cmp::Equiv;
2122
use core::hashmap::HashSet;
2223
use core::str;
2324
use core::task;
25+
use core::to_bytes;
2426

2527
#[auto_encode]
2628
#[auto_decode]
@@ -355,6 +357,19 @@ pub mod special_idents {
355357
pub static type_self: ident = ident { repr: 36u, ctxt: 0}; // `Self`
356358
}
357359
360+
pub struct StringRef<'self>(&'self str);
361+
362+
impl<'self> Equiv<@~str> for StringRef<'self> {
363+
#[inline(always)]
364+
fn equiv(&self, other: &@~str) -> bool { str::eq_slice(**self, **other) }
365+
}
366+
367+
impl<'self> to_bytes::IterBytes for StringRef<'self> {
368+
fn iter_bytes(&self, lsb0: bool, f: to_bytes::Cb) {
369+
(**self).iter_bytes(lsb0, f);
370+
}
371+
}
372+
358373
pub struct ident_interner {
359374
priv interner: Interner<@~str>,
360375
}
@@ -372,6 +387,13 @@ pub impl ident_interner {
372387
fn len(&self) -> uint {
373388
self.interner.len()
374389
}
390+
fn find_equiv<Q:Hash + IterBytes + Equiv<@~str>>(&self, val: &Q)
391+
-> Option<ast::ident> {
392+
match self.interner.find_equiv(val) {
393+
Some(v) => Some(ast::ident { repr: v }),
394+
None => None,
395+
}
396+
}
375397
}
376398
377399
pub fn mk_ident_interner() -> @ident_interner {

src/libsyntax/util/interner.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#[macro_escape];
1717

1818
use core::prelude::*;
19+
use core::cmp::Equiv;
1920
use core::hashmap::HashMap;
2021

2122
pub struct Interner<T> {
@@ -67,6 +68,14 @@ pub impl<T:Eq + IterBytes + Hash + Const + Copy> Interner<T> {
6768
fn get(&self, idx: uint) -> T { self.vect[idx] }
6869

6970
fn len(&self) -> uint { let vect = &*self.vect; vect.len() }
71+
72+
fn find_equiv<Q:Hash + IterBytes + Equiv<T>>(&self, val: &Q)
73+
-> Option<uint> {
74+
match self.map.find_equiv(val) {
75+
Some(v) => Some(*v),
76+
None => None,
77+
}
78+
}
7079
}
7180

7281
/* Key for thread-local data for sneaking interner information to the

0 commit comments

Comments
 (0)