gioser

tawasuyu/taffy

A high performance rust-powered UI layout library

Rust 497HTML 254C 19JavaScript 81329 Definiciones

Minor optimizations in node allocation

caa0ce8629f858d791af8d50bd47d9d30d7aefc3 · por Emil Sjölander · (hace 7 años) · padre a26a7d4db · ver el árbol en este commit

Delta semántico · 3

src/node.rs

1 archivos · +12 −8

diff --git a/src/node.rs b/src/node.rs
index 645c1f677..4afd58a9e 100644
--- a/src/node.rs
+++ b/src/node.rs
@@ -28,24 +28,25 @@ impl Node {
     pub fn new_leaf(style: Style, measure: Option<MeasureFunc>) -> Node {
         Node(Rc::new(RefCell::new(InternalNode {
             style,
-            parents: vec![],
-            children: vec![],
+            parents: Vec::with_capacity(1),
+            children: Vec::with_capacity(0),
             measure,
             layout_cache: RefCell::new(None),
         })))
     }
 
     pub fn new(style: Style, children: Vec<&Node>) -> Node {
-        let mut parent = Node(Rc::new(RefCell::new(InternalNode {
+        let parent = Node(Rc::new(RefCell::new(InternalNode {
             style,
-            parents: vec![],
-            children: vec![],
+            parents: Vec::with_capacity(1),
+            children: Vec::with_capacity(children.len()),
             measure: None,
             layout_cache: RefCell::new(None),
         })));
 
         for child in children {
-            parent.add_child(child);
+            child.0.borrow_mut().parents.push(Rc::downgrade(&parent.0));
+            parent.0.borrow_mut().children.push(Rc::clone(&child.0));
         }
 
         parent
@@ -64,11 +65,14 @@ impl Node {
             child.borrow_mut().parents.remove(position);
         }
 
-        self.0.borrow_mut().children = vec![];
+        self.0.borrow_mut().children = Vec::with_capacity(children.len());
 
         for child in children {
-            self.add_child(child);
+            child.0.borrow_mut().parents.push(Rc::downgrade(&self.0));
+            self.0.borrow_mut().children.push(Rc::clone(&child.0));
         }
+
+        self.mark_dirty();
     }
 
     pub fn remove_child(&mut self, child: &Node) -> Node {