on master help sign in
Shrink the size of `NodeKind` by putting `ElementNode`s behind a ref
bd2c84cNick Fitzgerald
src/diff.rs+1−1
@@ -925,7 +925,7 @@ fn create(
925 NodeKind::Text(TextNode { text }) => {
925 NodeKind::Text(TextNode { text }) => {
926 change_list.create_text_node(text);
926 change_list.create_text_node(text);
927 }
927 }
928− NodeKind::Element(ElementNode {
928+ NodeKind::Element(&ElementNode {
929 key: _,
929 key: _,
930 tag_name,
930 tag_name,
931 listeners,
931 listeners,
src/events.rs+8−13
@@ -19,7 +19,6 @@ cfg_if::cfg_if! {
19 pub(crate) fn clear_active_listeners(&mut self) {}
19 pub(crate) fn clear_active_listeners(&mut self) {}
20 }
20 }
21 } else {
21 } else {
22− use bumpalo::Bump;
23 use crate::{
22 use crate::{
24 node::{ElementNode, ListenerCallback, NodeKind},
23 node::{ElementNode, ListenerCallback, NodeKind},
25 vdom::VdomWeak,
24 vdom::VdomWeak,
@@ -99,18 +98,14 @@ cfg_if::cfg_if! {
99 }
98 }
100
99
101 pub(crate) fn remove_subtree(&mut self, node: &Node) {
100 pub(crate) fn remove_subtree(&mut self, node: &Node) {
102− let bump = Bump::new();
103− let mut stack = bumpalo::collections::Vec::with_capacity_in(64, &bump);
104− stack.push(node);
105−
106− while let Some(node) = stack.pop() {
107− match node.kind {
101+ match node.kind {
108− NodeKind::Cached(_) | NodeKind::Text(_) => continue,
102+ NodeKind::Cached(_) | NodeKind::Text(_) => return,
109− NodeKind::Element(ElementNode {listeners, children, ..}) => {
103+ NodeKind::Element(&ElementNode {listeners, children, ..}) => {
110− for l in listeners {
104+ for l in listeners {
111− self.remove(l);
105+ self.remove(l);
112− }
106+ }
113− stack.extend(children);
107+ for child in children {
108+ self.remove_subtree(child)
114 }
109 }
115 }
110 }
116 }
111 }
src/node.rs+11−9
@@ -24,7 +24,7 @@ pub_unstable_internal! {
24 Text(TextNode<'a>),
24 Text(TextNode<'a>),
25
25
26 /// An element potentially with attributes and children.
26 /// An element potentially with attributes and children.
27− Element(ElementNode<'a>),
27+ Element(&'a ElementNode<'a>),
28
28
29 /// A node in the vdom's `CachedSet`. This allows us to avoid
29 /// A node in the vdom's `CachedSet`. This allows us to avoid
30 /// re-rendering and re-diffing subtrees.
30 /// re-rendering and re-diffing subtrees.
@@ -187,15 +187,17 @@ impl<'a> Node<'a> {
187 let attributes: &'a Attributes = bump.alloc(attributes);
187 let attributes: &'a Attributes = bump.alloc(attributes);
188 let attributes: &'a [Attribute<'a>] = attributes.as_ref();
188 let attributes: &'a [Attribute<'a>] = attributes.as_ref();
189
189
190+ let element = bump.alloc_with(|| ElementNode {
191+ key,
192+ tag_name,
193+ listeners,
194+ attributes,
195+ children,
196+ namespace,
197+ });
198+
190 Node {
199 Node {
191− kind: NodeKind::Element(ElementNode {
200+ kind: NodeKind::Element(element),
192− key,
193− tag_name,
194− listeners,
195− attributes,
196− children,
197− namespace,
198− }),
199 }
201 }
200 }
202 }
201
203
tests/web/main.rs+1−1
@@ -89,7 +89,7 @@ pub fn assert_rendered<R: Render>(container: &web_sys::Element, r: &R) {
89 "actual.text_content() == expected.text()"
89 "actual.text_content() == expected.text()"
90 );
90 );
91 }
91 }
92− NodeKind::Element(ElementNode {
92+ NodeKind::Element(&ElementNode {
93 tag_name,
93 tag_name,
94 attributes,
94 attributes,
95 children,
95 children,