Skip to content

Commit ba2c5c5

Browse files
committed
rustc: use AccumulateVec in Substs::for_item.
1 parent dce288e commit ba2c5c5

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

src/librustc/ty/subst.rs

+19-9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
1717
use serialize::{self, Encodable, Encoder, Decodable, Decoder};
1818
use syntax_pos::{Span, DUMMY_SP};
1919
use rustc_data_structures::accumulate_vec::AccumulateVec;
20+
use rustc_data_structures::array_vec::ArrayVec;
2021

2122
use core::intrinsics;
2223
use std::fmt;
@@ -176,7 +177,12 @@ impl<'a, 'gcx, 'tcx> Substs<'tcx> {
176177
where F: FnMut(&ty::GenericParamDef, &[Kind<'tcx>]) -> Kind<'tcx>
177178
{
178179
let defs = tcx.generics_of(def_id);
179-
let mut substs = Vec::with_capacity(defs.count());
180+
let count = defs.count();
181+
let mut substs = if count <= 8 {
182+
AccumulateVec::Array(ArrayVec::new())
183+
} else {
184+
AccumulateVec::Heap(Vec::with_capacity(count))
185+
};
180186
Substs::fill_item(&mut substs, tcx, defs, &mut mk_kind);
181187
tcx.intern_substs(&substs)
182188
}
@@ -188,14 +194,15 @@ impl<'a, 'gcx, 'tcx> Substs<'tcx> {
188194
-> &'tcx Substs<'tcx>
189195
where F: FnMut(&ty::GenericParamDef, &[Kind<'tcx>]) -> Kind<'tcx>
190196
{
191-
let defs = tcx.generics_of(def_id);
192-
let mut result = Vec::with_capacity(defs.count());
193-
result.extend(self[..].iter().cloned());
194-
Substs::fill_single(&mut result, defs, &mut mk_kind);
195-
tcx.intern_substs(&result)
197+
Substs::for_item(tcx, def_id, |param, substs| {
198+
match self.get(param.index as usize) {
199+
Some(&kind) => kind,
200+
None => mk_kind(param, substs),
201+
}
202+
})
196203
}
197204

198-
fn fill_item<F>(substs: &mut Vec<Kind<'tcx>>,
205+
fn fill_item<F>(substs: &mut AccumulateVec<[Kind<'tcx>; 8]>,
199206
tcx: TyCtxt<'a, 'gcx, 'tcx>,
200207
defs: &ty::Generics,
201208
mk_kind: &mut F)
@@ -209,15 +216,18 @@ impl<'a, 'gcx, 'tcx> Substs<'tcx> {
209216
Substs::fill_single(substs, defs, mk_kind)
210217
}
211218

212-
fn fill_single<F>(substs: &mut Vec<Kind<'tcx>>,
219+
fn fill_single<F>(substs: &mut AccumulateVec<[Kind<'tcx>; 8]>,
213220
defs: &ty::Generics,
214221
mk_kind: &mut F)
215222
where F: FnMut(&ty::GenericParamDef, &[Kind<'tcx>]) -> Kind<'tcx>
216223
{
217224
for param in &defs.params {
218225
let kind = mk_kind(param, substs);
219226
assert_eq!(param.index as usize, substs.len());
220-
substs.push(kind);
227+
match *substs {
228+
AccumulateVec::Array(ref mut arr) => arr.push(kind),
229+
AccumulateVec::Heap(ref mut vec) => vec.push(kind),
230+
}
221231
}
222232
}
223233

0 commit comments

Comments
 (0)