proc_macro: make TokenStream::from_streams
pre-allocate its vector.
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.
This commit is contained in:
parent
5f60208ba1
commit
1a18336808
1 changed files with 7 additions and 1 deletions
|
@ -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 => {},
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue