From af3d75ad2e760dc885f54e6179543718ef8f141f Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Tue, 5 Jan 2021 00:22:42 +0800 Subject: Refactor TokenBuffer for reduc cloning --- crates/tt/src/buffer.rs | 105 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 76 insertions(+), 29 deletions(-) (limited to 'crates/tt') diff --git a/crates/tt/src/buffer.rs b/crates/tt/src/buffer.rs index 02c771f70..3606c887d 100644 --- a/crates/tt/src/buffer.rs +++ b/crates/tt/src/buffer.rs @@ -1,6 +1,6 @@ //! FIXME: write short doc here -use crate::{Subtree, TokenTree}; +use crate::{Leaf, Subtree, TokenTree}; #[derive(Copy, Clone, Debug, Eq, PartialEq)] struct EntryId(usize); @@ -13,7 +13,7 @@ struct EntryPtr(EntryId, usize); #[derive(Debug)] enum Entry<'t> { // Mimicking types from proc-macro. - Subtree(&'t TokenTree, EntryId), + Subtree(Option<&'t TokenTree>, &'t Subtree, EntryId), Leaf(&'t TokenTree), // End entries contain a pointer to the entry from the containing // token tree, or None if this is the outermost level. @@ -27,37 +27,64 @@ pub struct TokenBuffer<'t> { buffers: Vec]>>, } -impl<'t> TokenBuffer<'t> { - pub fn new(tokens: &'t [TokenTree]) -> TokenBuffer<'t> { - let mut buffers = vec![]; - - let idx = TokenBuffer::new_inner(tokens, &mut buffers, None); - assert_eq!(idx, 0); - - TokenBuffer { buffers } - } +trait TokenList<'a> { + fn entries(&self) -> (Vec<(usize, (&'a Subtree, Option<&'a TokenTree>))>, Vec>); +} - fn new_inner( - tokens: &'t [TokenTree], - buffers: &mut Vec]>>, - next: Option, - ) -> usize { +impl<'a> TokenList<'a> for &'a [TokenTree] { + fn entries(&self) -> (Vec<(usize, (&'a Subtree, Option<&'a TokenTree>))>, Vec>) { // Must contain everything in tokens and then the Entry::End - let start_capacity = tokens.len() + 1; + let start_capacity = self.len() + 1; let mut entries = Vec::with_capacity(start_capacity); let mut children = vec![]; - - for (idx, tt) in tokens.iter().enumerate() { + for (idx, tt) in self.iter().enumerate() { match tt { TokenTree::Leaf(_) => { entries.push(Entry::Leaf(tt)); } TokenTree::Subtree(subtree) => { entries.push(Entry::End(None)); - children.push((idx, (subtree, tt))); + children.push((idx, (subtree, Some(tt)))); } } } + (children, entries) + } +} + +impl<'a> TokenList<'a> for &'a Subtree { + fn entries(&self) -> (Vec<(usize, (&'a Subtree, Option<&'a TokenTree>))>, Vec>) { + // Must contain everything in tokens and then the Entry::End + let mut entries = vec![]; + let mut children = vec![]; + entries.push(Entry::End(None)); + children.push((0usize, (*self, None))); + (children, entries) + } +} + +impl<'t> TokenBuffer<'t> { + pub fn from_tokens(tokens: &'t [TokenTree]) -> TokenBuffer<'t> { + Self::new(tokens) + } + + pub fn from_subtree(subtree: &'t Subtree) -> TokenBuffer<'t> { + Self::new(subtree) + } + + fn new>(tokens: T) -> TokenBuffer<'t> { + let mut buffers = vec![]; + let idx = TokenBuffer::new_inner(tokens, &mut buffers, None); + assert_eq!(idx, 0); + TokenBuffer { buffers } + } + + fn new_inner>( + tokens: T, + buffers: &mut Vec]>>, + next: Option, + ) -> usize { + let (children, mut entries) = tokens.entries(); entries.push(Entry::End(next)); let res = buffers.len(); @@ -65,11 +92,11 @@ impl<'t> TokenBuffer<'t> { for (child_idx, (subtree, tt)) in children { let idx = TokenBuffer::new_inner( - &subtree.token_trees, + subtree.token_trees.as_slice(), buffers, Some(EntryPtr(EntryId(res), child_idx + 1)), ); - buffers[res].as_mut()[child_idx] = Entry::Subtree(tt, EntryId(idx)); + buffers[res].as_mut()[child_idx] = Entry::Subtree(tt, subtree, EntryId(idx)); } res @@ -87,6 +114,24 @@ impl<'t> TokenBuffer<'t> { } } +#[derive(Debug)] +pub enum TokenTreeRef<'a> { + Subtree(&'a Subtree, Option<&'a TokenTree>), + Leaf(&'a Leaf, &'a TokenTree), +} + +impl<'a> TokenTreeRef<'a> { + pub fn cloned(&self) -> TokenTree { + match &self { + TokenTreeRef::Subtree(subtree, tt) => match tt { + Some(it) => (*it).clone(), + None => (*subtree).clone().into(), + }, + TokenTreeRef::Leaf(_, tt) => (*tt).clone(), + } + } +} + /// A safe version of `Cursor` from `syn` crate https://github.com/dtolnay/syn/blob/6533607f91686545cb034d2838beea338d9d0742/src/buffer.rs#L125 #[derive(Copy, Clone, Debug)] pub struct Cursor<'a> { @@ -114,12 +159,11 @@ impl<'a> Cursor<'a> { match self.entry() { Some(Entry::End(Some(ptr))) => { let idx = ptr.1; - if let Some(Entry::Subtree(TokenTree::Subtree(subtree), _)) = + if let Some(Entry::Subtree(_, subtree, _)) = self.buffer.entry(&EntryPtr(ptr.0, idx - 1)) { return Some(subtree); } - None } _ => None, @@ -134,7 +178,7 @@ impl<'a> Cursor<'a> { /// a cursor into that subtree pub fn subtree(self) -> Option> { match self.entry() { - Some(Entry::Subtree(_, entry_id)) => { + Some(Entry::Subtree(_, _, entry_id)) => { Some(Cursor::create(self.buffer, EntryPtr(*entry_id, 0))) } _ => None, @@ -142,10 +186,13 @@ impl<'a> Cursor<'a> { } /// If the cursor is pointing at a `TokenTree`, returns it - pub fn token_tree(self) -> Option<&'a TokenTree> { + pub fn token_tree(self) -> Option> { match self.entry() { - Some(Entry::Leaf(tt)) => Some(tt), - Some(Entry::Subtree(tt, _)) => Some(tt), + Some(Entry::Leaf(tt)) => match tt { + TokenTree::Leaf(leaf) => Some(TokenTreeRef::Leaf(leaf, *tt)), + TokenTree::Subtree(subtree) => Some(TokenTreeRef::Subtree(subtree, Some(tt))), + }, + Some(Entry::Subtree(tt, subtree, _)) => Some(TokenTreeRef::Subtree(subtree, *tt)), Some(Entry::End(_)) => None, None => None, } @@ -172,7 +219,7 @@ impl<'a> Cursor<'a> { /// a cursor into that subtree pub fn bump_subtree(self) -> Cursor<'a> { match self.entry() { - Some(Entry::Subtree(_, _)) => self.subtree().unwrap(), + Some(Entry::Subtree(_, _, _)) => self.subtree().unwrap(), _ => self.bump(), } } -- cgit v1.2.3