@@ -24,6 +24,7 @@ use syntax_pos::{BytePos, Mark, Span, DUMMY_SP};
24
24
use rustc_data_structures:: static_assert;
25
25
use rustc_data_structures:: sync:: Lrc ;
26
26
use serialize:: { Decoder , Decodable , Encoder , Encodable } ;
27
+ use smallvec:: { SmallVec , smallvec} ;
27
28
28
29
use std:: borrow:: Cow ;
29
30
use std:: { fmt, iter, mem} ;
@@ -224,7 +225,7 @@ impl From<Token> for TokenStream {
224
225
225
226
impl < T : Into < TokenStream > > iter:: FromIterator < T > for TokenStream {
226
227
fn from_iter < I : IntoIterator < Item = T > > ( iter : I ) -> Self {
227
- TokenStream :: from_streams ( iter. into_iter ( ) . map ( Into :: into) . collect :: < Vec < _ > > ( ) )
228
+ TokenStream :: from_streams ( iter. into_iter ( ) . map ( Into :: into) . collect :: < SmallVec < _ > > ( ) )
228
229
}
229
230
}
230
231
@@ -256,7 +257,7 @@ impl TokenStream {
256
257
}
257
258
}
258
259
259
- pub ( crate ) fn from_streams ( mut streams : Vec < TokenStream > ) -> TokenStream {
260
+ pub ( crate ) fn from_streams ( mut streams : SmallVec < [ TokenStream ; 2 ] > ) -> TokenStream {
260
261
match streams. len ( ) {
261
262
0 => TokenStream :: empty ( ) ,
262
263
1 => streams. pop ( ) . unwrap ( ) ,
@@ -393,12 +394,13 @@ impl TokenStream {
393
394
}
394
395
}
395
396
397
+ // 99.5%+ of the time we have 1 or 2 elements in this vector.
396
398
#[ derive( Clone ) ]
397
- pub struct TokenStreamBuilder ( Vec < TokenStream > ) ;
399
+ pub struct TokenStreamBuilder ( SmallVec < [ TokenStream ; 2 ] > ) ;
398
400
399
401
impl TokenStreamBuilder {
400
402
pub fn new ( ) -> TokenStreamBuilder {
401
- TokenStreamBuilder ( Vec :: new ( ) )
403
+ TokenStreamBuilder ( SmallVec :: new ( ) )
402
404
}
403
405
404
406
pub fn push < T : Into < TokenStream > > ( & mut self , stream : T ) {
@@ -485,7 +487,7 @@ impl Cursor {
485
487
}
486
488
let index = self . index ;
487
489
let stream = mem:: replace ( & mut self . stream , TokenStream ( None ) ) ;
488
- * self = TokenStream :: from_streams ( vec ! [ stream, new_stream] ) . into_trees ( ) ;
490
+ * self = TokenStream :: from_streams ( smallvec ! [ stream, new_stream] ) . into_trees ( ) ;
489
491
self . index = index;
490
492
}
491
493
@@ -572,7 +574,7 @@ mod tests {
572
574
let test_res = string_to_ts ( "foo::bar::baz" ) ;
573
575
let test_fst = string_to_ts ( "foo::bar" ) ;
574
576
let test_snd = string_to_ts ( "::baz" ) ;
575
- let eq_res = TokenStream :: from_streams ( vec ! [ test_fst, test_snd] ) ;
577
+ let eq_res = TokenStream :: from_streams ( smallvec ! [ test_fst, test_snd] ) ;
576
578
assert_eq ! ( test_res. trees( ) . count( ) , 5 ) ;
577
579
assert_eq ! ( eq_res. trees( ) . count( ) , 5 ) ;
578
580
assert_eq ! ( test_res. eq_unspanned( & eq_res) , true ) ;
0 commit comments