mirror of
https://github.com/DioxusLabs/dioxus
synced 2024-12-19 17:13:12 +00:00
047ed1e553
This commit adds subtree memoization to Dioxus. Subtree memoization is basically a compile-time step that drastically reduces the amount of work the diffing engine needs to do at runtime by extracting non-changing nodes out into a static "template." Templates are then understood by the various renderers in the ecosystem as a faster way of rendering the same items. For example, in the web, templates are simply a set of DOM Nodes created once and then cloned later. This is the same pattern frameworks like Lithtml and SolidJS use to achieve near-perfect performance. Subtree memoization adds an additional level of complexity to Dioxus. The RSX macro needs to be much smarter to identify changing/nonchanging nodes and generate a mapping between the Template and its runtime counterparts. This commit represents a working starter point for this work, adding support for templates for the web, desktop, liveview, ssr, and native-core renderers. In the future we will try to shrink code generation, generally improve performance, and simplify our implementation.
118 lines
2.8 KiB
Rust
118 lines
2.8 KiB
Rust
use dioxus::core::{self as dioxus_core, GlobalNodeId};
|
|
use dioxus::prelude::*;
|
|
use dioxus_native_core::real_dom::RealDom;
|
|
use dioxus_native_core::state::State;
|
|
use dioxus_native_core_macro::State;
|
|
|
|
#[derive(Default, Clone, State)]
|
|
struct Empty {}
|
|
|
|
#[test]
|
|
fn initial_build_simple() {
|
|
#[allow(non_snake_case)]
|
|
fn Base(cx: Scope) -> Element {
|
|
render!(div {})
|
|
}
|
|
|
|
let vdom = VirtualDom::new(Base);
|
|
|
|
let mutations = vdom.create_vnodes(rsx! {
|
|
div{}
|
|
});
|
|
|
|
let mut dom: RealDom<Empty> = RealDom::new();
|
|
|
|
let _to_update = dom.apply_mutations(vec![mutations]);
|
|
|
|
assert_eq!(dom.size(), 1);
|
|
assert_eq!(
|
|
dom[GlobalNodeId::TemplateId {
|
|
template_ref_id: dioxus_core::ElementId(1),
|
|
template_node_id: dioxus::prelude::TemplateNodeId(0),
|
|
}]
|
|
.node_data
|
|
.height,
|
|
1
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn initial_build_with_children() {
|
|
#[allow(non_snake_case)]
|
|
fn Base(cx: Scope) -> Element {
|
|
render!(div {})
|
|
}
|
|
|
|
let vdom = VirtualDom::new(Base);
|
|
|
|
let mutations = vdom.create_vnodes(rsx! {
|
|
div{
|
|
div{
|
|
"hello"
|
|
p{
|
|
"world"
|
|
}
|
|
"hello world"
|
|
}
|
|
}
|
|
});
|
|
|
|
let mut dom: RealDom<Empty> = RealDom::new();
|
|
|
|
let _to_update = dom.apply_mutations(vec![mutations]);
|
|
assert_eq!(dom.size(), 1);
|
|
assert_eq!(
|
|
dom[GlobalNodeId::TemplateId {
|
|
template_ref_id: dioxus_core::ElementId(1),
|
|
template_node_id: dioxus::prelude::TemplateNodeId(0),
|
|
}]
|
|
.node_data
|
|
.height,
|
|
1
|
|
);
|
|
assert_eq!(
|
|
dom[GlobalNodeId::TemplateId {
|
|
template_ref_id: dioxus_core::ElementId(1),
|
|
template_node_id: dioxus::prelude::TemplateNodeId(1),
|
|
}]
|
|
.node_data
|
|
.height,
|
|
2
|
|
);
|
|
assert_eq!(
|
|
dom[GlobalNodeId::TemplateId {
|
|
template_ref_id: dioxus_core::ElementId(1),
|
|
template_node_id: dioxus::prelude::TemplateNodeId(2),
|
|
}]
|
|
.node_data
|
|
.height,
|
|
3
|
|
);
|
|
assert_eq!(
|
|
dom[GlobalNodeId::TemplateId {
|
|
template_ref_id: dioxus_core::ElementId(1),
|
|
template_node_id: dioxus::prelude::TemplateNodeId(3),
|
|
}]
|
|
.node_data
|
|
.height,
|
|
3
|
|
);
|
|
assert_eq!(
|
|
dom[GlobalNodeId::TemplateId {
|
|
template_ref_id: dioxus_core::ElementId(1),
|
|
template_node_id: dioxus::prelude::TemplateNodeId(4),
|
|
}]
|
|
.node_data
|
|
.height,
|
|
4
|
|
);
|
|
assert_eq!(
|
|
dom[GlobalNodeId::TemplateId {
|
|
template_ref_id: dioxus_core::ElementId(1),
|
|
template_node_id: dioxus::prelude::TemplateNodeId(5),
|
|
}]
|
|
.node_data
|
|
.height,
|
|
3
|
|
);
|
|
}
|