Skip to content

Commit 1a18336

Browse files
committedJan 30, 2019
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.
1 parent 5f60208 commit 1a18336

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed
 

‎src/libsyntax/tokenstream.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,13 @@ impl TokenStream {
255255
0 => TokenStream::empty(),
256256
1 => streams.pop().unwrap(),
257257
_ => {
258-
let mut vec = vec![];
258+
// rust-lang/rust#57735: pre-allocate vector to avoid
259+
// quadratic blow-up due to on-the-fly reallocations.
260+
let tree_count = streams.iter()
261+
.map(|ts| match &ts.0 { None => 0, Some(s) => s.len() })
262+
.sum();
263+
let mut vec = Vec::with_capacity(tree_count);
264+
259265
for stream in streams {
260266
match stream.0 {
261267
None => {},

0 commit comments

Comments
 (0)