Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proc_macro: make TokenStream::from_streams pre-allocate its vector. #58001

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/libsyntax/tokenstream.rs
Original file line number Diff line number Diff line change
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