From 8628c1fc35d0da6e2d92d5824cf63a85b1ee8c57 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Wed, 1 Jul 2020 17:13:31 -0400 Subject: [PATCH] Box most NodeData variants This reduces memory usage in NodeData by a factor of 5 (80 -> 16), and usage in Node by a factor of 2 (120 -> 56). --- src/lib.rs | 2 ++ src/tree.rs | 32 ++++++++++++++++---------------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 050226a8c..f0eed0563 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,6 +5,8 @@ Kuchiki (朽木), a HTML/XML tree manipulation library for Rust. */ #![deny(missing_docs)] +#![warn(variant_size_differences)] +#![warn(clippy::large_enum_variant)] #[macro_use] extern crate html5ever; diff --git a/src/tree.rs b/src/tree.rs index 49f09cf39..6a85f7c51 100644 --- a/src/tree.rs +++ b/src/tree.rs @@ -13,22 +13,22 @@ use crate::iter::NodeIterator; #[derive(Debug, PartialEq, Clone)] pub enum NodeData { /// Element node - Element(ElementData), + Element(Box), /// Text node - Text(RefCell), + Text(Box>), /// Comment node - Comment(RefCell), + Comment(Box>), /// Processing instruction node - ProcessingInstruction(RefCell<(String, String)>), + ProcessingInstruction(Box>), /// Doctype node - Doctype(Doctype), + Doctype(Box), /// Document node - Document(DocumentData), + Document(Box), /// Document fragment node DocumentFragment, @@ -212,7 +212,7 @@ impl NodeRef { where I: IntoIterator, { - NodeRef::new(NodeData::Element(ElementData { + NodeRef::new(NodeData::Element(Box::new(ElementData { template_contents: if name.expanded() == expanded_name!(html "template") { Some(NodeRef::new(NodeData::DocumentFragment)) } else { @@ -222,19 +222,19 @@ impl NodeRef { attributes: RefCell::new(Attributes { map: attributes.into_iter().collect(), }), - })) + }))) } /// Create a new text node. #[inline] pub fn new_text>(value: T) -> NodeRef { - NodeRef::new(NodeData::Text(RefCell::new(value.into()))) + NodeRef::new(NodeData::Text(Box::new(RefCell::new(value.into())))) } /// Create a new comment node. #[inline] pub fn new_comment>(value: T) -> NodeRef { - NodeRef::new(NodeData::Comment(RefCell::new(value.into()))) + NodeRef::new(NodeData::Comment(Box::new(RefCell::new(value.into())))) } /// Create a new processing instruction node. @@ -244,10 +244,10 @@ impl NodeRef { T1: Into, T2: Into, { - NodeRef::new(NodeData::ProcessingInstruction(RefCell::new(( + NodeRef::new(NodeData::ProcessingInstruction(Box::new(RefCell::new(( target.into(), data.into(), - )))) + ))))) } /// Create a new doctype node. @@ -258,19 +258,19 @@ impl NodeRef { T2: Into, T3: Into, { - NodeRef::new(NodeData::Doctype(Doctype { + NodeRef::new(NodeData::Doctype(Box::new(Doctype { name: name.into(), public_id: public_id.into(), system_id: system_id.into(), - })) + }))) } /// Create a new document node. #[inline] pub fn new_document() -> NodeRef { - NodeRef::new(NodeData::Document(DocumentData { + NodeRef::new(NodeData::Document(Box::new(DocumentData { _quirks_mode: Cell::new(QuirksMode::NoQuirks), - })) + }))) } /// Return the concatenation of all text nodes in this subtree.