Skip to content

Commit

Permalink
proc_macro: make TokenStream::from_streams pre-allocate its vector.
Browse files Browse the repository at this point in the history
This requires a pre-pass over the input streams. But that is cheap
compared to the quadratic blowup associated with reallocating the
accumulating vector on-the-fly.
  • Loading branch information
pnkfelix committed Jan 30, 2019
1 parent 5f60208 commit 1a18336
Showing 1 changed file with 7 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/libsyntax/tokenstream.rs
Expand Up @@ -255,7 +255,13 @@ impl TokenStream {
0 => TokenStream::empty(),
1 => streams.pop().unwrap(),
_ => {
let mut vec = vec![];
// rust-lang/rust#57735: pre-allocate vector to avoid
// quadratic blow-up due to on-the-fly reallocations.
let tree_count = streams.iter()
.map(|ts| match &ts.0 { None => 0, Some(s) => s.len() })
.sum();
let mut vec = Vec::with_capacity(tree_count);

for stream in streams {
match stream.0 {
None => {},
Expand Down

0 comments on commit 1a18336

Please sign in to comment.