1
Fork 0

Make TokenStream::from_iter less general and more efficient.

The current code has this impl:
```
impl<T: Into<TokenStream>> iter::FromIterator<T> for TokenStream
```
If given an `IntoIterator<Item = TokenTree>`, it will convert each individual
`TokenTree` to a `TokenStream` (at the cost of two allocations: a `Vec`
and an `Lrc`). It will then merge those `TokenStream`s into a single
`TokenStream`. This is inefficient.

This commit changes the impl to this less general one:
```
impl iter::FromIterator<TokenTree> for TokenStream
```
It collects the `TokenTree`s into a single `Vec` first and then converts that
to a `TokenStream` by wrapping it in a single `Lrc`. The previous generality
was unnecessary; no other code needs changing.

This change speeds up several benchmarks by up to 4%.
This commit is contained in:
Nicholas Nethercote 2019-10-14 10:37:21 +11:00
parent d0eaf60d5e
commit a6eef299d3

View file

@ -202,9 +202,9 @@ impl From<TokenTree> for TreeAndJoint {
} }
} }
impl<T: Into<TokenStream>> iter::FromIterator<T> for TokenStream { impl iter::FromIterator<TokenTree> for TokenStream {
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self { fn from_iter<I: IntoIterator<Item = TokenTree>>(iter: I) -> Self {
TokenStream::from_streams(iter.into_iter().map(Into::into).collect::<SmallVec<_>>()) TokenStream::new(iter.into_iter().map(Into::into).collect::<Vec<TreeAndJoint>>())
} }
} }