Skip to content

Commit 51e85f5

Browse files
committed
Avoid unnecessary heap allocations in the metadata ty decoder
1 parent b870477 commit 51e85f5

File tree

1 file changed

+43
-46
lines changed

1 file changed

+43
-46
lines changed

src/librustc/metadata/tydecode.rs

+43-46
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,17 @@ pub struct PState {
6262
tcx: ty::ctxt
6363
}
6464

65-
fn peek(st: @mut PState) -> char {
65+
fn peek(st: &PState) -> char {
6666
st.data[st.pos] as char
6767
}
6868

69-
fn next(st: @mut PState) -> char {
69+
fn next(st: &mut PState) -> char {
7070
let ch = st.data[st.pos] as char;
7171
st.pos = st.pos + 1u;
7272
return ch;
7373
}
7474

75-
fn next_byte(st: @mut PState) -> u8 {
75+
fn next_byte(st: &mut PState) -> u8 {
7676
let b = st.data[st.pos];
7777
st.pos = st.pos + 1u;
7878
return b;
@@ -92,20 +92,20 @@ fn scan<R>(st: &mut PState, is_last: &fn(char) -> bool,
9292
return op(st.data.slice(start_pos, end_pos));
9393
}
9494

95-
pub fn parse_ident(st: @mut PState, last: char) -> ast::ident {
95+
pub fn parse_ident(st: &mut PState, last: char) -> ast::ident {
9696
fn is_last(b: char, c: char) -> bool { return c == b; }
9797
return parse_ident_(st, |a| is_last(last, a) );
9898
}
9999

100-
fn parse_ident_(st: @mut PState, is_last: @fn(char) -> bool) ->
100+
fn parse_ident_(st: &mut PState, is_last: @fn(char) -> bool) ->
101101
ast::ident {
102102
let rslt = scan(st, is_last, str::from_bytes);
103103
return st.tcx.sess.ident_of(rslt);
104104
}
105105

106106
pub fn parse_state_from_data(data: @~[u8], crate_num: int,
107-
pos: uint, tcx: ty::ctxt) -> @mut PState {
108-
@mut PState {
107+
pos: uint, tcx: ty::ctxt) -> PState {
108+
PState {
109109
data: data,
110110
crate: crate_num,
111111
pos: pos,
@@ -115,23 +115,23 @@ pub fn parse_state_from_data(data: @~[u8], crate_num: int,
115115

116116
pub fn parse_ty_data(data: @~[u8], crate_num: int, pos: uint, tcx: ty::ctxt,
117117
conv: conv_did) -> ty::t {
118-
let st = parse_state_from_data(data, crate_num, pos, tcx);
119-
parse_ty(st, conv)
118+
let mut st = parse_state_from_data(data, crate_num, pos, tcx);
119+
parse_ty(&mut st, conv)
120120
}
121121

122122
pub fn parse_bare_fn_ty_data(data: @~[u8], crate_num: int, pos: uint, tcx: ty::ctxt,
123123
conv: conv_did) -> ty::BareFnTy {
124-
let st = parse_state_from_data(data, crate_num, pos, tcx);
125-
parse_bare_fn_ty(st, conv)
124+
let mut st = parse_state_from_data(data, crate_num, pos, tcx);
125+
parse_bare_fn_ty(&mut st, conv)
126126
}
127127

128128
pub fn parse_trait_ref_data(data: @~[u8], crate_num: int, pos: uint, tcx: ty::ctxt,
129129
conv: conv_did) -> ty::TraitRef {
130-
let st = parse_state_from_data(data, crate_num, pos, tcx);
131-
parse_trait_ref(st, conv)
130+
let mut st = parse_state_from_data(data, crate_num, pos, tcx);
131+
parse_trait_ref(&mut st, conv)
132132
}
133133

134-
fn parse_path(st: @mut PState) -> @ast::Path {
134+
fn parse_path(st: &mut PState) -> @ast::Path {
135135
let mut idents: ~[ast::ident] = ~[];
136136
fn is_last(c: char) -> bool { return c == '(' || c == ':'; }
137137
idents.push(parse_ident_(st, is_last));
@@ -151,7 +151,7 @@ fn parse_path(st: @mut PState) -> @ast::Path {
151151
};
152152
}
153153

154-
fn parse_sigil(st: @mut PState) -> ast::Sigil {
154+
fn parse_sigil(st: &mut PState) -> ast::Sigil {
155155
match next(st) {
156156
'@' => ast::ManagedSigil,
157157
'~' => ast::OwnedSigil,
@@ -160,7 +160,7 @@ fn parse_sigil(st: @mut PState) -> ast::Sigil {
160160
}
161161
}
162162

163-
fn parse_vstore(st: @mut PState) -> ty::vstore {
163+
fn parse_vstore(st: &mut PState) -> ty::vstore {
164164
assert_eq!(next(st), '/');
165165

166166
let c = peek(st);
@@ -178,7 +178,7 @@ fn parse_vstore(st: @mut PState) -> ty::vstore {
178178
}
179179
}
180180

181-
fn parse_trait_store(st: @mut PState) -> ty::TraitStore {
181+
fn parse_trait_store(st: &mut PState) -> ty::TraitStore {
182182
match next(st) {
183183
'~' => ty::UniqTraitStore,
184184
'@' => ty::BoxTraitStore,
@@ -187,10 +187,10 @@ fn parse_trait_store(st: @mut PState) -> ty::TraitStore {
187187
}
188188
}
189189

190-
fn parse_substs(st: @mut PState, conv: conv_did) -> ty::substs {
191-
let self_r = parse_opt(st, || parse_region(st) );
190+
fn parse_substs(st: &mut PState, conv: conv_did) -> ty::substs {
191+
let self_r = parse_opt(st, |st| parse_region(st) );
192192

193-
let self_ty = parse_opt(st, || parse_ty(st, conv) );
193+
let self_ty = parse_opt(st, |st| parse_ty(st, conv) );
194194

195195
assert_eq!(next(st), '[');
196196
let mut params: ~[ty::t] = ~[];
@@ -204,7 +204,7 @@ fn parse_substs(st: @mut PState, conv: conv_did) -> ty::substs {
204204
};
205205
}
206206

207-
fn parse_bound_region(st: @mut PState) -> ty::bound_region {
207+
fn parse_bound_region(st: &mut PState) -> ty::bound_region {
208208
match next(st) {
209209
's' => ty::br_self,
210210
'a' => {
@@ -222,7 +222,7 @@ fn parse_bound_region(st: @mut PState) -> ty::bound_region {
222222
}
223223
}
224224

225-
fn parse_region(st: @mut PState) -> ty::Region {
225+
fn parse_region(st: &mut PState) -> ty::Region {
226226
match next(st) {
227227
'b' => {
228228
ty::re_bound(parse_bound_region(st))
@@ -251,15 +251,15 @@ fn parse_region(st: @mut PState) -> ty::Region {
251251
}
252252
}
253253

254-
fn parse_opt<T>(st: @mut PState, f: &fn() -> T) -> Option<T> {
254+
fn parse_opt<T>(st: &mut PState, f: &fn(&mut PState) -> T) -> Option<T> {
255255
match next(st) {
256256
'n' => None,
257-
's' => Some(f()),
257+
's' => Some(f(st)),
258258
_ => fail!("parse_opt: bad input")
259259
}
260260
}
261261

262-
fn parse_str(st: @mut PState, term: char) -> ~str {
262+
fn parse_str(st: &mut PState, term: char) -> ~str {
263263
let mut result = ~"";
264264
while peek(st) != term {
265265
result += str::from_byte(next_byte(st));
@@ -268,13 +268,13 @@ fn parse_str(st: @mut PState, term: char) -> ~str {
268268
return result;
269269
}
270270

271-
fn parse_trait_ref(st: @mut PState, conv: conv_did) -> ty::TraitRef {
271+
fn parse_trait_ref(st: &mut PState, conv: conv_did) -> ty::TraitRef {
272272
let def = parse_def(st, NominalType, conv);
273273
let substs = parse_substs(st, conv);
274274
ty::TraitRef {def_id: def, substs: substs}
275275
}
276276

277-
fn parse_ty(st: @mut PState, conv: conv_did) -> ty::t {
277+
fn parse_ty(st: &mut PState, conv: conv_did) -> ty::t {
278278
match next(st) {
279279
'n' => return ty::mk_nil(),
280280
'z' => return ty::mk_bot(),
@@ -370,8 +370,8 @@ fn parse_ty(st: @mut PState, conv: conv_did) -> ty::t {
370370
match st.tcx.rcache.find(&key) {
371371
Some(&tt) => return tt,
372372
None => {
373-
let ps = @mut PState {pos: pos ,.. copy *st};
374-
let tt = parse_ty(ps, conv);
373+
let mut ps = PState {pos: pos ,.. copy *st};
374+
let tt = parse_ty(&mut ps, conv);
375375
st.tcx.rcache.insert(key, tt);
376376
return tt;
377377
}
@@ -394,28 +394,25 @@ fn parse_ty(st: @mut PState, conv: conv_did) -> ty::t {
394394
}
395395
}
396396

397-
fn parse_mutability(st: @mut PState) -> ast::mutability {
397+
fn parse_mutability(st: &mut PState) -> ast::mutability {
398398
match peek(st) {
399399
'm' => { next(st); ast::m_mutbl }
400400
'?' => { next(st); ast::m_const }
401401
_ => { ast::m_imm }
402402
}
403403
}
404404

405-
fn parse_mt(st: @mut PState, conv: conv_did) -> ty::mt {
405+
fn parse_mt(st: &mut PState, conv: conv_did) -> ty::mt {
406406
let m = parse_mutability(st);
407407
ty::mt { ty: parse_ty(st, conv), mutbl: m }
408408
}
409409

410-
fn parse_def(st: @mut PState, source: DefIdSource,
410+
fn parse_def(st: &mut PState, source: DefIdSource,
411411
conv: conv_did) -> ast::def_id {
412-
let mut def = ~[];
413-
while peek(st) != '|' { def.push(next_byte(st)); }
414-
st.pos = st.pos + 1u;
415-
return conv(source, parse_def_id(def));
412+
return conv(source, scan(st, |c| { c == '|' }, parse_def_id));
416413
}
417414

418-
fn parse_uint(st: @mut PState) -> uint {
415+
fn parse_uint(st: &mut PState) -> uint {
419416
let mut n = 0;
420417
loop {
421418
let cur = peek(st);
@@ -426,7 +423,7 @@ fn parse_uint(st: @mut PState) -> uint {
426423
};
427424
}
428425

429-
fn parse_hex(st: @mut PState) -> uint {
426+
fn parse_hex(st: &mut PState) -> uint {
430427
let mut n = 0u;
431428
loop {
432429
let cur = peek(st);
@@ -449,7 +446,7 @@ fn parse_purity(c: char) -> purity {
449446
}
450447
}
451448

452-
fn parse_abi_set(st: @mut PState) -> AbiSet {
449+
fn parse_abi_set(st: &mut PState) -> AbiSet {
453450
assert_eq!(next(st), '[');
454451
let mut abis = AbiSet::empty();
455452
while peek(st) != ']' {
@@ -470,7 +467,7 @@ fn parse_onceness(c: char) -> ast::Onceness {
470467
}
471468
}
472469

473-
fn parse_closure_ty(st: @mut PState, conv: conv_did) -> ty::ClosureTy {
470+
fn parse_closure_ty(st: &mut PState, conv: conv_did) -> ty::ClosureTy {
474471
let sigil = parse_sigil(st);
475472
let purity = parse_purity(next(st));
476473
let onceness = parse_onceness(next(st));
@@ -487,7 +484,7 @@ fn parse_closure_ty(st: @mut PState, conv: conv_did) -> ty::ClosureTy {
487484
}
488485
}
489486

490-
fn parse_bare_fn_ty(st: @mut PState, conv: conv_did) -> ty::BareFnTy {
487+
fn parse_bare_fn_ty(st: &mut PState, conv: conv_did) -> ty::BareFnTy {
491488
let purity = parse_purity(next(st));
492489
let abi = parse_abi_set(st);
493490
let sig = parse_sig(st, conv);
@@ -498,7 +495,7 @@ fn parse_bare_fn_ty(st: @mut PState, conv: conv_did) -> ty::BareFnTy {
498495
}
499496
}
500497

501-
fn parse_sig(st: @mut PState, conv: conv_did) -> ty::FnSig {
498+
fn parse_sig(st: &mut PState, conv: conv_did) -> ty::FnSig {
502499
assert_eq!(next(st), '[');
503500
let mut inputs = ~[];
504501
while peek(st) != ']' {
@@ -541,16 +538,16 @@ pub fn parse_type_param_def_data(data: @~[u8], start: uint,
541538
crate_num: int, tcx: ty::ctxt,
542539
conv: conv_did) -> ty::TypeParameterDef
543540
{
544-
let st = parse_state_from_data(data, crate_num, start, tcx);
545-
parse_type_param_def(st, conv)
541+
let mut st = parse_state_from_data(data, crate_num, start, tcx);
542+
parse_type_param_def(&mut st, conv)
546543
}
547544

548-
fn parse_type_param_def(st: @mut PState, conv: conv_did) -> ty::TypeParameterDef {
545+
fn parse_type_param_def(st: &mut PState, conv: conv_did) -> ty::TypeParameterDef {
549546
ty::TypeParameterDef {def_id: parse_def(st, NominalType, conv),
550547
bounds: @parse_bounds(st, conv)}
551548
}
552549

553-
fn parse_bounds(st: @mut PState, conv: conv_did) -> ty::ParamBounds {
550+
fn parse_bounds(st: &mut PState, conv: conv_did) -> ty::ParamBounds {
554551
let mut param_bounds = ty::ParamBounds {
555552
builtin_bounds: ty::EmptyBuiltinBounds(),
556553
trait_bounds: ~[]

0 commit comments

Comments
 (0)