|
@@ -1,8 +1,9 @@
|
|
-use crate::innerlude::*;
|
|
|
|
-
|
|
|
|
use futures_channel::mpsc::UnboundedSender;
|
|
use futures_channel::mpsc::UnboundedSender;
|
|
|
|
+use fxhash::{FxHashMap, FxHashSet};
|
|
|
|
+use slab::Slab;
|
|
use std::{
|
|
use std::{
|
|
any::{Any, TypeId},
|
|
any::{Any, TypeId},
|
|
|
|
+ borrow::Borrow,
|
|
cell::{Cell, RefCell},
|
|
cell::{Cell, RefCell},
|
|
collections::HashMap,
|
|
collections::HashMap,
|
|
future::Future,
|
|
future::Future,
|
|
@@ -10,8 +11,351 @@ use std::{
|
|
rc::Rc,
|
|
rc::Rc,
|
|
};
|
|
};
|
|
|
|
|
|
|
|
+use crate::{innerlude::*, unsafe_utils::extend_vnode};
|
|
use bumpalo::{boxed::Box as BumpBox, Bump};
|
|
use bumpalo::{boxed::Box as BumpBox, Bump};
|
|
|
|
|
|
|
|
+pub(crate) type FcSlot = *const ();
|
|
|
|
+
|
|
|
|
+pub(crate) struct Heuristic {
|
|
|
|
+ hook_arena_size: usize,
|
|
|
|
+ node_arena_size: usize,
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+// a slab-like arena with stable references even when new scopes are allocated
|
|
|
|
+// uses a bump arena as a backing
|
|
|
|
+//
|
|
|
|
+// has an internal heuristics engine to pre-allocate arenas to the right size
|
|
|
|
+pub(crate) struct ScopeArena {
|
|
|
|
+ pub pending_futures: RefCell<FxHashSet<ScopeId>>,
|
|
|
|
+ pub scope_counter: Cell<usize>,
|
|
|
|
+ pub sender: UnboundedSender<SchedulerMsg>,
|
|
|
|
+ pub bump: Bump,
|
|
|
|
+ pub scopes: RefCell<FxHashMap<ScopeId, *mut ScopeState>>,
|
|
|
|
+ pub heuristics: RefCell<FxHashMap<FcSlot, Heuristic>>,
|
|
|
|
+ pub free_scopes: RefCell<Vec<*mut ScopeState>>,
|
|
|
|
+ pub nodes: RefCell<Slab<*const VNode<'static>>>,
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+impl ScopeArena {
|
|
|
|
+ pub(crate) fn new(sender: UnboundedSender<SchedulerMsg>) -> Self {
|
|
|
|
+ let bump = Bump::new();
|
|
|
|
+
|
|
|
|
+ // allocate a container for the root element
|
|
|
|
+ // this will *never* show up in the diffing process
|
|
|
|
+ // todo: figure out why this is necessary. i forgot. whoops.
|
|
|
|
+ let el = bump.alloc(VElement {
|
|
|
|
+ tag_name: "root",
|
|
|
|
+ namespace: None,
|
|
|
|
+ key: None,
|
|
|
|
+ dom_id: Cell::new(Some(ElementId(0))),
|
|
|
|
+ parent_id: Default::default(),
|
|
|
|
+ listeners: &[],
|
|
|
|
+ attributes: &[],
|
|
|
|
+ children: &[],
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ let node = bump.alloc(VNode::Element(el));
|
|
|
|
+ let mut nodes = Slab::new();
|
|
|
|
+ let root_id = nodes.insert(unsafe { std::mem::transmute(node as *const _) });
|
|
|
|
+
|
|
|
|
+ debug_assert_eq!(root_id, 0);
|
|
|
|
+
|
|
|
|
+ Self {
|
|
|
|
+ scope_counter: Cell::new(0),
|
|
|
|
+ bump,
|
|
|
|
+ pending_futures: RefCell::new(FxHashSet::default()),
|
|
|
|
+ scopes: RefCell::new(FxHashMap::default()),
|
|
|
|
+ heuristics: RefCell::new(FxHashMap::default()),
|
|
|
|
+ free_scopes: RefCell::new(Vec::new()),
|
|
|
|
+ nodes: RefCell::new(nodes),
|
|
|
|
+ sender,
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /// Safety:
|
|
|
|
+ /// - Obtaining a mutable refernece to any Scope is unsafe
|
|
|
|
+ /// - Scopes use interior mutability when sharing data into components
|
|
|
|
+ pub(crate) fn get_scope(&self, id: ScopeId) -> Option<&ScopeState> {
|
|
|
|
+ unsafe { self.scopes.borrow().get(&id).map(|f| &**f) }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ pub(crate) fn get_scope_raw(&self, id: ScopeId) -> Option<*mut ScopeState> {
|
|
|
|
+ self.scopes.borrow().get(&id).copied()
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ pub(crate) fn new_with_key(
|
|
|
|
+ &self,
|
|
|
|
+ fc_ptr: *const (),
|
|
|
|
+ vcomp: Box<dyn AnyProps>,
|
|
|
|
+ parent_scope: Option<ScopeId>,
|
|
|
|
+ container: ElementId,
|
|
|
|
+ subtree: u32,
|
|
|
|
+ ) -> ScopeId {
|
|
|
|
+ let new_scope_id = ScopeId(self.scope_counter.get());
|
|
|
|
+ self.scope_counter.set(self.scope_counter.get() + 1);
|
|
|
|
+ let parent_scope = parent_scope.map(|f| self.get_scope_raw(f)).flatten();
|
|
|
|
+ let height = parent_scope
|
|
|
|
+ .as_ref()
|
|
|
|
+ .map(|f| unsafe { &**f })
|
|
|
|
+ .map(|f| f.height + 1)
|
|
|
|
+ .unwrap_or_default();
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ This scopearena aggressively reuse old scopes when possible.
|
|
|
|
+ We try to minimize the new allocations for props/arenas.
|
|
|
|
+
|
|
|
|
+ However, this will probably lead to some sort of fragmentation.
|
|
|
|
+ I'm not exactly sure how to improve this today.
|
|
|
|
+ */
|
|
|
|
+ match self.free_scopes.borrow_mut().pop() {
|
|
|
|
+ // No free scope, make a new scope
|
|
|
|
+ None => {
|
|
|
|
+ let (node_capacity, hook_capacity) = self
|
|
|
|
+ .heuristics
|
|
|
|
+ .borrow()
|
|
|
|
+ .get(&fc_ptr)
|
|
|
|
+ .map(|h| (h.node_arena_size, h.hook_arena_size))
|
|
|
|
+ .unwrap_or_default();
|
|
|
|
+
|
|
|
|
+ let frames = [BumpFrame::new(node_capacity), BumpFrame::new(node_capacity)];
|
|
|
|
+
|
|
|
|
+ let scope = self.bump.alloc(ScopeState {
|
|
|
|
+ sender: self.sender.clone(),
|
|
|
|
+ container,
|
|
|
|
+ our_arena_idx: new_scope_id,
|
|
|
|
+ parent_scope,
|
|
|
|
+ height,
|
|
|
|
+ frames,
|
|
|
|
+ subtree: Cell::new(subtree),
|
|
|
|
+ is_subtree_root: Cell::new(false),
|
|
|
|
+
|
|
|
|
+ props: vcomp,
|
|
|
|
+ generation: 0.into(),
|
|
|
|
+
|
|
|
|
+ shared_contexts: Default::default(),
|
|
|
|
+
|
|
|
|
+ items: RefCell::new(SelfReferentialItems {
|
|
|
|
+ listeners: Default::default(),
|
|
|
|
+ borrowed_props: Default::default(),
|
|
|
|
+ tasks: Default::default(),
|
|
|
|
+ }),
|
|
|
|
+
|
|
|
|
+ hook_arena: Bump::new(),
|
|
|
|
+ hook_vals: RefCell::new(Vec::with_capacity(hook_capacity)),
|
|
|
|
+ hook_idx: Default::default(),
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ let any_item = self.scopes.borrow_mut().insert(new_scope_id, scope);
|
|
|
|
+ debug_assert!(any_item.is_none());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // Reuse a free scope
|
|
|
|
+ Some(old_scope) => {
|
|
|
|
+ let scope = unsafe { &mut *old_scope };
|
|
|
|
+ scope.props.set(vcomp);
|
|
|
|
+ scope.parent_scope = parent_scope;
|
|
|
|
+ scope.height = height;
|
|
|
|
+ scope.subtree = Cell::new(subtree);
|
|
|
|
+ scope.our_arena_idx = new_scope_id;
|
|
|
|
+ scope.container = container;
|
|
|
|
+ let any_item = self.scopes.borrow_mut().insert(new_scope_id, scope);
|
|
|
|
+ debug_assert!(any_item.is_none());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ new_scope_id
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // Removes a scope and its descendents from the arena
|
|
|
|
+ pub fn try_remove(&self, id: ScopeId) -> Option<()> {
|
|
|
|
+ self.ensure_drop_safety(id);
|
|
|
|
+
|
|
|
|
+ // Safety:
|
|
|
|
+ // - ensure_drop_safety ensures that no references to this scope are in use
|
|
|
|
+ // - this raw pointer is removed from the map
|
|
|
|
+ let scope = unsafe { &mut *self.scopes.borrow_mut().remove(&id).unwrap() };
|
|
|
|
+ scope.reset();
|
|
|
|
+
|
|
|
|
+ todo!("drop");
|
|
|
|
+ // unsafe { &*scope.render.get() }.release();
|
|
|
|
+
|
|
|
|
+ self.free_scopes.borrow_mut().push(scope);
|
|
|
|
+
|
|
|
|
+ Some(())
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ pub fn reserve_node<'a>(&self, node: &'a VNode<'a>) -> ElementId {
|
|
|
|
+ let mut els = self.nodes.borrow_mut();
|
|
|
|
+ let entry = els.vacant_entry();
|
|
|
|
+ let key = entry.key();
|
|
|
|
+ let id = ElementId(key);
|
|
|
|
+ let node = unsafe { extend_vnode(node) };
|
|
|
|
+ entry.insert(node as *const _);
|
|
|
|
+ id
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ 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;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ pub fn collect_garbage(&self, id: ElementId) {
|
|
|
|
+ self.nodes.borrow_mut().remove(id.0);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /// This method cleans up any references to data held within our hook list. This prevents mutable aliasing from
|
|
|
|
+ /// causing UB in our tree.
|
|
|
|
+ ///
|
|
|
|
+ /// This works by cleaning up our references from the bottom of the tree to the top. The directed graph of components
|
|
|
|
+ /// essentially forms a dependency tree that we can traverse from the bottom to the top. As we traverse, we remove
|
|
|
|
+ /// any possible references to the data in the hook list.
|
|
|
|
+ ///
|
|
|
|
+ /// References to hook data can only be stored in listeners and component props. During diffing, we make sure to log
|
|
|
|
+ /// all listeners and borrowed props so we can clear them here.
|
|
|
|
+ ///
|
|
|
|
+ /// 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) {
|
|
|
|
+ if let Some(scope) = self.get_scope(scope_id) {
|
|
|
|
+ let mut items = scope.items.borrow_mut();
|
|
|
|
+
|
|
|
|
+ // make sure we drop all borrowed props manually to guarantee that their drop implementation is called before we
|
|
|
|
+ // run the hooks (which hold an &mut Reference)
|
|
|
|
+ // recursively call ensure_drop_safety on all children
|
|
|
|
+ items.borrowed_props.drain(..).for_each(|comp| {
|
|
|
|
+ let scope_id = comp
|
|
|
|
+ .scope
|
|
|
|
+ .get()
|
|
|
|
+ .expect("VComponents should be associated with a valid Scope");
|
|
|
|
+
|
|
|
|
+ self.ensure_drop_safety(scope_id);
|
|
|
|
+
|
|
|
|
+ let g = comp.props.take();
|
|
|
|
+ // comp.props.release();
|
|
|
|
+ });
|
|
|
|
+
|
|
|
|
+ // Now that all the references are gone, we can safely drop our own references in our listeners.
|
|
|
|
+ items
|
|
|
|
+ .listeners
|
|
|
|
+ .drain(..)
|
|
|
|
+ .for_each(|listener| drop(listener.callback.callback.borrow_mut().take()));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ pub(crate) fn run_scope(&self, id: ScopeId) {
|
|
|
|
+ // 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
|
|
|
|
+ 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") };
|
|
|
|
+
|
|
|
|
+ // 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
|
|
|
|
+ scope.hook_idx.set(0);
|
|
|
|
+
|
|
|
|
+ // book keeping to ensure safety around the borrowed data
|
|
|
|
+ {
|
|
|
|
+ // Safety:
|
|
|
|
+ // - We've dropped all references to the wip bump frame with "ensure_drop_safety"
|
|
|
|
+ unsafe { scope.reset_wip_frame() };
|
|
|
|
+
|
|
|
|
+ let mut items = scope.items.borrow_mut();
|
|
|
|
+
|
|
|
|
+ // just forget about our suspended nodes while we're at it
|
|
|
|
+ items.tasks.clear();
|
|
|
|
+
|
|
|
|
+ // guarantee that we haven't screwed up - there should be no latent references anywhere
|
|
|
|
+ debug_assert!(items.listeners.is_empty());
|
|
|
|
+ debug_assert!(items.borrowed_props.is_empty());
|
|
|
|
+ debug_assert!(items.tasks.is_empty());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // safety: this is definitely not dropped
|
|
|
|
+
|
|
|
|
+ /*
|
|
|
|
+ If the component returns None, then we fill in a placeholder node. This will wipe what was there.
|
|
|
|
+ An alternate approach is to leave the Real Dom the same, but that can lead to safety issues and a lot more checks.
|
|
|
|
+
|
|
|
|
+ Instead, we just treat the `None` as a shortcut to placeholder.
|
|
|
|
+ If the developer wants to prevent a scope from updating, they should control its memoization instead.
|
|
|
|
+
|
|
|
|
+ Also, the way we implement hooks allows us to cut rendering short before the next hook is recalled.
|
|
|
|
+ I'm not sure if React lets you abort the component early, but we let you do that.
|
|
|
|
+ */
|
|
|
|
+ if let Some(node) = scope.props.render(scope) {
|
|
|
|
+ if !scope.items.borrow().tasks.is_empty() {
|
|
|
|
+ self.pending_futures.borrow_mut().insert(id);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ let frame = scope.wip_frame();
|
|
|
|
+ let node = frame.bump.alloc(node);
|
|
|
|
+ frame.node.set(unsafe { extend_vnode(node) });
|
|
|
|
+ } else {
|
|
|
|
+ let frame = scope.wip_frame();
|
|
|
|
+ let node = frame
|
|
|
|
+ .bump
|
|
|
|
+ .alloc(VNode::Placeholder(frame.bump.alloc(VPlaceholder {
|
|
|
|
+ dom_id: Default::default(),
|
|
|
|
+ })));
|
|
|
|
+ frame.node.set(unsafe { extend_vnode(node) });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // make the "wip frame" contents the "finished frame"
|
|
|
|
+ // any future dipping into completed nodes after "render" will go through "fin head"
|
|
|
|
+ scope.cycle_frame();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ pub fn call_listener_with_bubbling(&self, event: UserEvent, element: ElementId) {
|
|
|
|
+ let nodes = self.nodes.borrow();
|
|
|
|
+ let mut cur_el = Some(element);
|
|
|
|
+
|
|
|
|
+ while let Some(id) = cur_el.take() {
|
|
|
|
+ if let Some(el) = nodes.get(id.0) {
|
|
|
|
+ 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 {
|
|
|
|
+ let mut cb = listener.callback.callback.borrow_mut();
|
|
|
|
+ if let Some(cb) = cb.as_mut() {
|
|
|
|
+ // todo: arcs are pretty heavy to clone
|
|
|
|
+ // we really want to convert arc to rc
|
|
|
|
+ // unfortunately, the SchedulerMsg must be send/sync to be sent across threads
|
|
|
|
+ // we could convert arc to rc internally or something
|
|
|
|
+ (cb)(event.data.clone());
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ cur_el = real_el.parent_id.get();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // The head of the bumpframe is the first linked NodeLink
|
|
|
|
+ pub fn wip_head(&self, id: ScopeId) -> &VNode {
|
|
|
|
+ let scope = self.get_scope(id).unwrap();
|
|
|
|
+ let frame = scope.wip_frame();
|
|
|
|
+ let node = unsafe { &*frame.node.get() };
|
|
|
|
+ unsafe { extend_vnode(node) }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // The head of the bumpframe is the first linked NodeLink
|
|
|
|
+ pub fn fin_head(&self, id: ScopeId) -> &VNode {
|
|
|
|
+ let scope = self.get_scope(id).unwrap();
|
|
|
|
+ let frame = scope.fin_frame();
|
|
|
|
+ let node = unsafe { &*frame.node.get() };
|
|
|
|
+ unsafe { extend_vnode(node) }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ pub fn root_node(&self, id: ScopeId) -> &VNode {
|
|
|
|
+ self.fin_head(id)
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
/// Components in Dioxus use the "Context" object to interact with their lifecycle.
|
|
/// Components in Dioxus use the "Context" object to interact with their lifecycle.
|
|
///
|
|
///
|
|
/// This lets components access props, schedule updates, integrate hooks, and expose shared state.
|
|
/// This lets components access props, schedule updates, integrate hooks, and expose shared state.
|
|
@@ -72,34 +416,29 @@ pub struct ScopeId(pub usize);
|
|
/// use case they might have.
|
|
/// use case they might have.
|
|
pub struct ScopeState {
|
|
pub struct ScopeState {
|
|
pub(crate) parent_scope: Option<*mut ScopeState>,
|
|
pub(crate) parent_scope: Option<*mut ScopeState>,
|
|
-
|
|
|
|
pub(crate) container: ElementId,
|
|
pub(crate) container: ElementId,
|
|
-
|
|
|
|
pub(crate) our_arena_idx: ScopeId,
|
|
pub(crate) our_arena_idx: ScopeId,
|
|
-
|
|
|
|
pub(crate) height: u32,
|
|
pub(crate) height: u32,
|
|
|
|
+ pub(crate) sender: UnboundedSender<SchedulerMsg>,
|
|
|
|
|
|
- pub(crate) subtree: Cell<u32>,
|
|
|
|
-
|
|
|
|
|
|
+ // todo: subtrees
|
|
pub(crate) is_subtree_root: Cell<bool>,
|
|
pub(crate) is_subtree_root: Cell<bool>,
|
|
|
|
+ pub(crate) subtree: Cell<u32>,
|
|
|
|
|
|
- pub(crate) generation: Cell<u32>,
|
|
|
|
|
|
+ pub(crate) props: Option<RefCell<Box<dyn AnyProps>>>,
|
|
|
|
|
|
|
|
+ // nodes, items
|
|
pub(crate) frames: [BumpFrame; 2],
|
|
pub(crate) frames: [BumpFrame; 2],
|
|
-
|
|
|
|
- pub(crate) caller: Cell<*const dyn Fn(&ScopeState) -> Element>,
|
|
|
|
-
|
|
|
|
|
|
+ pub(crate) generation: Cell<u32>,
|
|
pub(crate) items: RefCell<SelfReferentialItems<'static>>,
|
|
pub(crate) items: RefCell<SelfReferentialItems<'static>>,
|
|
|
|
|
|
|
|
+ // hooks
|
|
pub(crate) hook_arena: Bump,
|
|
pub(crate) hook_arena: Bump,
|
|
-
|
|
|
|
pub(crate) hook_vals: RefCell<Vec<*mut dyn Any>>,
|
|
pub(crate) hook_vals: RefCell<Vec<*mut dyn Any>>,
|
|
-
|
|
|
|
pub(crate) hook_idx: Cell<usize>,
|
|
pub(crate) hook_idx: Cell<usize>,
|
|
|
|
|
|
|
|
+ // shared state -> todo: move this out of scopestate
|
|
pub(crate) shared_contexts: RefCell<HashMap<TypeId, Rc<dyn Any>>>,
|
|
pub(crate) shared_contexts: RefCell<HashMap<TypeId, Rc<dyn Any>>>,
|
|
-
|
|
|
|
- pub(crate) sender: UnboundedSender<SchedulerMsg>,
|
|
|
|
}
|
|
}
|
|
|
|
|
|
pub struct SelfReferentialItems<'a> {
|
|
pub struct SelfReferentialItems<'a> {
|
|
@@ -143,7 +482,6 @@ impl ScopeState {
|
|
///
|
|
///
|
|
/// ```rust, ignore
|
|
/// ```rust, ignore
|
|
/// fn App(cx: Scope<()>) -> Element {
|
|
/// fn App(cx: Scope<()>) -> Element {
|
|
- /// todo!();
|
|
|
|
/// rsx!(cx, div { "Subtree {id}"})
|
|
/// rsx!(cx, div { "Subtree {id}"})
|
|
/// };
|
|
/// };
|
|
/// ```
|
|
/// ```
|
|
@@ -251,7 +589,7 @@ impl ScopeState {
|
|
|
|
|
|
/// Get the Root Node of this scope
|
|
/// Get the Root Node of this scope
|
|
pub fn root_node(&self) -> &VNode {
|
|
pub fn root_node(&self) -> &VNode {
|
|
- let node = unsafe { &*self.wip_frame().node.get() };
|
|
|
|
|
|
+ let node = unsafe { &*self.fin_frame().node.get() };
|
|
unsafe { std::mem::transmute(node) }
|
|
unsafe { std::mem::transmute(node) }
|
|
}
|
|
}
|
|
|
|
|
|
@@ -465,45 +803,37 @@ impl ScopeState {
|
|
&self.wip_frame().bump
|
|
&self.wip_frame().bump
|
|
}
|
|
}
|
|
|
|
|
|
- pub(crate) fn drop_hooks(&mut self) {
|
|
|
|
- self.hook_vals.get_mut().drain(..).for_each(|state| {
|
|
|
|
- let as_mut = unsafe { &mut *state };
|
|
|
|
- let boxed = unsafe { bumpalo::boxed::Box::from_raw(as_mut) };
|
|
|
|
- drop(boxed);
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
|
|
+ // todo: disable bookkeeping on drop (unncessary)
|
|
pub(crate) fn reset(&mut self) {
|
|
pub(crate) fn reset(&mut self) {
|
|
- // we're just reusing scopes so we need to clear it out
|
|
|
|
- self.drop_hooks();
|
|
|
|
-
|
|
|
|
|
|
+ // first: book keaping
|
|
self.hook_idx.set(0);
|
|
self.hook_idx.set(0);
|
|
-
|
|
|
|
- self.hook_arena.reset();
|
|
|
|
- self.shared_contexts.get_mut().clear();
|
|
|
|
self.parent_scope = None;
|
|
self.parent_scope = None;
|
|
self.generation.set(0);
|
|
self.generation.set(0);
|
|
self.is_subtree_root.set(false);
|
|
self.is_subtree_root.set(false);
|
|
self.subtree.set(0);
|
|
self.subtree.set(0);
|
|
|
|
|
|
- self.frames[0].reset();
|
|
|
|
- self.frames[1].reset();
|
|
|
|
|
|
+ // next: shared context data
|
|
|
|
+ self.shared_contexts.get_mut().clear();
|
|
|
|
|
|
|
|
+ // next: reset the node data
|
|
let SelfReferentialItems {
|
|
let SelfReferentialItems {
|
|
borrowed_props,
|
|
borrowed_props,
|
|
listeners,
|
|
listeners,
|
|
tasks,
|
|
tasks,
|
|
} = self.items.get_mut();
|
|
} = self.items.get_mut();
|
|
-
|
|
|
|
borrowed_props.clear();
|
|
borrowed_props.clear();
|
|
listeners.clear();
|
|
listeners.clear();
|
|
tasks.clear();
|
|
tasks.clear();
|
|
- }
|
|
|
|
-}
|
|
|
|
|
|
+ self.frames[0].reset();
|
|
|
|
+ self.frames[1].reset();
|
|
|
|
|
|
-impl Drop for ScopeState {
|
|
|
|
- fn drop(&mut self) {
|
|
|
|
- self.drop_hooks();
|
|
|
|
|
|
+ // Finally, free up the hook values
|
|
|
|
+ self.hook_arena.reset();
|
|
|
|
+ self.hook_vals.get_mut().drain(..).for_each(|state| {
|
|
|
|
+ let as_mut = unsafe { &mut *state };
|
|
|
|
+ let boxed = unsafe { bumpalo::boxed::Box::from_raw(as_mut) };
|
|
|
|
+ drop(boxed);
|
|
|
|
+ });
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|