wip: debugging

This commit is contained in:
Jonathan Kelley committed 2022-01-30 14:08:03 -05:00
1 parent 875977f5a6
commit 3edf3e367f
11 files changed
+367 -128

No files matched your search

+53 -6
View File
@@ -443,6 +443,13 @@ impl<'bump> DiffState<'bump> {
new_idx
};
log::info!(
"created component {:?} with parent {:?} and originator {:?}",
new_idx,
parent_idx,
vcomponent.originator
);
// Actually initialize the caller's slot with the right address
vcomponent.scope.set(Some(new_idx));
@@ -476,6 +483,11 @@ impl<'bump> DiffState<'bump> {
// Check the most common cases first
// these are *actual* elements, not wrappers around lists
(Text(old), Text(new)) => {
if std::ptr::eq(old, new) {
log::trace!("skipping node diff - text are the sames");
return;
}
if let Some(root) = old.id.get() {
if old.text != new.text {
self.mutations.set_text(new.text, root.as_u64());
@@ -487,24 +499,46 @@ impl<'bump> DiffState<'bump> {
}
(Placeholder(old), Placeholder(new)) => {
if std::ptr::eq(old, new) {
log::trace!("skipping node diff - placeholder are the sames");
return;
}
if let Some(root) = old.id.get() {
self.scopes.update_node(new_node, root);
new.id.set(Some(root))
}
}
(Element(old), Element(new)) => self.diff_element_nodes(old, new, old_node, new_node),
(Element(old), Element(new)) => {
if std::ptr::eq(old, new) {
log::trace!("skipping node diff - element are the sames");
return;
}
self.diff_element_nodes(old, new, old_node, new_node)
}
// These two sets are pointers to nodes but are not actually nodes themselves
(Component(old), Component(new)) => {
if std::ptr::eq(old, new) {
log::trace!("skipping node diff - placeholder are the sames");
return;
}
self.diff_component_nodes(old_node, new_node, *old, *new)
}
(Fragment(old), Fragment(new)) => self.diff_fragment_nodes(old, new),
(Fragment(old), Fragment(new)) => {
if std::ptr::eq(old, new) {
log::trace!("skipping node diff - fragment are the sames");
return;
}
self.diff_fragment_nodes(old, new)
}
// The normal pathway still works, but generates slightly weird instructions
// This pathway ensures uses the ReplaceAll, not the InsertAfter and remove
(Placeholder(_), Fragment(new)) => {
log::debug!("replacing placeholder with fragment {:?}", new_node);
self.stack
.create_children(new.children, MountType::Replace { old: old_node });
}
@@ -686,7 +720,11 @@ impl<'bump> DiffState<'bump> {
new: &'bump VComponent<'bump>,
) {
let scope_addr = old.scope.get().unwrap();
log::trace!("diff_component_nodes: {:?}", scope_addr);
log::trace!(
"diff_component_nodes. old: {:#?} new: {:#?}",
old_node,
new_node
);
if std::ptr::eq(old, new) {
log::trace!("skipping component diff - component is the sames");
@@ -747,6 +785,8 @@ impl<'bump> DiffState<'bump> {
self.stack.scope_stack.pop();
} else {
//
log::debug!("scope stack is {:#?}", self.stack.scope_stack);
self.stack
.create_node(new_node, MountType::Replace { old: old_node });
}
@@ -790,7 +830,10 @@ impl<'bump> DiffState<'bump> {
match (old, new) {
([], []) => {}
([], _) => self.stack.create_children(new, MountType::Append),
(_, []) => self.remove_nodes(old, true),
(_, []) => {
log::debug!("removing nodes {:?}", old);
self.remove_nodes(old, true)
}
_ => {
let new_is_keyed = new[0].key().is_some();
let old_is_keyed = old[0].key().is_some();
@@ -1216,6 +1259,7 @@ impl<'bump> DiffState<'bump> {
}
fn replace_node(&mut self, old: &'bump VNode<'bump>, nodes_created: usize) {
log::debug!("Replacing node {:?}", old);
match old {
VNode::Element(el) => {
let id = old
@@ -1262,11 +1306,12 @@ impl<'bump> DiffState<'bump> {
) {
// or cache the vec on the diff machine
for node in nodes {
log::debug!("removing {:?}", node);
match node {
VNode::Text(t) => {
// this check exists because our null node will be removed but does not have an ID
if let Some(id) = t.id.get() {
self.scopes.collect_garbage(id);
// self.scopes.collect_garbage(id);
if gen_muts {
self.mutations.remove(id.as_u64());
@@ -1275,7 +1320,7 @@ impl<'bump> DiffState<'bump> {
}
VNode::Placeholder(a) => {
let id = a.id.get().unwrap();
self.scopes.collect_garbage(id);
// self.scopes.collect_garbage(id);
if gen_muts {
self.mutations.remove(id.as_u64());
@@ -1289,6 +1334,8 @@ impl<'bump> DiffState<'bump> {
}
self.remove_nodes(e.children, false);
// self.scopes.collect_garbage(id);
}
VNode::Fragment(f) => {
+13 -5
View File
@@ -4,7 +4,7 @@
//! cheap and *very* fast to construct - building a full tree should be quick.
use crate::{
innerlude::{Element, Properties, Scope, ScopeId, ScopeState},
innerlude::{Element, FcSlot, Properties, Scope, ScopeId, ScopeState},
lazynodes::LazyNodes,
AnyEvent, Component,
};
@@ -177,11 +177,19 @@ impl Debug for VNode<'_> {
.field("children", &el.children)
.finish(),
VNode::Text(t) => write!(s, "VNode::VText {{ text: {} }}", t.text),
VNode::Placeholder(_) => write!(s, "VNode::VPlaceholder"),
VNode::Placeholder(t) => write!(s, "VNode::VPlaceholder {{ id: {:?} }}", t.id),
VNode::Fragment(frag) => {
write!(s, "VNode::VFragment {{ children: {:?} }}", frag.children)
}
VNode::Component(comp) => write!(s, "VNode::VComponent {{ fc: {:?}}}", comp.user_fc),
VNode::Component(comp) => {
s.debug_struct("VNode::VComponent")
.field("fnptr", &comp.user_fc)
.field("key", &comp.key)
.field("scope", &comp.scope)
.field("originator", &comp.originator)
.finish()
//write!(s, "VNode::VComponent {{ fc: {:?}}}", comp.user_fc)
}
}
}
}
@@ -384,7 +392,7 @@ pub struct VComponent<'src> {
pub originator: ScopeId,
pub scope: Cell<Option<ScopeId>>,
pub can_memoize: bool,
pub user_fc: *const (),
pub user_fc: FcSlot,
pub props: RefCell<Option<Box<dyn AnyProps + 'src>>>,
}
@@ -557,7 +565,7 @@ impl<'a> NodeFactory<'a> {
key: key.map(|f| self.raw_text(f).0),
scope: Default::default(),
can_memoize: P::IS_STATIC,
user_fc: component as *const (),
user_fc: component as *mut std::os::raw::c_void,
originator: self.scope.scope_id(),
props: RefCell::new(Some(Box::new(VComponentProps {
// local_props: RefCell::new(Some(props)),
+60 -53
View File
@@ -13,7 +13,9 @@ use std::{
rc::Rc,
};
pub(crate) type FcSlot = *const ();
/// for traceability, we use the raw fn pointer to identify the function
/// we can use this with the traceback crate to resolve funciton names
pub(crate) type FcSlot = *mut std::os::raw::c_void;
pub(crate) struct Heuristic {
hook_arena_size: usize,
@@ -82,7 +84,7 @@ impl ScopeArena {
pub(crate) fn new_with_key(
&self,
fc_ptr: *const (),
fc_ptr: FcSlot,
vcomp: Box<dyn AnyProps>,
parent_scope: Option<ScopeId>,
container: ElementId,
@@ -116,25 +118,47 @@ impl ScopeArena {
scope.subtree.set(subtree);
scope.our_arena_idx = new_scope_id;
scope.container = container;
scope.fnptr = fc_ptr;
let any_item = self.scopes.borrow_mut().insert(new_scope_id, scope);
debug_assert!(any_item.is_none());
} else {
// else create a new scope
let (node_capacity, hook_capacity) = self
.heuristics
.borrow()
.get(&fc_ptr)
.map(|h| (h.node_arena_size, h.hook_arena_size))
.unwrap_or_default();
self.scopes.borrow_mut().insert(
new_scope_id,
self.bump.alloc(ScopeState::new(
height,
self.bump.alloc(ScopeState {
container,
new_scope_id,
our_arena_idx: new_scope_id,
parent_scope,
vcomp,
self.tasks.clone(),
self.heuristics
.borrow()
.get(&fc_ptr)
.map(|h| (h.node_arena_size, h.hook_arena_size))
.unwrap_or_default(),
)),
height,
fnptr: fc_ptr,
props: RefCell::new(Some(vcomp)),
frames: [BumpFrame::new(node_capacity), BumpFrame::new(node_capacity)],
// todo: subtrees
subtree: Cell::new(0),
is_subtree_root: Cell::new(false),
generation: 0.into(),
tasks: self.tasks.clone(),
shared_contexts: Default::default(),
items: RefCell::new(SelfReferentialItems {
listeners: Default::default(),
borrowed_props: Default::default(),
}),
hook_arena: Bump::new(),
hook_vals: RefCell::new(Vec::with_capacity(hook_capacity)),
hook_idx: Default::default(),
}),
);
}
@@ -169,10 +193,17 @@ impl ScopeArena {
pub fn update_node<'a>(&self, node: &'a VNode<'a>, id: ElementId) {
let node = unsafe { extend_vnode(node) };
*self.nodes.borrow_mut().get_mut(id.0).unwrap() = node;
let mut nodes = self.nodes.borrow_mut();
let entry = nodes.get_mut(id.0);
match entry {
Some(_node) => *_node = node,
None => panic!("cannot update node {}", id),
}
}
pub fn collect_garbage(&self, id: ElementId) {
log::debug!("collecting garbage for {:?}", id);
self.nodes.borrow_mut().remove(id.0);
}
@@ -189,7 +220,7 @@ 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::trace!("Ensuring drop safety for scope {:?}", scope_id);
// log::trace!("Ensuring drop safety for scope {:?}", scope_id);
if let Some(scope) = self.get_scope(scope_id) {
let mut items = scope.items.borrow_mut();
@@ -217,12 +248,23 @@ 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::trace!("Running scope {:?}", id);
self.ensure_drop_safety(id);
// todo: we *know* that this is aliased by the contents of the scope itself
let scope = unsafe { &mut *self.get_scope_raw(id).expect("could not find scope") };
// if cfg!(debug_assertions) {
log::debug!("running scope {:?} symbol: {:?}", id, scope.fnptr);
// todo: resolve frames properly
backtrace::resolve(scope.fnptr, |symbol| {
// backtrace::resolve(scope.fnptr as *mut std::os::raw::c_void, |symbol| {
// panic!("asd");
// log::trace!("Running scope {:?}, ptr {:?}", id, scope.fnptr);
log::debug!("running scope {:?} symbol: {:?}", id, symbol.name());
});
// }
// Safety:
// - We dropped the listeners, so no more &mut T can be used while these are held
// - All children nodes that rely on &mut T are replaced with a new reference
@@ -421,6 +463,7 @@ pub struct ScopeState {
pub(crate) container: ElementId,
pub(crate) our_arena_idx: ScopeId,
pub(crate) height: u32,
pub(crate) fnptr: FcSlot,
// todo: subtrees
pub(crate) is_subtree_root: Cell<bool>,
@@ -449,43 +492,6 @@ pub struct SelfReferentialItems<'a> {
// Public methods exposed to libraries and components
impl ScopeState {
fn new(
height: u32,
container: ElementId,
our_arena_idx: ScopeId,
parent_scope: Option<*mut ScopeState>,
vcomp: Box<dyn AnyProps>,
tasks: Rc<TaskQueue>,
(node_capacity, hook_capacity): (usize, usize),
) -> Self {
ScopeState {
container,
our_arena_idx,
parent_scope,
height,
props: RefCell::new(Some(vcomp)),
frames: [BumpFrame::new(node_capacity), BumpFrame::new(node_capacity)],
// todo: subtrees
subtree: Cell::new(0),
is_subtree_root: Cell::new(false),
generation: 0.into(),
tasks,
shared_contexts: Default::default(),
items: RefCell::new(SelfReferentialItems {
listeners: Default::default(),
borrowed_props: Default::default(),
}),
hook_arena: Bump::new(),
hook_vals: RefCell::new(Vec::with_capacity(hook_capacity)),
hook_idx: Default::default(),
}
}
/// Get the subtree ID that this scope belongs to.
///
/// Each component has its own subtree ID - the root subtree has an ID of 0. This ID is used by the renderer to route
@@ -731,6 +737,7 @@ impl ScopeState {
while let Some(parent_ptr) = search_parent {
// safety: all parent pointers are valid thanks to the bump arena
let parent = unsafe { &*parent_ptr };
log::trace!("Searching parent scope {:?}", parent.scope_id());
if let Some(shared) = parent.shared_contexts.borrow().get(&TypeId::of::<T>()) {
return Some(shared.clone().downcast::<T>().unwrap());
}
+3 -1
View File
@@ -212,7 +212,7 @@ impl VirtualDom {
let scopes = ScopeArena::new(channel.0.clone());
scopes.new_with_key(
root as *const _,
root as *mut std::os::raw::c_void,
Box::new(VComponentProps {
props: root_props,
memo: |_a, _b| unreachable!("memo on root will neve be run"),
@@ -475,6 +475,8 @@ impl VirtualDom {
let (old, new) = (self.scopes.wip_head(scopeid), self.scopes.fin_head(scopeid));
diff_state.stack.push(DiffInstruction::Diff { new, old });
log::debug!("pushing scope {:?} onto scope stack", scopeid);
diff_state.stack.scope_stack.push(scopeid);
let scope = scopes.get_scope(scopeid).unwrap();