No need to Arc reds, they are rooted anyways

This commit is contained in:
Aleksey Kladov 2018-07-30 02:39:26 +03:00
parent 4e79073e38
commit 83acbc06bd
2 changed files with 13 additions and 8 deletions

View File

@ -1,4 +1,7 @@
use std::sync::{Arc, RwLock}; use std::{
ptr,
sync::RwLock,
};
use { use {
TextUnit, TextUnit,
yellow::GreenNode, yellow::GreenNode,
@ -8,7 +11,7 @@ use {
pub(crate) struct RedNode { pub(crate) struct RedNode {
green: GreenNode, green: GreenNode,
parent: Option<ParentData>, parent: Option<ParentData>,
children: RwLock<Vec<Option<Arc<RedNode>>>>, children: RwLock<Vec<Option<Box<RedNode>>>>,
} }
#[derive(Debug)] #[derive(Debug)]
@ -43,7 +46,8 @@ impl RedNode {
green: GreenNode, green: GreenNode,
parent: Option<ParentData>, parent: Option<ParentData>,
) -> RedNode { ) -> RedNode {
let children = vec![None; green.children().len()]; let n_children = green.children().len();
let children = (0..n_children).map(|_| None).collect();
RedNode { green, parent, children: RwLock::new(children) } RedNode { green, parent, children: RwLock::new(children) }
} }
@ -62,9 +66,9 @@ impl RedNode {
self.green.children().len() self.green.children().len()
} }
pub(crate) fn nth_child(&self, idx: usize) -> Arc<RedNode> { pub(crate) fn nth_child(&self, idx: usize) -> ptr::NonNull<RedNode> {
match &self.children.read().unwrap()[idx] { match &self.children.read().unwrap()[idx] {
Some(child) => return child.clone(), Some(child) => return ptr::NonNull::from(&**child),
None => (), None => (),
} }
let mut children = self.children.write().unwrap(); let mut children = self.children.write().unwrap();
@ -73,8 +77,9 @@ impl RedNode {
let start_offset = self.start_offset() let start_offset = self.start_offset()
+ green_children[..idx].iter().map(|x| x.text_len()).sum::<TextUnit>(); + green_children[..idx].iter().map(|x| x.text_len()).sum::<TextUnit>();
let child = RedNode::new_child(green_children[idx].clone(), self, start_offset, idx); let child = RedNode::new_child(green_children[idx].clone(), self, start_offset, idx);
children[idx] = Some(Arc::new(child)) children[idx] = Some(Box::new(child))
} }
children[idx].as_ref().unwrap().clone() let child = children[idx].as_ref().unwrap();
ptr::NonNull::from(&**child)
} }
} }

View File

@ -60,7 +60,7 @@ impl SyntaxNode {
for i in 0..n_children { for i in 0..n_children {
res.push(SyntaxNode { res.push(SyntaxNode {
root: self.root.clone(), root: self.root.clone(),
red: (&*red.nth_child(i)).into(), red: red.nth_child(i),
}); });
} }
res res