@@ -17,6 +17,7 @@ use ty::fold::{TypeFoldable, TypeFolder, TypeVisitor};
17
17
use serialize:: { self , Encodable , Encoder , Decodable , Decoder } ;
18
18
use syntax_pos:: { Span , DUMMY_SP } ;
19
19
use rustc_data_structures:: accumulate_vec:: AccumulateVec ;
20
+ use rustc_data_structures:: array_vec:: ArrayVec ;
20
21
21
22
use core:: intrinsics;
22
23
use std:: fmt;
@@ -176,7 +177,12 @@ impl<'a, 'gcx, 'tcx> Substs<'tcx> {
176
177
where F : FnMut ( & ty:: GenericParamDef , & [ Kind < ' tcx > ] ) -> Kind < ' tcx >
177
178
{
178
179
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
+ } ;
180
186
Substs :: fill_item ( & mut substs, tcx, defs, & mut mk_kind) ;
181
187
tcx. intern_substs ( & substs)
182
188
}
@@ -188,14 +194,15 @@ impl<'a, 'gcx, 'tcx> Substs<'tcx> {
188
194
-> & ' tcx Substs < ' tcx >
189
195
where F : FnMut ( & ty:: GenericParamDef , & [ Kind < ' tcx > ] ) -> Kind < ' tcx >
190
196
{
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
+ } )
196
203
}
197
204
198
- fn fill_item < F > ( substs : & mut Vec < Kind < ' tcx > > ,
205
+ fn fill_item < F > ( substs : & mut AccumulateVec < [ Kind < ' tcx > ; 8 ] > ,
199
206
tcx : TyCtxt < ' a , ' gcx , ' tcx > ,
200
207
defs : & ty:: Generics ,
201
208
mk_kind : & mut F )
@@ -209,15 +216,18 @@ impl<'a, 'gcx, 'tcx> Substs<'tcx> {
209
216
Substs :: fill_single ( substs, defs, mk_kind)
210
217
}
211
218
212
- fn fill_single < F > ( substs : & mut Vec < Kind < ' tcx > > ,
219
+ fn fill_single < F > ( substs : & mut AccumulateVec < [ Kind < ' tcx > ; 8 ] > ,
213
220
defs : & ty:: Generics ,
214
221
mk_kind : & mut F )
215
222
where F : FnMut ( & ty:: GenericParamDef , & [ Kind < ' tcx > ] ) -> Kind < ' tcx >
216
223
{
217
224
for param in & defs. params {
218
225
let kind = mk_kind ( param, substs) ;
219
226
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
+ }
221
231
}
222
232
}
223
233
0 commit comments