From 1a183368082ad357b1fef0f55038becc9ac14b7b Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Wed, 30 Jan 2019 15:12:41 +0100 Subject: [PATCH] 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. --- src/libsyntax/tokenstream.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/libsyntax/tokenstream.rs b/src/libsyntax/tokenstream.rs index f5d2d6f18ee87..7d6ffceb2c0d4 100644 --- a/src/libsyntax/tokenstream.rs +++ b/src/libsyntax/tokenstream.rs @@ -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 => {},