Skip to content

Commit a1ec22f

Browse files
authored
Rollup merge of rust-lang#58001 - pnkfelix:issue-57735-proc-macro-with-large-tokenstream-slow, r=eddyb
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. Fix rust-lang#57735
2 parents b2c6b8c + 1a18336 commit a1ec22f

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)