diff options
| author | bors <bors@rust-lang.org> | 2013-06-12 18:10:49 -0700 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2013-06-12 18:10:49 -0700 |
| commit | da510bfb4a3f6ca805e849372f9bbe7b2b0f6a61 (patch) | |
| tree | cb272a50c99d8001304f044fd8a1128d30654b61 /src/libsyntax/util | |
| parent | 84bed9769b5d15871481b657860ad6a7d0a62f42 (diff) | |
| parent | 5ebffd46d5f7476c8124856c37538088da0c918e (diff) | |
| download | rust-da510bfb4a3f6ca805e849372f9bbe7b2b0f6a61.tar.gz rust-da510bfb4a3f6ca805e849372f9bbe7b2b0f6a61.zip | |
auto merge of #7086 : huonw/rust/5048, r=graydon
Fixes #5048. I'm sure this reduces memory usage, but I can't get cgroups to work properly to actually measure memory. (It doesn't appear to offer much speed improvement, but I'm fairly sure it's not slower.) This is quite huge, so it'd be nice to get a resolution soon.
Diffstat (limited to 'src/libsyntax/util')
| -rw-r--r-- | src/libsyntax/util/interner.rs | 60 | ||||
| -rw-r--r-- | src/libsyntax/util/parser_testing.rs | 22 |
2 files changed, 41 insertions, 41 deletions
diff --git a/src/libsyntax/util/interner.rs b/src/libsyntax/util/interner.rs index b55050184fe..d4f183ada7b 100644 --- a/src/libsyntax/util/interner.rs +++ b/src/libsyntax/util/interner.rs @@ -19,7 +19,6 @@ use core::prelude::*; use core::cmp::Equiv; use core::hashmap::HashMap; -use syntax::parse::token::StringRef; pub struct Interner<T> { priv map: @mut HashMap<T, uint>, @@ -80,8 +79,8 @@ impl<T:Eq + IterBytes + Hash + Const + Copy> Interner<T> { // A StrInterner differs from Interner<String> in that it accepts // borrowed pointers rather than @ ones, resulting in less allocation. pub struct StrInterner { - priv map: @mut HashMap<@~str, uint>, - priv vect: @mut ~[@~str], + priv map: @mut HashMap<@str, uint>, + priv vect: @mut ~[@str], } // when traits can extend traits, we should extend index<uint,T> to get [] @@ -95,37 +94,38 @@ impl StrInterner { pub fn prefill(init: &[&str]) -> StrInterner { let rv = StrInterner::new(); - for init.each() |v| { rv.intern(*v); } + for init.each |&v| { rv.intern(v); } rv } pub fn intern(&self, val: &str) -> uint { - match self.map.find_equiv(&StringRef(val)) { + match self.map.find_equiv(&val) { Some(&idx) => return idx, None => (), } let new_idx = self.len(); - self.map.insert(@val.to_owned(), new_idx); - self.vect.push(@val.to_owned()); + let val = val.to_managed(); + self.map.insert(val, new_idx); + self.vect.push(val); new_idx } pub fn gensym(&self, val: &str) -> uint { let new_idx = self.len(); // leave out of .map to avoid colliding - self.vect.push(@val.to_owned()); + self.vect.push(val.to_managed()); new_idx } // this isn't "pure" in the traditional sense, because it can go from // failing to returning a value as items are interned. But for typestate, // where we first check a pred and then rely on it, ceasing to fail is ok. - pub fn get(&self, idx: uint) -> @~str { self.vect[idx] } + pub fn get(&self, idx: uint) -> @str { self.vect[idx] } pub fn len(&self) -> uint { let vect = &*self.vect; vect.len() } - pub fn find_equiv<Q:Hash + IterBytes + Equiv<@~str>>(&self, val: &Q) + pub fn find_equiv<Q:Hash + IterBytes + Equiv<@str>>(&self, val: &Q) -> Option<uint> { match self.map.find_equiv(val) { Some(v) => Some(*v), @@ -140,41 +140,41 @@ mod tests { #[test] #[should_fail] fn i1 () { - let i : Interner<@~str> = Interner::new(); + let i : Interner<@str> = Interner::new(); i.get(13); } #[test] fn i2 () { - let i : Interner<@~str> = Interner::new(); + let i : Interner<@str> = Interner::new(); // first one is zero: - assert_eq!(i.intern (@~"dog"), 0); + assert_eq!(i.intern (@"dog"), 0); // re-use gets the same entry: - assert_eq!(i.intern (@~"dog"), 0); + assert_eq!(i.intern (@"dog"), 0); // different string gets a different #: - assert_eq!(i.intern (@~"cat"), 1); - assert_eq!(i.intern (@~"cat"), 1); + assert_eq!(i.intern (@"cat"), 1); + assert_eq!(i.intern (@"cat"), 1); // dog is still at zero - assert_eq!(i.intern (@~"dog"), 0); + assert_eq!(i.intern (@"dog"), 0); // gensym gets 3 - assert_eq!(i.gensym (@~"zebra" ), 2); + assert_eq!(i.gensym (@"zebra" ), 2); // gensym of same string gets new number : - assert_eq!(i.gensym (@~"zebra" ), 3); + assert_eq!(i.gensym (@"zebra" ), 3); // gensym of *existing* string gets new number: - assert_eq!(i.gensym (@~"dog"), 4); - assert_eq!(i.get(0), @~"dog"); - assert_eq!(i.get(1), @~"cat"); - assert_eq!(i.get(2), @~"zebra"); - assert_eq!(i.get(3), @~"zebra"); - assert_eq!(i.get(4), @~"dog"); + assert_eq!(i.gensym (@"dog"), 4); + assert_eq!(i.get(0), @"dog"); + assert_eq!(i.get(1), @"cat"); + assert_eq!(i.get(2), @"zebra"); + assert_eq!(i.get(3), @"zebra"); + assert_eq!(i.get(4), @"dog"); } #[test] fn i3 () { - let i : Interner<@~str> = Interner::prefill([@~"Alan",@~"Bob",@~"Carol"]); - assert_eq!(i.get(0), @~"Alan"); - assert_eq!(i.get(1), @~"Bob"); - assert_eq!(i.get(2), @~"Carol"); - assert_eq!(i.intern(@~"Bob"), 1); + let i : Interner<@str> = Interner::prefill([@"Alan",@"Bob",@"Carol"]); + assert_eq!(i.get(0), @"Alan"); + assert_eq!(i.get(1), @"Bob"); + assert_eq!(i.get(2), @"Carol"); + assert_eq!(i.intern(@"Bob"), 1); } } diff --git a/src/libsyntax/util/parser_testing.rs b/src/libsyntax/util/parser_testing.rs index 76055ca7914..d0961ddbc70 100644 --- a/src/libsyntax/util/parser_testing.rs +++ b/src/libsyntax/util/parser_testing.rs @@ -18,50 +18,50 @@ use parse::token; // map a string to tts, using a made-up filename: return both the token_trees // and the ParseSess -pub fn string_to_tts_and_sess (source_str : @~str) -> (~[ast::token_tree],@mut ParseSess) { +pub fn string_to_tts_and_sess (source_str : @str) -> (~[ast::token_tree],@mut ParseSess) { let ps = new_parse_sess(None); - (filemap_to_tts(ps,string_to_filemap(ps,source_str,~"bogofile")),ps) + (filemap_to_tts(ps,string_to_filemap(ps,source_str,@"bogofile")),ps) } -pub fn string_to_parser_and_sess(source_str: @~str) -> (Parser,@mut ParseSess) { +pub fn string_to_parser_and_sess(source_str: @str) -> (Parser,@mut ParseSess) { let ps = new_parse_sess(None); - (new_parser_from_source_str(ps,~[],~"bogofile",source_str),ps) + (new_parser_from_source_str(ps,~[],@"bogofile",source_str),ps) } // map string to parser (via tts) -pub fn string_to_parser(source_str: @~str) -> Parser { +pub fn string_to_parser(source_str: @str) -> Parser { let (p,_) = string_to_parser_and_sess(source_str); p } -pub fn string_to_crate (source_str : @~str) -> @ast::crate { +pub fn string_to_crate (source_str : @str) -> @ast::crate { string_to_parser(source_str).parse_crate_mod() } // parse a string, return an expr -pub fn string_to_expr (source_str : @~str) -> @ast::expr { +pub fn string_to_expr (source_str : @str) -> @ast::expr { string_to_parser(source_str).parse_expr() } // parse a string, return an item -pub fn string_to_item (source_str : @~str) -> Option<@ast::item> { +pub fn string_to_item (source_str : @str) -> Option<@ast::item> { string_to_parser(source_str).parse_item(~[]) } // parse a string, return an item and the ParseSess -pub fn string_to_item_and_sess (source_str : @~str) -> (Option<@ast::item>,@mut ParseSess) { +pub fn string_to_item_and_sess (source_str : @str) -> (Option<@ast::item>,@mut ParseSess) { let (p,ps) = string_to_parser_and_sess(source_str); (p.parse_item(~[]),ps) } // parse a string, return a stmt -pub fn string_to_stmt(source_str : @~str) -> @ast::stmt { +pub fn string_to_stmt(source_str : @str) -> @ast::stmt { string_to_parser(source_str).parse_stmt(~[]) } // parse a string, return a pat. Uses "irrefutable"... which doesn't // (currently) affect parsing. -pub fn string_to_pat(source_str : @~str) -> @ast::pat { +pub fn string_to_pat(source_str : @str) -> @ast::pat { string_to_parser(source_str).parse_pat() } |
