wip: memoize dom in the prescence of identical components
This commit is contained in:
1 parent
7cf15ee4e8
commit
cb2782b4bb
27 files changed
+474
-219
No files matched your search
+12
-16
@@ -390,10 +390,7 @@ impl<'bump> DiffState<'bump> {
|
||||
self.stack.add_child_count(1);
|
||||
|
||||
if let Some(cur_scope_id) = self.stack.current_scope() {
|
||||
let scope = self.scopes.get_scope(cur_scope_id).unwrap();
|
||||
|
||||
for listener in *listeners {
|
||||
self.attach_listener_to_scope(listener, scope);
|
||||
listener.mounted_node.set(Some(real_id));
|
||||
self.mutations.new_event_listener(listener, cur_scope_id);
|
||||
}
|
||||
@@ -446,9 +443,9 @@ impl<'bump> DiffState<'bump> {
|
||||
}
|
||||
false => {
|
||||
// track this component internally so we know the right drop order
|
||||
let cur_scope = self.scopes.get_scope(parent_idx).unwrap();
|
||||
let extended = unsafe { std::mem::transmute(vcomponent) };
|
||||
cur_scope.items.borrow_mut().borrowed_props.push(extended);
|
||||
// let cur_scope = self.scopes.get_scope(parent_idx).unwrap();
|
||||
// let extended = unsafe { std::mem::transmute(vcomponent) };
|
||||
// cur_scope.items.borrow_mut().borrowed_props.push(extended);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -580,8 +577,6 @@ impl<'bump> DiffState<'bump> {
|
||||
//
|
||||
// TODO: take a more efficient path than this
|
||||
if let Some(cur_scope_id) = self.stack.current_scope() {
|
||||
let scope = self.scopes.get_scope(cur_scope_id).unwrap();
|
||||
|
||||
if old.listeners.len() == new.listeners.len() {
|
||||
for (old_l, new_l) in old.listeners.iter().zip(new.listeners.iter()) {
|
||||
if old_l.event != new_l.event {
|
||||
@@ -590,7 +585,6 @@ impl<'bump> DiffState<'bump> {
|
||||
self.mutations.new_event_listener(new_l, cur_scope_id);
|
||||
}
|
||||
new_l.mounted_node.set(old_l.mounted_node.get());
|
||||
self.attach_listener_to_scope(new_l, scope);
|
||||
}
|
||||
} else {
|
||||
for listener in old.listeners {
|
||||
@@ -600,7 +594,6 @@ impl<'bump> DiffState<'bump> {
|
||||
for listener in new.listeners {
|
||||
listener.mounted_node.set(Some(root));
|
||||
self.mutations.new_event_listener(listener, cur_scope_id);
|
||||
self.attach_listener_to_scope(listener, scope);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -687,6 +680,12 @@ impl<'bump> DiffState<'bump> {
|
||||
new: &'bump VComponent<'bump>,
|
||||
) {
|
||||
let scope_addr = old.scope.get().unwrap();
|
||||
log::debug!("diff_component_nodes: {:?}", scope_addr);
|
||||
|
||||
if std::ptr::eq(old, new) {
|
||||
log::debug!("skipping component diff - component is the sames");
|
||||
return;
|
||||
}
|
||||
|
||||
// Make sure we're dealing with the same component (by function pointer)
|
||||
if old.user_fc == new.user_fc {
|
||||
@@ -728,11 +727,14 @@ impl<'bump> DiffState<'bump> {
|
||||
|
||||
// this should auto drop the previous props
|
||||
self.scopes.run_scope(scope_addr);
|
||||
self.mutations.dirty_scopes.insert(scope_addr);
|
||||
|
||||
self.diff_node(
|
||||
self.scopes.wip_head(scope_addr),
|
||||
self.scopes.fin_head(scope_addr),
|
||||
);
|
||||
} else {
|
||||
log::debug!("memoized");
|
||||
// memoization has taken place
|
||||
drop(new_props);
|
||||
};
|
||||
@@ -1293,10 +1295,4 @@ impl<'bump> DiffState<'bump> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Adds a listener closure to a scope during diff.
|
||||
fn attach_listener_to_scope(&mut self, listener: &'bump Listener<'bump>, scope: &ScopeState) {
|
||||
let long_listener = unsafe { std::mem::transmute(listener) };
|
||||
scope.items.borrow_mut().listeners.push(long_listener)
|
||||
}
|
||||
}
|
||||
@@ -244,15 +244,15 @@ fn round_to_words(len: usize) -> usize {
|
||||
fn it_works() {
|
||||
let bump = bumpalo::Bump::new();
|
||||
|
||||
let factory = NodeFactory { bump: &bump };
|
||||
// let factory = NodeFactory { bump: &bump };
|
||||
|
||||
let caller = LazyNodes::new_some(|f| {
|
||||
//
|
||||
f.text(format_args!("hello world!"))
|
||||
});
|
||||
let g = caller.call(factory);
|
||||
// let caller = LazyNodes::new_some(|f| {
|
||||
// //
|
||||
// f.text(format_args!("hello world!"))
|
||||
// });
|
||||
// let g = caller.call(factory);
|
||||
|
||||
dbg!(g);
|
||||
// dbg!(g);
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -260,37 +260,37 @@ fn it_drops() {
|
||||
use std::rc::Rc;
|
||||
let bump = bumpalo::Bump::new();
|
||||
|
||||
let factory = NodeFactory { bump: &bump };
|
||||
// let factory = NodeFactory { bump: &bump };
|
||||
|
||||
struct DropInner {
|
||||
id: i32,
|
||||
}
|
||||
impl Drop for DropInner {
|
||||
fn drop(&mut self) {
|
||||
log::debug!("dropping inner");
|
||||
}
|
||||
}
|
||||
let val = Rc::new(10);
|
||||
// struct DropInner {
|
||||
// id: i32,
|
||||
// }
|
||||
// impl Drop for DropInner {
|
||||
// fn drop(&mut self) {
|
||||
// log::debug!("dropping inner");
|
||||
// }
|
||||
// }
|
||||
// let val = Rc::new(10);
|
||||
|
||||
let caller = {
|
||||
let it = (0..10)
|
||||
.map(|i| {
|
||||
let val = val.clone();
|
||||
// let caller = {
|
||||
// let it = (0..10)
|
||||
// .map(|i| {
|
||||
// let val = val.clone();
|
||||
|
||||
LazyNodes::new_some(move |f| {
|
||||
log::debug!("hell closure");
|
||||
let inner = DropInner { id: i };
|
||||
f.text(format_args!("hello world {:?}, {:?}", inner.id, val))
|
||||
})
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
// LazyNodes::new_some(move |f| {
|
||||
// log::debug!("hell closure");
|
||||
// let inner = DropInner { id: i };
|
||||
// f.text(format_args!("hello world {:?}, {:?}", inner.id, val))
|
||||
// })
|
||||
// })
|
||||
// .collect::<Vec<_>>();
|
||||
|
||||
LazyNodes::new_some(|f| {
|
||||
log::debug!("main closure");
|
||||
f.fragment_from_iter(it)
|
||||
})
|
||||
};
|
||||
// LazyNodes::new_some(|f| {
|
||||
// log::debug!("main closure");
|
||||
// f.fragment_from_iter(it)
|
||||
// })
|
||||
// };
|
||||
|
||||
let _ = caller.call(factory);
|
||||
assert_eq!(Rc::strong_count(&val), 1);
|
||||
// let _ = caller.call(factory);
|
||||
// assert_eq!(Rc::strong_count(&val), 1);
|
||||
}
|
||||
@@ -395,12 +395,16 @@ impl<P> AnyProps for VComponentProps<P> {
|
||||
/// This struct adds metadata to the final VNode about listeners, attributes, and children
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct NodeFactory<'a> {
|
||||
pub(crate) scope: &'a ScopeState,
|
||||
pub(crate) bump: &'a Bump,
|
||||
}
|
||||
|
||||
impl<'a> NodeFactory<'a> {
|
||||
pub fn new(bump: &'a Bump) -> NodeFactory<'a> {
|
||||
NodeFactory { bump }
|
||||
pub fn new(scope: &'a ScopeState) -> NodeFactory<'a> {
|
||||
NodeFactory {
|
||||
scope,
|
||||
bump: &scope.wip_frame().bump,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
@@ -473,6 +477,12 @@ impl<'a> NodeFactory<'a> {
|
||||
) -> VNode<'a> {
|
||||
let key = key.map(|f| self.raw_text(f).0);
|
||||
|
||||
let mut items = self.scope.items.borrow_mut();
|
||||
for listener in listeners {
|
||||
let long_listener = unsafe { std::mem::transmute(listener) };
|
||||
items.listeners.push(long_listener);
|
||||
}
|
||||
|
||||
VNode::Element(self.bump.alloc(VElement {
|
||||
tag: tag_name,
|
||||
key,
|
||||
@@ -511,7 +521,7 @@ impl<'a> NodeFactory<'a> {
|
||||
where
|
||||
P: Properties + 'a,
|
||||
{
|
||||
VNode::Component(self.bump.alloc(VComponent {
|
||||
let vcomp = self.bump.alloc(VComponent {
|
||||
key: key.map(|f| self.raw_text(f).0),
|
||||
scope: Default::default(),
|
||||
can_memoize: P::IS_STATIC,
|
||||
@@ -528,7 +538,15 @@ impl<'a> NodeFactory<'a> {
|
||||
// the transformation from this specific lifetime to the for<'a> lifetime
|
||||
render_fn: unsafe { std::mem::transmute(component) },
|
||||
}))),
|
||||
}))
|
||||
});
|
||||
|
||||
if !P::IS_STATIC {
|
||||
let vcomp = &*vcomp;
|
||||
let vcomp = unsafe { std::mem::transmute(vcomp) };
|
||||
self.scope.items.borrow_mut().borrowed_props.push(vcomp);
|
||||
}
|
||||
|
||||
VNode::Component(vcomp)
|
||||
}
|
||||
|
||||
pub fn listener(self, event: &'static str, callback: EventHandler<'a>) -> Listener<'a> {
|
||||
@@ -730,8 +748,11 @@ impl IntoVNode<'_> for Arguments<'_> {
|
||||
|
||||
impl<'a> IntoVNode<'a> for &Option<VNode<'a>> {
|
||||
fn into_vnode(self, cx: NodeFactory<'a>) -> VNode<'a> {
|
||||
let r = self.as_ref().map(|f| f.decouple());
|
||||
cx.fragment_from_iter(r)
|
||||
self.as_ref()
|
||||
.map(|f| f.decouple())
|
||||
.unwrap_or(VNode::Placeholder(
|
||||
cx.bump.alloc(VPlaceholder { id: empty_cell() }),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -143,6 +143,7 @@ impl ScopeArena {
|
||||
|
||||
// Removes a scope and its descendents from the arena
|
||||
pub fn try_remove(&self, id: ScopeId) -> Option<()> {
|
||||
log::debug!("removing scope {:?}", id);
|
||||
self.ensure_drop_safety(id);
|
||||
|
||||
// Safety:
|
||||
@@ -188,6 +189,8 @@ impl ScopeArena {
|
||||
/// This also makes sure that drop order is consistent and predictable. All resources that rely on being dropped will
|
||||
/// be dropped.
|
||||
pub(crate) fn ensure_drop_safety(&self, scope_id: ScopeId) {
|
||||
log::debug!("Ensuring drop safety for scope {:?}", scope_id);
|
||||
|
||||
if let Some(scope) = self.get_scope(scope_id) {
|
||||
let mut items = scope.items.borrow_mut();
|
||||
|
||||
@@ -217,6 +220,7 @@ impl ScopeArena {
|
||||
// Cycle to the next frame and then reset it
|
||||
// This breaks any latent references, invalidating every pointer referencing into it.
|
||||
// Remove all the outdated listeners
|
||||
log::debug!("Running scope {:?}", id);
|
||||
self.ensure_drop_safety(id);
|
||||
|
||||
// todo: we *know* that this is aliased by the contents of the scope itself
|
||||
@@ -278,14 +282,19 @@ impl ScopeArena {
|
||||
let nodes = self.nodes.borrow();
|
||||
let mut cur_el = Some(element);
|
||||
|
||||
log::debug!("calling listener {:?}, {:?}", event, element);
|
||||
let state = Rc::new(BubbleState::new());
|
||||
|
||||
while let Some(id) = cur_el.take() {
|
||||
if let Some(el) = nodes.get(id.0) {
|
||||
log::debug!("Found valid receiver element");
|
||||
|
||||
let real_el = unsafe { &**el };
|
||||
if let VNode::Element(real_el) = real_el {
|
||||
for listener in real_el.listeners.borrow().iter() {
|
||||
if listener.event == event.name {
|
||||
log::debug!("Found valid receiver event");
|
||||
|
||||
if state.canceled.get() {
|
||||
// stop bubbling if canceled
|
||||
break;
|
||||
@@ -651,12 +660,14 @@ impl ScopeState {
|
||||
/// rsx!(cx, div { "hello {state.0}" })
|
||||
/// }
|
||||
/// ```
|
||||
pub fn provide_context<T: 'static>(&self, value: T) {
|
||||
pub fn provide_context<T: 'static>(&self, value: T) -> Rc<T> {
|
||||
let value = Rc::new(value);
|
||||
self.shared_contexts
|
||||
.borrow_mut()
|
||||
.insert(TypeId::of::<T>(), Rc::new(value))
|
||||
.insert(TypeId::of::<T>(), value.clone())
|
||||
.map(|f| f.downcast::<T>().ok())
|
||||
.flatten();
|
||||
value
|
||||
}
|
||||
|
||||
/// Try to retrieve a SharedState with type T from the any parent Scope.
|
||||
@@ -713,6 +724,7 @@ impl ScopeState {
|
||||
///```
|
||||
pub fn render<'src>(&'src self, rsx: LazyNodes<'src, '_>) -> Option<VNode<'src>> {
|
||||
Some(rsx.call(NodeFactory {
|
||||
scope: self,
|
||||
bump: &self.wip_frame().bump,
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -531,6 +531,9 @@ impl VirtualDom {
|
||||
diff_state.stack.element_stack.push(ElementId(0));
|
||||
diff_state.stack.scope_stack.push(scope_id);
|
||||
diff_state.work(|| false);
|
||||
self.dirty_scopes.clear();
|
||||
assert!(self.dirty_scopes.is_empty());
|
||||
|
||||
diff_state.mutations
|
||||
}
|
||||
|
||||
@@ -599,7 +602,7 @@ impl VirtualDom {
|
||||
pub fn render_vnodes<'a>(&'a self, lazy_nodes: LazyNodes<'a, '_>) -> &'a VNode<'a> {
|
||||
let scope = self.scopes.get_scope(ScopeId(0)).unwrap();
|
||||
let frame = scope.wip_frame();
|
||||
let factory = NodeFactory { bump: &frame.bump };
|
||||
let factory = NodeFactory::new(scope);
|
||||
let node = lazy_nodes.call(factory);
|
||||
frame.bump.alloc(node)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user