feat: it compiles once more

This commit is contained in:
Jonathan Kelley committed 2021-11-11 16:36:51 -05:00
1 parent 74c6211408
commit 8acdd2ea83
9 files changed
+278 -120

No files matched your search

+33 -11
View File
@@ -454,7 +454,6 @@ impl<'bump> DiffState<'bump> {
// TODO: // TODO:
// add noderefs to current noderef list Noderefs // add noderefs to current noderef list Noderefs
// add effects to current effect list Effects // add effects to current effect list Effects
let new_component = self.scopes.get_scope(&new_idx).unwrap(); let new_component = self.scopes.get_scope(&new_idx).unwrap();
log::debug!( log::debug!(
@@ -633,17 +632,26 @@ impl<'bump> DiffState<'bump> {
) { ) {
let scope_addr = old.associated_scope.get().unwrap(); let scope_addr = old.associated_scope.get().unwrap();
log::debug!(
"Diffing components. old_scope: {:?}, old_addr: {:?}, new_addr: {:?}",
scope_addr,
old.user_fc,
new.user_fc
);
// Make sure we're dealing with the same component (by function pointer) // Make sure we're dealing with the same component (by function pointer)
if old.user_fc == new.user_fc { if old.user_fc == new.user_fc {
log::debug!("Diffing component {:?} - {:?}", new.user_fc, scope_addr);
//
self.stack.scope_stack.push(scope_addr); self.stack.scope_stack.push(scope_addr);
// Make sure the new component vnode is referencing the right scope id // Make sure the new component vnode is referencing the right scope id
new.associated_scope.set(Some(scope_addr)); new.associated_scope.set(Some(scope_addr));
// make sure the component's caller function is up to date // make sure the component's caller function is up to date
let scope = unsafe { self.scopes.get_scope_mut(&scope_addr).unwrap() }; let scope = unsafe {
self.scopes
.get_scope_mut(&scope_addr)
.expect(&format!("could not find {:?}", scope_addr))
};
scope.caller = unsafe { std::mem::transmute(new.caller) }; scope.caller = unsafe { std::mem::transmute(new.caller) };
// React doesn't automatically memoize, but we do. // React doesn't automatically memoize, but we do.
@@ -1164,7 +1172,6 @@ impl<'bump> DiffState<'bump> {
} }
VNode::Component(el) => { VNode::Component(el) => {
let scope_id = el.associated_scope.get().unwrap(); let scope_id = el.associated_scope.get().unwrap();
// let scope = self.scopes.get_scope(&scope_id).unwrap();
search_node = Some(self.scopes.root_node(&scope_id)); search_node = Some(self.scopes.root_node(&scope_id));
} }
} }
@@ -1198,7 +1205,13 @@ impl<'bump> DiffState<'bump> {
fn replace_node(&mut self, old: &'bump VNode<'bump>, nodes_created: usize) { fn replace_node(&mut self, old: &'bump VNode<'bump>, nodes_created: usize) {
match old { match old {
VNode::Text(_) | VNode::Element(_) | VNode::Anchor(_) | VNode::Suspended(_) => { VNode::Element(el) => {
let id = old.try_mounted_id().expect(&format!("broke on {:?}", old));
self.mutations.replace_with(id, nodes_created as u32);
self.remove_nodes(el.children, false);
}
VNode::Text(_) | VNode::Anchor(_) | VNode::Suspended(_) => {
let id = old.try_mounted_id().expect(&format!("broke on {:?}", old)); let id = old.try_mounted_id().expect(&format!("broke on {:?}", old));
self.mutations.replace_with(id, nodes_created as u32); self.mutations.replace_with(id, nodes_created as u32);
} }
@@ -1211,6 +1224,11 @@ impl<'bump> DiffState<'bump> {
VNode::Component(c) => { VNode::Component(c) => {
let node = self.scopes.fin_head(&c.associated_scope.get().unwrap()); let node = self.scopes.fin_head(&c.associated_scope.get().unwrap());
self.replace_node(node, nodes_created); self.replace_node(node, nodes_created);
let scope_id = c.associated_scope.get().unwrap();
println!("replacing c {:?} ", scope_id);
log::debug!("Destroying scope {:?}", scope_id);
self.scopes.try_remove(&scope_id).unwrap();
} }
VNode::Linked(l) => { VNode::Linked(l) => {
@@ -1276,16 +1294,20 @@ impl<'bump> DiffState<'bump> {
} }
VNode::Component(c) => { VNode::Component(c) => {
let scope_id = c.associated_scope.get().unwrap(); self.destroy_vomponent(c, gen_muts);
let root = self.scopes.root_node(&scope_id);
self.remove_nodes(Some(root), gen_muts);
log::debug!("Destroying scope {:?}", scope_id);
self.scopes.try_remove(&scope_id).unwrap();
} }
} }
} }
} }
fn destroy_vomponent(&mut self, vc: &VComponent, gen_muts: bool) {
let scope_id = vc.associated_scope.get().unwrap();
let root = self.scopes.root_node(&scope_id);
self.remove_nodes(Some(root), gen_muts);
log::debug!("Destroying scope {:?}", scope_id);
self.scopes.try_remove(&scope_id).unwrap();
}
/// Adds a listener closure to a scope during diff. /// Adds a listener closure to a scope during diff.
fn attach_listener_to_scope(&mut self, listener: &'bump Listener<'bump>, scope: &Scope) { fn attach_listener_to_scope(&mut self, listener: &'bump Listener<'bump>, scope: &Scope) {
let long_listener = unsafe { std::mem::transmute(listener) }; let long_listener = unsafe { std::mem::transmute(listener) };
+1 -12
View File
@@ -27,7 +27,7 @@ pub struct LazyNodes<'a, 'b> {
inner: StackNodeStorage<'a, 'b>, inner: StackNodeStorage<'a, 'b>,
} }
type StackHeapSize = [usize; 8]; type StackHeapSize = [usize; 16];
enum StackNodeStorage<'a, 'b> { enum StackNodeStorage<'a, 'b> {
Stack(LazyStack), Stack(LazyStack),
@@ -88,21 +88,10 @@ impl<'a, 'b> LazyNodes<'a, 'b> {
let max_size = mem::size_of::<StackHeapSize>(); let max_size = mem::size_of::<StackHeapSize>();
if stored_size > max_size { if stored_size > max_size {
log::debug!(
"lazy nodes was too large to fit into stack. falling back to heap. max: {}, actual {:?}",
max_size,
stored_size
);
Self { Self {
inner: StackNodeStorage::Heap(Box::new(val)), inner: StackNodeStorage::Heap(Box::new(val)),
} }
} else { } else {
log::debug!(
"lazy nodes fits on stack! max: {}, actual: {:?}",
max_size,
stored_size
);
let mut buf: StackHeapSize = StackHeapSize::default(); let mut buf: StackHeapSize = StackHeapSize::default();
assert!(info.len() + round_to_words(size) <= buf.as_ref().len()); assert!(info.len() + round_to_words(size) <= buf.as_ref().len());
+1 -1
View File
@@ -499,7 +499,7 @@ impl Scope {
} }
} }
pub fn root_node(&self) -> &VNode { pub fn root_node<'a>(&'a self) -> &'a VNode<'a> {
let node = *self.wip_frame().nodes.borrow().get(0).unwrap(); let node = *self.wip_frame().nodes.borrow().get(0).unwrap();
unsafe { std::mem::transmute(&*node) } unsafe { std::mem::transmute(&*node) }
} }
+131 -76
View File
@@ -19,9 +19,10 @@ pub struct Heuristic {
// has an internal heuristics engine to pre-allocate arenas to the right size // has an internal heuristics engine to pre-allocate arenas to the right size
pub(crate) struct ScopeArena { pub(crate) struct ScopeArena {
bump: Bump, bump: Bump,
scopes: RefCell<Vec<*mut Scope>>, scope_counter: Cell<usize>,
scopes: RefCell<FxHashMap<ScopeId, *mut Scope>>,
pub heuristics: RefCell<FxHashMap<FcSlot, Heuristic>>, pub heuristics: RefCell<FxHashMap<FcSlot, Heuristic>>,
free_scopes: RefCell<Vec<ScopeId>>, free_scopes: RefCell<Vec<*mut Scope>>,
nodes: RefCell<Slab<*const VNode<'static>>>, nodes: RefCell<Slab<*const VNode<'static>>>,
pub(crate) sender: UnboundedSender<SchedulerMsg>, pub(crate) sender: UnboundedSender<SchedulerMsg>,
} }
@@ -29,8 +30,9 @@ pub(crate) struct ScopeArena {
impl ScopeArena { impl ScopeArena {
pub fn new(sender: UnboundedSender<SchedulerMsg>) -> Self { pub fn new(sender: UnboundedSender<SchedulerMsg>) -> Self {
Self { Self {
scope_counter: Cell::new(0),
bump: Bump::new(), bump: Bump::new(),
scopes: RefCell::new(Vec::new()), scopes: RefCell::new(FxHashMap::default()),
heuristics: RefCell::new(FxHashMap::default()), heuristics: RefCell::new(FxHashMap::default()),
free_scopes: RefCell::new(Vec::new()), free_scopes: RefCell::new(Vec::new()),
nodes: RefCell::new(Slab::new()), nodes: RefCell::new(Slab::new()),
@@ -39,17 +41,17 @@ impl ScopeArena {
} }
pub fn get_scope(&self, id: &ScopeId) -> Option<&Scope> { pub fn get_scope(&self, id: &ScopeId) -> Option<&Scope> {
unsafe { self.scopes.borrow().get(id.0).map(|f| &**f) } unsafe { self.scopes.borrow().get(id).map(|f| &**f) }
} }
// this is unsafe // this is unsafe
pub unsafe fn get_scope_raw(&self, id: &ScopeId) -> Option<*mut Scope> { pub unsafe fn get_scope_raw(&self, id: &ScopeId) -> Option<*mut Scope> {
self.scopes.borrow().get(id.0).map(|f| *f) self.scopes.borrow().get(id).map(|f| *f)
} }
// this is unsafe // this is unsafe
pub unsafe fn get_scope_mut(&self, id: &ScopeId) -> Option<&mut Scope> { pub unsafe fn get_scope_mut(&self, id: &ScopeId) -> Option<&mut Scope> {
self.scopes.borrow().get(id.0).map(|s| &mut **s) self.scopes.borrow().get(id).map(|s| &mut **s)
} }
pub fn new_with_key( pub fn new_with_key(
@@ -60,80 +62,128 @@ impl ScopeArena {
height: u32, height: u32,
subtree: u32, subtree: u32,
) -> ScopeId { ) -> ScopeId {
let scope_id = ScopeId(self.scopes.borrow().len()); let new_scope_id = ScopeId(self.scope_counter.get());
self.scope_counter.set(self.scope_counter.get() + 1);
let (node_capacity, hook_capacity) = { //
let heuristics = self.heuristics.borrow(); //
if let Some(heuristic) = heuristics.get(&fc_ptr) { if let Some(old_scope) = self.free_scopes.borrow_mut().pop() {
(heuristic.node_arena_size, heuristic.hook_arena_size) let scope = unsafe { &mut *old_scope };
} else { log::debug!(
(0, 0) "reusing scope {:?} as {:?}",
} scope.our_arena_idx,
}; new_scope_id
);
let mut frames = [BumpFrame::new(node_capacity), BumpFrame::new(node_capacity)]; scope.caller = caller;
scope.parent_scope = parent_scope;
scope.height = height;
scope.subtree = Cell::new(subtree);
scope.our_arena_idx = new_scope_id;
frames[0].nodes.get_mut().push({ scope.frames[0].nodes.get_mut().push({
let vnode = frames[0] let vnode = scope.frames[0]
.bump .bump
.alloc(VNode::Text(frames[0].bump.alloc(VText { .alloc(VNode::Text(scope.frames[0].bump.alloc(VText {
dom_id: Default::default(), dom_id: Default::default(),
is_static: false, is_static: false,
text: "", text: "",
}))); })));
unsafe { std::mem::transmute(vnode as *mut VNode) } unsafe { std::mem::transmute(vnode as *mut VNode) }
}); });
frames[1].nodes.get_mut().push({ scope.frames[1].nodes.get_mut().push({
let vnode = frames[1] let vnode = scope.frames[1]
.bump .bump
.alloc(VNode::Text(frames[1].bump.alloc(VText { .alloc(VNode::Text(scope.frames[1].bump.alloc(VText {
dom_id: Default::default(), dom_id: Default::default(),
is_static: false, is_static: false,
text: "", text: "",
}))); })));
unsafe { std::mem::transmute(vnode as *mut VNode) } unsafe { std::mem::transmute(vnode as *mut VNode) }
}); });
let mut new_scope = Scope { let r = self.scopes.borrow_mut().insert(new_scope_id, scope);
sender: self.sender.clone(),
our_arena_idx: scope_id,
parent_scope,
height,
frames,
subtree: Cell::new(subtree),
is_subtree_root: Cell::new(false),
caller, assert!(r.is_none());
generation: 0.into(),
hooks: HookList::new(hook_capacity), new_scope_id
shared_contexts: Default::default(),
items: RefCell::new(SelfReferentialItems {
listeners: Default::default(),
borrowed_props: Default::default(),
suspended_nodes: Default::default(),
tasks: Default::default(),
pending_effects: Default::default(),
}),
};
if let Some(id) = self.free_scopes.borrow_mut().pop() {
let scope = unsafe { self.get_scope_mut(&id) }.unwrap();
std::mem::swap(&mut new_scope, scope);
id
} else { } else {
let scope = self.bump.alloc(new_scope); let (node_capacity, hook_capacity) = {
self.scopes.borrow_mut().push(scope); let heuristics = self.heuristics.borrow();
scope_id if let Some(heuristic) = heuristics.get(&fc_ptr) {
(heuristic.node_arena_size, heuristic.hook_arena_size)
} else {
(0, 0)
}
};
let mut frames = [BumpFrame::new(node_capacity), BumpFrame::new(node_capacity)];
frames[0].nodes.get_mut().push({
let vnode = frames[0]
.bump
.alloc(VNode::Text(frames[0].bump.alloc(VText {
dom_id: Default::default(),
is_static: false,
text: "",
})));
unsafe { std::mem::transmute(vnode as *mut VNode) }
});
frames[1].nodes.get_mut().push({
let vnode = frames[1]
.bump
.alloc(VNode::Text(frames[1].bump.alloc(VText {
dom_id: Default::default(),
is_static: false,
text: "",
})));
unsafe { std::mem::transmute(vnode as *mut VNode) }
});
let scope = self.bump.alloc(Scope {
sender: self.sender.clone(),
our_arena_idx: new_scope_id,
parent_scope,
height,
frames,
subtree: Cell::new(subtree),
is_subtree_root: Cell::new(false),
caller,
generation: 0.into(),
hooks: HookList::new(hook_capacity),
shared_contexts: Default::default(),
items: RefCell::new(SelfReferentialItems {
listeners: Default::default(),
borrowed_props: Default::default(),
suspended_nodes: Default::default(),
tasks: Default::default(),
pending_effects: Default::default(),
}),
});
dbg!(self.scopes.borrow());
let r = self.scopes.borrow_mut().insert(new_scope_id, scope);
assert!(r.is_none());
// .expect(&format!("scope shouldnt exist, {:?}", new_scope_id));
new_scope_id
} }
} }
pub fn try_remove(&self, id: &ScopeId) -> Option<()> { pub fn try_remove(&self, id: &ScopeId) -> Option<()> {
self.ensure_drop_safety(id); self.ensure_drop_safety(id);
let mut scope = unsafe { &mut *self.get_scope_raw(id)? }; log::debug!("removing scope {:?}", id);
println!("removing scope {:?}", id);
let scope = unsafe { &mut *self.scopes.borrow_mut().remove(&id).unwrap() };
// we're just reusing scopes so we need to clear it out // we're just reusing scopes so we need to clear it out
scope.hooks.clear(); scope.hooks.clear();
@@ -143,6 +193,12 @@ impl ScopeArena {
scope.is_subtree_root.set(false); scope.is_subtree_root.set(false);
scope.subtree.set(0); scope.subtree.set(0);
scope.frames[0].nodes.get_mut().clear();
scope.frames[1].nodes.get_mut().clear();
scope.frames[0].bump.reset();
scope.frames[1].bump.reset();
let SelfReferentialItems { let SelfReferentialItems {
borrowed_props, borrowed_props,
listeners, listeners,
@@ -157,7 +213,8 @@ impl ScopeArena {
suspended_nodes.clear(); suspended_nodes.clear();
tasks.clear(); tasks.clear();
self.free_scopes.borrow_mut().push(*id); self.free_scopes.borrow_mut().push(scope);
Some(()) Some(())
} }
@@ -218,11 +275,9 @@ impl ScopeArena {
} }
pub(crate) fn run_scope(&self, id: &ScopeId) -> bool { pub(crate) fn run_scope(&self, id: &ScopeId) -> bool {
let scope = unsafe { let scope = unsafe { &mut *self.get_scope_mut(id).expect("could not find scope") };
&mut *self
.get_scope_mut(id) log::debug!("found scope, about to run: {:?}", id);
.expect("The base scope should never be moved")
};
// Cycle to the next frame and then reset it // Cycle to the next frame and then reset it
// This breaks any latent references, invalidating every pointer referencing into it. // This breaks any latent references, invalidating every pointer referencing into it.
@@ -285,8 +340,8 @@ impl ScopeArena {
// The head of the bumpframe is the first linked NodeLink // The head of the bumpframe is the first linked NodeLink
pub fn wip_head(&self, id: &ScopeId) -> &VNode { pub fn wip_head(&self, id: &ScopeId) -> &VNode {
let scope = self.get_scope(id).unwrap(); let scope = self.get_scope(id).unwrap();
let wip_frame = scope.wip_frame(); let frame = scope.wip_frame();
let nodes = wip_frame.nodes.borrow(); let nodes = frame.nodes.borrow();
let node: &VNode = unsafe { &**nodes.get(0).unwrap() }; let node: &VNode = unsafe { &**nodes.get(0).unwrap() };
unsafe { std::mem::transmute::<&VNode, &VNode>(node) } unsafe { std::mem::transmute::<&VNode, &VNode>(node) }
} }
@@ -294,8 +349,8 @@ impl ScopeArena {
// The head of the bumpframe is the first linked NodeLink // The head of the bumpframe is the first linked NodeLink
pub fn fin_head(&self, id: &ScopeId) -> &VNode { pub fn fin_head(&self, id: &ScopeId) -> &VNode {
let scope = self.get_scope(id).unwrap(); let scope = self.get_scope(id).unwrap();
let wip_frame = scope.fin_frame(); let frame = scope.fin_frame();
let nodes = wip_frame.nodes.borrow(); let nodes = frame.nodes.borrow();
let node: &VNode = unsafe { &**nodes.get(0).unwrap() }; let node: &VNode = unsafe { &**nodes.get(0).unwrap() };
unsafe { std::mem::transmute::<&VNode, &VNode>(node) } unsafe { std::mem::transmute::<&VNode, &VNode>(node) }
} }
+6 -6
View File
@@ -401,13 +401,13 @@ impl VirtualDom {
if let Some(element) = event.mounted_dom_id { if let Some(element) = event.mounted_dom_id {
log::info!("Calling listener {:?}, {:?}", event.scope_id, element); log::info!("Calling listener {:?}, {:?}", event.scope_id, element);
let scope = self.scopes.get_scope(&event.scope_id).unwrap(); if let Some(scope) = self.scopes.get_scope(&event.scope_id) {
// TODO: bubble properly here
scope.call_listener(event, element);
// TODO: bubble properly here while let Ok(Some(dirty_scope)) = self.receiver.try_next() {
scope.call_listener(event, element); self.pending_messages.push_front(dirty_scope);
}
while let Ok(Some(dirty_scope)) = self.receiver.try_next() {
self.pending_messages.push_front(dirty_scope);
} }
} else { } else {
log::debug!("User event without a targetted ElementId. Not currently supported.\nUnsure how to proceed. {:?}", event); log::debug!("User event without a targetted ElementId. Not currently supported.\nUnsure how to proceed. {:?}", event);
+93
View File
@@ -220,3 +220,96 @@ fn components_generate() {
] ]
); );
} }
#[test]
fn component_swap() {
// simple_logger::init();
static App: FC<()> = |cx, _| {
let mut render_phase = use_state(cx, || 0);
render_phase += 1;
cx.render(match *render_phase {
0 => rsx!(
div {
NavBar {}
Dashboard {}
}
),
1 => rsx!(
div {
NavBar {}
Results {}
}
),
2 => rsx!(
div {
NavBar {}
Dashboard {}
}
),
3 => rsx!(
div {
NavBar {}
Results {}
}
),
4 => rsx!(
div {
NavBar {}
Dashboard {}
}
),
_ => rsx!("blah"),
})
};
static NavBar: FC<()> = |cx, _| {
println!("running navbar");
cx.render(rsx! {
h1 {
"NavBar"
{(0..3).map(|f| rsx!(NavLink {}))}
}
})
};
static NavLink: FC<()> = |cx, _| {
println!("running navlink");
cx.render(rsx! {
h1 {
"NavLink"
}
})
};
static Dashboard: FC<()> = |cx, _| {
println!("running dashboard");
cx.render(rsx! {
div {
"dashboard"
}
})
};
static Results: FC<()> = |cx, _| {
println!("running results");
cx.render(rsx! {
div {
"results"
}
})
};
let mut dom = VirtualDom::new(App);
let edits = dom.rebuild();
dbg!(&edits);
let edits = dom.work_with_deadline(|| false);
dbg!(&edits);
let edits = dom.work_with_deadline(|| false);
dbg!(&edits);
let edits = dom.work_with_deadline(|| false);
dbg!(&edits);
let edits = dom.work_with_deadline(|| false);
dbg!(&edits);
}
+2
View File
@@ -17,6 +17,8 @@ pub(crate) struct ProvidedStateInner<T> {
impl<T> ProvidedStateInner<T> { impl<T> ProvidedStateInner<T> {
pub(crate) fn notify_consumers(&mut self) { pub(crate) fn notify_consumers(&mut self) {
for consumer in self.consumers.iter() { for consumer in self.consumers.iter() {
println!("notifiying {:?}", consumer);
// log::debug("notifiying {:?}", consumer);
(self.notify_any)(*consumer); (self.notify_any)(*consumer);
} }
} }
+8 -14
View File
@@ -61,7 +61,7 @@ impl<R: Routable> RouterService<R> {
/// This hould only be used once per app /// This hould only be used once per app
/// ///
/// You can manually parse the route if you want, but the derived `parse` method on `Routable` will also work just fine /// You can manually parse the route if you want, but the derived `parse` method on `Routable` will also work just fine
pub fn use_router<R: Routable>(cx: Context, cfg: impl FnOnce(&str) -> R) -> Option<R> { pub fn use_router<R: Routable>(cx: Context, cfg: impl FnOnce(&str) -> R) -> Option<&R> {
// for the web, attach to the history api // for the web, attach to the history api
cx.use_hook( cx.use_hook(
|f| { |f| {
@@ -88,30 +88,24 @@ pub fn use_router<R: Routable>(cx: Context, cfg: impl FnOnce(&str) -> R) -> Opti
|f| { |f| {
// //
}, },
|f| {
//
},
); );
let router = use_router_service::<R>(cx)?; todo!()
Some(cfg(router.get_current_route())) // let router = use_router_service::<R>(cx)?;
// Some(cfg(router.get_current_route()))
} }
pub fn use_router_service<R: Routable>(cx: Context) -> Option<&Rc<RouterService<R>>> { pub fn use_router_service<R: Routable>(cx: Context) -> Option<&Rc<RouterService<R>>> {
cx.use_hook( cx.use_hook(|_| cx.consume_state::<RouterService<R>>(), |f| f.as_ref())
|_| cx.consume_state::<RouterService<R>>(),
|f| f.as_ref(),
|f| {},
)
} }
#[derive(Props)] #[derive(Props)]
pub struct LinkProps<'a, R: Routable> { pub struct LinkProps<R: Routable> {
to: R, to: R,
children: ScopeChildren<'a>, children: Element,
} }
pub fn Link<'a, R: Routable>((cx, props): Scope<'a, LinkProps<'a, R>>) -> Element { pub fn Link<'a, R: Routable>(cx: Context, props: &LinkProps<R>) -> Element {
let service = use_router_service::<R>(cx)?; let service = use_router_service::<R>(cx)?;
cx.render(rsx! { cx.render(rsx! {
a { a {
+3
View File
@@ -185,6 +185,9 @@ pub use dioxus_mobile as mobile;
#[cfg(feature = "desktop")] #[cfg(feature = "desktop")]
pub use dioxus_desktop as desktop; pub use dioxus_desktop as desktop;
#[cfg(feature = "router")]
pub use dioxus_router as router;
pub mod debug {} pub mod debug {}
pub mod prelude { pub mod prelude {