diff options
| author | mojave2 <chenchen145@huawei.com> | 2023-09-04 20:07:28 +0800 |
|---|---|---|
| committer | mojave2 <chenchen145@huawei.com> | 2023-09-04 20:07:28 +0800 |
| commit | f404990eb058819e0842a69a60f733a1de56fb66 (patch) | |
| tree | 35e457b54cd80527e32989cca41e020ebdf0e490 | |
| parent | abfc6c44381fb033c6b3b0a6bfb804a799f39afd (diff) | |
| download | rust-f404990eb058819e0842a69a60f733a1de56fb66.tar.gz rust-f404990eb058819e0842a69a60f733a1de56fb66.zip | |
improve `AttrTokenStream`
| -rw-r--r-- | compiler/rustc_ast/src/tokenstream.rs | 18 |
1 files changed, 7 insertions, 11 deletions
diff --git a/compiler/rustc_ast/src/tokenstream.rs b/compiler/rustc_ast/src/tokenstream.rs index e9591c7c8db..1e18b1232de 100644 --- a/compiler/rustc_ast/src/tokenstream.rs +++ b/compiler/rustc_ast/src/tokenstream.rs @@ -213,14 +213,10 @@ impl AttrTokenStream { .into_iter() } AttrTokenTree::Attributes(data) => { - let mut outer_attrs = Vec::new(); - let mut inner_attrs = Vec::new(); - for attr in &data.attrs { - match attr.style { - crate::AttrStyle::Outer => outer_attrs.push(attr), - crate::AttrStyle::Inner => inner_attrs.push(attr), - } - } + let idx = data + .attrs + .partition_point(|attr| matches!(attr.style, crate::AttrStyle::Outer)); + let (outer_attrs, inner_attrs) = data.attrs.split_at(idx); let mut target_tokens: Vec<_> = data .tokens @@ -265,10 +261,10 @@ impl AttrTokenStream { "Failed to find trailing delimited group in: {target_tokens:?}" ); } - let mut flat: SmallVec<[_; 1]> = SmallVec::new(); + let mut flat: SmallVec<[_; 1]> = + SmallVec::with_capacity(target_tokens.len() + outer_attrs.len()); for attr in outer_attrs { - // FIXME: Make this more efficient - flat.extend(attr.tokens().0.clone().iter().cloned()); + flat.extend(attr.tokens().0.iter().cloned()); } flat.extend(target_tokens); flat.into_iter() |
