mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 13:25:09 +00:00
Auto merge of #16482 - GnomedDev:boxed-subtree, r=lnicola
Swap Subtree::token_trees from Vec to boxed slice Performs one of the optimizations suggested in #16325, but a little bit more. Boxed slices guarantee `shrink_to_fit` aswell as saving a pointer width as no capacity has to be stored. Most of the diff is: - Changing `vec![]` to `Box::new([])` - Changing initialize -> fill into fill -> into_boxed_slice - Working around the lack of an owned iterator or automatic iteration over a `Box<[T]>` I would like to use my own crate, [small-fixed-array](https://lib.rs/small-fixed-array), although I understand if it isn't mature enough for this. If I'm given the go ahead, I can rework this PR to use it instead.
This commit is contained in:
commit
7fb639ffc1
22 changed files with 201 additions and 158 deletions
|
@ -23,10 +23,10 @@ pub enum TokenTree<S> {
|
|||
}
|
||||
impl_from!(Leaf<S>, Subtree<S> for TokenTree);
|
||||
impl<S: Span> TokenTree<S> {
|
||||
pub const fn empty(span: S) -> Self {
|
||||
pub fn empty(span: S) -> Self {
|
||||
Self::Subtree(Subtree {
|
||||
delimiter: Delimiter::invisible_spanned(span),
|
||||
token_trees: vec![],
|
||||
token_trees: Box::new([]),
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -34,7 +34,7 @@ impl<S: Span> TokenTree<S> {
|
|||
match self {
|
||||
TokenTree::Leaf(_) => Subtree {
|
||||
delimiter: Delimiter::invisible_delim_spanned(span),
|
||||
token_trees: vec![self],
|
||||
token_trees: Box::new([self]),
|
||||
},
|
||||
TokenTree::Subtree(s) => s,
|
||||
}
|
||||
|
@ -69,12 +69,35 @@ impl_from!(Literal<S>, Punct<S>, Ident<S> for Leaf);
|
|||
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||
pub struct Subtree<S> {
|
||||
pub delimiter: Delimiter<S>,
|
||||
pub token_trees: Vec<TokenTree<S>>,
|
||||
pub token_trees: Box<[TokenTree<S>]>,
|
||||
}
|
||||
|
||||
impl<S: Span> Subtree<S> {
|
||||
pub const fn empty(span: DelimSpan<S>) -> Self {
|
||||
Subtree { delimiter: Delimiter::invisible_delim_spanned(span), token_trees: vec![] }
|
||||
pub fn empty(span: DelimSpan<S>) -> Self {
|
||||
Subtree { delimiter: Delimiter::invisible_delim_spanned(span), token_trees: Box::new([]) }
|
||||
}
|
||||
|
||||
/// This is slow, and should be avoided, as it will always reallocate!
|
||||
pub fn push(&mut self, subtree: TokenTree<S>) {
|
||||
let mut mutable_trees = std::mem::take(&mut self.token_trees).into_vec();
|
||||
|
||||
// Reserve exactly space for one element, to avoid `into_boxed_slice` having to reallocate again.
|
||||
mutable_trees.reserve_exact(1);
|
||||
mutable_trees.push(subtree);
|
||||
|
||||
self.token_trees = mutable_trees.into_boxed_slice();
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, PartialEq, Eq, Hash)]
|
||||
pub struct SubtreeBuilder<S> {
|
||||
pub delimiter: Delimiter<S>,
|
||||
pub token_trees: Vec<TokenTree<S>>,
|
||||
}
|
||||
|
||||
impl<S> SubtreeBuilder<S> {
|
||||
pub fn build(self) -> Subtree<S> {
|
||||
Subtree { delimiter: self.delimiter, token_trees: self.token_trees.into_boxed_slice() }
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -228,7 +251,7 @@ impl<S> fmt::Display for Subtree<S> {
|
|||
};
|
||||
f.write_str(l)?;
|
||||
let mut needs_space = false;
|
||||
for tt in &self.token_trees {
|
||||
for tt in self.token_trees.iter() {
|
||||
if needs_space {
|
||||
f.write_str(" ")?;
|
||||
}
|
||||
|
@ -303,7 +326,7 @@ impl<S> Subtree<S> {
|
|||
let mut res = String::new();
|
||||
res.push_str(delim.0);
|
||||
let mut last = None;
|
||||
for child in &self.token_trees {
|
||||
for child in self.token_trees.iter() {
|
||||
let s = match child {
|
||||
TokenTree::Leaf(it) => {
|
||||
let s = match it {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue