@@ -17,6 +17,7 @@ use ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
1717use serialize:: { self , Encodable , Encoder , Decodable , Decoder } ;
1818use syntax_pos:: { Span , DUMMY_SP } ;
1919use rustc_data_structures:: accumulate_vec:: AccumulateVec ;
20+ use rustc_data_structures:: array_vec:: ArrayVec ;
2021
2122use core:: intrinsics;
2223use 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