diff --git a/benches/README.md b/benches/README.md index f4a5aa1..b15a91c 100644 --- a/benches/README.md +++ b/benches/README.md @@ -15,14 +15,13 @@ Memory: 15.9 GB # Result ``` -convert() time: [70.877 ms 71.319 ms 71.799 ms] - change: [-1.7418% -0.7475% +0.1900%] (p = 0.13 > 0.05) +convert() time: [68.311 ms 68.743 ms 69.198 ms] + change: [-1.0805% +0.1103% +1.2999%] (p = 0.86 > 0.05) No change in performance detected. -Found 8 outliers among 100 measurements (8.00%) - 7 (7.00%) high mild - 1 (1.00%) high severe +Found 1 outliers among 100 measurements (1.00%) + 1 (1.00%) high mild ``` -*Updated at Tue, 25 Jun 2024 08:27:01 GMT* +*Updated at Wed, 26 Jun 2024 12:45:36 GMT* *Generated by [bench.ts](bench.ts)* diff --git a/src/dom_walker.rs b/src/dom_walker.rs index 62c3132..6eaf52c 100644 --- a/src/dom_walker.rs +++ b/src/dom_walker.rs @@ -42,16 +42,13 @@ pub(crate) fn walk_node( ref attrs, .. } => { - let attrs_value = attrs.take(); - // Put it back - attrs.replace(attrs_value.clone()); visit_element( buffer, node, handler, options, - name.local.to_string(), - attrs_value, + &name.local, + &attrs.borrow(), is_pre, ); } @@ -110,8 +107,8 @@ fn visit_element( node: &Rc, handler: &Box<&dyn ElementHandler>, options: &Options, - tag: String, - attrs: Vec, + tag: &str, + attrs: &Vec, is_pre: bool, ) { let is_head = tag == "head"; @@ -120,10 +117,10 @@ fn visit_element( let is_block = is_block_element(&tag); walk_children(buffer, node, is_block, handler, options, is_pre); let md = handler.on_visit( - node.clone(), + node, tag, attrs, - join_contents(&buffer[prev_buffer_len..]), + &join_contents(&buffer[prev_buffer_len..]), options, ); // Remove the temporary text clips of children @@ -150,10 +147,8 @@ fn join_contents(contents: &[String]) -> String { let left = result.trim_end_matches(|ch| ch == '\n'); let right = content.trim_start_matches(|ch| ch == '\n'); - let max_trimmed_new_lines = std::cmp::max( - result_len - left.len(), - content_len - right.len(), - ); + let max_trimmed_new_lines = + std::cmp::max(result_len - left.len(), content_len - right.len()); let separator_new_lines = std::cmp::min(max_trimmed_new_lines, 2); let separator = "\n".repeat(separator_new_lines); diff --git a/src/element_handler/anchor.rs b/src/element_handler/anchor.rs index add6309..27a67ce 100644 --- a/src/element_handler/anchor.rs +++ b/src/element_handler/anchor.rs @@ -33,10 +33,10 @@ impl ElementHandler for AnchorElementHandler { fn on_visit( &self, - _node: Rc, - _tag: String, - attrs: Vec, - content: String, + _node: &Rc, + _tag: &str, + attrs: &Vec, + content: &str, options: &Options, ) -> Option { let mut link: Option = None; @@ -51,7 +51,7 @@ impl ElementHandler for AnchorElementHandler { } let Some(link) = link else { - return Some(content); + return Some(content.to_string()); }; let process_title = |text: String| { @@ -81,7 +81,7 @@ impl AnchorElementHandler { Self {} } - fn build_inlined_anchor(&self, content: String, link: String, title: Option) -> String { + fn build_inlined_anchor(&self, content: &str, link: String, title: Option) -> String { let has_spaces_in_link = link.contains(' '); let (content, leading_whitespace) = content.strip_leading_whitespace(); let (content, trailing_whitespace) = content.strip_trailing_whitespace(); @@ -103,7 +103,7 @@ impl AnchorElementHandler { fn build_referenced_anchor( &self, - content: String, + content: &str, link: String, style: &LinkReferenceStyle, ) -> String { diff --git a/src/element_handler/mod.rs b/src/element_handler/mod.rs index b5f89d8..0186229 100644 --- a/src/element_handler/mod.rs +++ b/src/element_handler/mod.rs @@ -34,10 +34,10 @@ pub trait ElementHandler { fn on_visit( &self, - node: Rc, - tag: String, - attrs: Vec, - content: String, + node: &Rc, + tag: &str, + attrs: &Vec, + content: &str, options: &Options, ) -> Option; } @@ -57,10 +57,10 @@ where { fn on_visit( &self, - node: Rc, - tag: String, - attrs: Vec, - content: String, + node: &Rc, + tag: &str, + attrs: &Vec, + content: &str, options: &Options, ) -> Option { self(Element { @@ -143,20 +143,15 @@ impl ElementHandlers { impl ElementHandler for ElementHandlers { fn on_visit( &self, - node: Rc, - tag: String, - attrs: Vec, - content: String, + node: &Rc, + tag: &str, + attrs: &Vec, + content: &str, options: &Options, ) -> Option { - match self - .rules - .iter() - .rev() - .find(|rule| rule.tags.contains(&tag)) - { + match self.rules.iter().rev().find(|rule| rule.tags.contains(tag)) { Some(rule) => rule.handler.on_visit(node, tag, attrs, content, options), - None => Some(content), + None => Some(content.to_string()), } } } diff --git a/src/lib.rs b/src/lib.rs index 424a111..92639cf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -30,13 +30,13 @@ pub fn convert(html: &str) -> Result { /// The DOM element. pub struct Element<'a> { /// The html5ever node of the element. - pub node: Rc, + pub node: &'a Rc, /// The tag name. - pub tag: String, + pub tag: &'a str, /// The attribute list. - pub attrs: Vec, + pub attrs: &'a Vec, /// The content text, can be raw text or converted Markdown text. - pub content: String, + pub content: &'a str, /// Converter options. pub options: &'a Options, }