seperate text and other dynamic nodes

This commit is contained in:
Evan Almloff committed 2023-01-12 16:34:46 -06:00
1 parent 260589cd7f
commit a01a65a0eb
1 file changed
+46 -18
+46 -18
View File
@@ -32,6 +32,7 @@ fn create_random_attribute(attr_idx: &mut usize) -> TemplateAttribute<'static> {
} }
fn create_random_template_node( fn create_random_template_node(
dynamic_node_types: &mut Vec<DynamicNodeType>,
template_idx: &mut usize, template_idx: &mut usize,
attr_idx: &mut usize, attr_idx: &mut usize,
depth: usize, depth: usize,
@@ -49,11 +50,18 @@ fn create_random_template_node(
namespace: random_ns(), namespace: random_ns(),
attrs, attrs,
children: { children: {
if depth > 10 { if depth > 4 {
&[] &[]
} else { } else {
let children: Vec<_> = (0..(rand::random::<usize>() % 3)) let children: Vec<_> = (0..(rand::random::<usize>() % 3))
.map(|_| create_random_template_node(template_idx, attr_idx, depth + 1)) .map(|_| {
create_random_template_node(
dynamic_node_types,
template_idx,
attr_idx,
depth + 1,
)
})
.collect(); .collect();
Box::leak(children.into_boxed_slice()) Box::leak(children.into_boxed_slice())
} }
@@ -67,6 +75,7 @@ fn create_random_template_node(
id: { id: {
let old_idx = *template_idx; let old_idx = *template_idx;
*template_idx += 1; *template_idx += 1;
dynamic_node_types.push(DynamicNodeType::Text);
old_idx old_idx
}, },
}, },
@@ -74,6 +83,7 @@ fn create_random_template_node(
id: { id: {
let old_idx = *template_idx; let old_idx = *template_idx;
*template_idx += 1; *template_idx += 1;
dynamic_node_types.push(DynamicNodeType::Other);
old_idx old_idx
}, },
}, },
@@ -113,11 +123,19 @@ fn generate_paths(
} }
} }
fn create_random_template(name: &'static str) -> Template<'static> { enum DynamicNodeType {
Text,
Other,
}
fn create_random_template(name: &'static str) -> (Template<'static>, Vec<DynamicNodeType>) {
let mut dynamic_node_type = Vec::new();
let mut template_idx = 0; let mut template_idx = 0;
let mut attr_idx = 0; let mut attr_idx = 0;
let roots = (0..(1 + rand::random::<usize>() % 5)) let roots = (0..(1 + rand::random::<usize>() % 5))
.map(|_| create_random_template_node(&mut template_idx, &mut attr_idx, 0)) .map(|_| {
create_random_template_node(&mut dynamic_node_type, &mut template_idx, &mut attr_idx, 0)
})
.collect::<Vec<_>>(); .collect::<Vec<_>>();
assert!(!roots.is_empty()); assert!(!roots.is_empty());
let roots = Box::leak(roots.into_boxed_slice()); let roots = Box::leak(roots.into_boxed_slice());
@@ -140,18 +158,17 @@ fn create_random_template(name: &'static str) -> Template<'static> {
.collect::<Vec<_>>() .collect::<Vec<_>>()
.into_boxed_slice(), .into_boxed_slice(),
); );
Template { name, roots, node_paths, attr_paths } (
Template { name, roots, node_paths, attr_paths },
dynamic_node_type,
)
} }
fn create_random_dynamic_node(cx: &ScopeState, depth: usize) -> DynamicNode { fn create_random_dynamic_node(cx: &ScopeState, depth: usize) -> DynamicNode {
let range = if depth > 10 { 2 } else { 4 }; let range = if depth > 3 { 1 } else { 3 };
match rand::random::<u8>() % range { match rand::random::<u8>() % range {
0 => DynamicNode::Text(VText { 0 => DynamicNode::Placeholder(Default::default()),
value: Box::leak(format!("{}", rand::random::<usize>()).into_boxed_str()), 1 => cx.make_node((0..(rand::random::<u8>() % 5)).map(|_| VNode {
id: Default::default(),
}),
1 => DynamicNode::Placeholder(Default::default()),
2 => cx.make_node((0..(rand::random::<u8>() % 5)).map(|_| VNode {
key: None, key: None,
parent: Default::default(), parent: Default::default(),
template: Cell::new(Template { template: Cell::new(Template {
@@ -168,7 +185,7 @@ fn create_random_dynamic_node(cx: &ScopeState, depth: usize) -> DynamicNode {
)]), )]),
dynamic_attrs: &[], dynamic_attrs: &[],
})), })),
3 => cx.component( 2 => cx.component(
create_random_element, create_random_element,
DepthProps { depth, root: false }, DepthProps { depth, root: false },
"create_random_element", "create_random_element",
@@ -212,7 +229,7 @@ fn create_random_element(cx: Scope<DepthProps>) -> Element {
let range = if cx.props.root { 2 } else { 3 }; let range = if cx.props.root { 2 } else { 3 };
let node = match rand::random::<usize>() % range { let node = match rand::random::<usize>() % range {
0 | 1 => { 0 | 1 => {
let template = create_random_template(Box::leak( let (template, dynamic_node_types) = create_random_template(Box::leak(
format!( format!(
"{}{}", "{}{}",
concat!(file!(), ":", line!(), ":", column!(), ":"), concat!(file!(), ":", line!(), ":", column!(), ":"),
@@ -233,8 +250,19 @@ fn create_random_element(cx: Scope<DepthProps>) -> Element {
template: Cell::new(template), template: Cell::new(template),
root_ids: Default::default(), root_ids: Default::default(),
dynamic_nodes: { dynamic_nodes: {
let dynamic_nodes: Vec<_> = (0..template.node_paths.len()) let dynamic_nodes: Vec<_> = dynamic_node_types
.map(|_| create_random_dynamic_node(cx, cx.props.depth + 1)) .iter()
.map(|ty| match ty {
DynamicNodeType::Text => DynamicNode::Text(VText {
value: Box::leak(
format!("{}", rand::random::<usize>()).into_boxed_str(),
),
id: Default::default(),
}),
DynamicNodeType::Other => {
create_random_dynamic_node(cx, cx.props.depth + 1)
}
})
.collect(); .collect();
cx.bump().alloc(dynamic_nodes) cx.bump().alloc(dynamic_nodes)
}, },
@@ -255,7 +283,7 @@ fn create_random_element(cx: Scope<DepthProps>) -> Element {
// test for panics when creating random nodes and templates // test for panics when creating random nodes and templates
#[test] #[test]
fn create() { fn create() {
for _ in 0..1000 { for _ in 0..100 {
let mut vdom = let mut vdom =
VirtualDom::new_with_props(create_random_element, DepthProps { depth: 0, root: true }); VirtualDom::new_with_props(create_random_element, DepthProps { depth: 0, root: true });
let _ = vdom.rebuild(); let _ = vdom.rebuild();
@@ -266,7 +294,7 @@ fn create() {
// This test will change the template every render which is not very realistic, but it helps stress the system // This test will change the template every render which is not very realistic, but it helps stress the system
#[test] #[test]
fn diff() { fn diff() {
for _ in 0..100 { for _ in 0..10 {
let mut vdom = let mut vdom =
VirtualDom::new_with_props(create_random_element, DepthProps { depth: 0, root: true }); VirtualDom::new_with_props(create_random_element, DepthProps { depth: 0, root: true });
let _ = vdom.rebuild(); let _ = vdom.rebuild();