Skip to content

Commit

Permalink
Improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
tbranyen committed Nov 8, 2022
1 parent 97fd541 commit e5c0f12
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 79 deletions.
70 changes: 14 additions & 56 deletions packages/diffhtml-rust-parser/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions packages/diffhtml-rust-parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ crate-type = ["cdylib"]

[dependencies]
tl = { version = "0.7.7", features = ["simd"] }
serde = { version = "1.0", features = ["derive"] }
serde_derive = "1.0.147"
wee_alloc = "0.4.5"
wasm-bindgen = "0.2.83"
serde = { version = "1.0", features = ["derive"] }
serde-wasm-bindgen = "0.4"
#serde_derive = "1.0.147"
#wee_alloc = "0.4.5"
js-sys = "0.3.60"

[profile.release]
Expand Down
4 changes: 2 additions & 2 deletions packages/diffhtml-rust-parser/Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
default:
cargo build --release
RUSTFLAGS="-C target-feature=+simd128" cargo build --release
mkdir -p dist
wasm-bindgen ./target/wasm32-unknown-unknown/release/parser.wasm --out-dir dist
wasm-bindgen ./target/wasm32-unknown-unknown/release/parser.wasm --out-dir dist
36 changes: 18 additions & 18 deletions packages/diffhtml-rust-parser/src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
extern crate wee_alloc;
//extern crate wee_alloc;

use js_sys::Reflect;
use tl::{Node, Parser};
use serde_wasm_bindgen::to_value;
use wasm_bindgen::prelude::*;
use wasm_bindgen::{intern};
use js_sys::{Reflect, JsString};
use js_sys::Array;
use js_sys::Object;

// Opt for a smaller allocator to save on resources.
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
//#[global_allocator]
//static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

#[derive(Clone, Debug)]
#[wasm_bindgen(getter_with_clone)]
pub struct VTree {
pub rawNodeName: String,
Expand All @@ -29,23 +30,21 @@ pub fn crawl_node(dom_node: &Node, parser: &Parser) -> VTree {
let mut node_name = "#text".to_string();

match node_tag {
Some(htmlTag) => {
// Set the node name
node_name = htmlTag.name().as_utf8_str().to_string();
Some(html_tag) => {
node_name = html_tag.name().as_utf8_str().to_string();

// Add the attributes
for attr in htmlTag.attributes().iter() {
for attr in html_tag.attributes().iter() {
let key = attr.0.chars().as_str();
let mut val = match attr.1 {
let value = match attr.1 {
None => { "" }
Some(ref cowStr) => {
let varChar = cowStr.chars();

varChar.as_str()
Some(ref cow_str) => {
cow_str.chars().as_str()
}
};

Reflect::set(&attributes, &JsValue::from_str(key), &JsValue::from_str(val));
// FIXME Look into performance improvements this is a very expensive line.
Reflect::set(&attributes, &JsValue::from_str(key), &JsValue::from_str(value));
}
}
None => {}
Expand All @@ -54,7 +53,7 @@ pub fn crawl_node(dom_node: &Node, parser: &Parser) -> VTree {
// Loop over the children and build them up into the child_nodes array.
for inner_children in dom_node.children().iter() {
for inner_node in inner_children.top().iter() {
let v_tree = crawl_node(inner_node.get(parser).unwrap(), parser).clone();
let v_tree = crawl_node(inner_node.get(parser).unwrap(), parser);
child_nodes.push(&JsValue::from(v_tree));
}
}
Expand All @@ -79,6 +78,7 @@ pub extern "C" fn parse(markup: &str) -> VTree {
let dom = tl::parse(markup, tl::ParserOptions::default()).unwrap();
let parser = dom.parser();
let child_nodes = Array::new();
let fragment_name = "#document-fragment".to_string();

// Use the DOM and map into the VTree structure diffHTML expects.
for dom_node in dom.children().iter() {
Expand All @@ -88,8 +88,8 @@ pub extern "C" fn parse(markup: &str) -> VTree {

// Root node is always a document fragment.
VTree {
rawNodeName: "#document-fragment".to_string(),
nodeName: "#document-fragment".to_string(),
rawNodeName: fragment_name.clone(),
nodeName: fragment_name.clone(),
nodeValue: "".to_string(),
childNodes: child_nodes,
attributes: Object::new(),
Expand Down

0 comments on commit e5c0f12

Please sign in to comment.