|
@@ -19,9 +19,10 @@ pub struct Heuristic {
|
|
|
// has an internal heuristics engine to pre-allocate arenas to the right size
|
|
|
pub(crate) struct ScopeArena {
|
|
|
bump: Bump,
|
|
|
- scopes: RefCell<Vec<*mut Scope>>,
|
|
|
+ scope_counter: Cell<usize>,
|
|
|
+ scopes: RefCell<FxHashMap<ScopeId, *mut Scope>>,
|
|
|
pub heuristics: RefCell<FxHashMap<FcSlot, Heuristic>>,
|
|
|
- free_scopes: RefCell<Vec<ScopeId>>,
|
|
|
+ free_scopes: RefCell<Vec<*mut Scope>>,
|
|
|
nodes: RefCell<Slab<*const VNode<'static>>>,
|
|
|
pub(crate) sender: UnboundedSender<SchedulerMsg>,
|
|
|
}
|
|
@@ -29,8 +30,9 @@ pub(crate) struct ScopeArena {
|
|
|
impl ScopeArena {
|
|
|
pub fn new(sender: UnboundedSender<SchedulerMsg>) -> Self {
|
|
|
Self {
|
|
|
+ scope_counter: Cell::new(0),
|
|
|
bump: Bump::new(),
|
|
|
- scopes: RefCell::new(Vec::new()),
|
|
|
+ scopes: RefCell::new(FxHashMap::default()),
|
|
|
heuristics: RefCell::new(FxHashMap::default()),
|
|
|
free_scopes: RefCell::new(Vec::new()),
|
|
|
nodes: RefCell::new(Slab::new()),
|
|
@@ -39,17 +41,17 @@ impl ScopeArena {
|
|
|
}
|
|
|
|
|
|
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
|
|
|
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
|
|
|
|
|
|
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(
|
|
@@ -60,80 +62,128 @@ impl ScopeArena {
|
|
|
height: u32,
|
|
|
subtree: u32,
|
|
|
) -> ScopeId {
|
|
|
- let scope_id = ScopeId(self.scopes.borrow().len());
|
|
|
-
|
|
|
- let (node_capacity, hook_capacity) = {
|
|
|
- let heuristics = self.heuristics.borrow();
|
|
|
- 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 mut new_scope = 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,
|
|
|
- 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(),
|
|
|
- }),
|
|
|
- };
|
|
|
-
|
|
|
- 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
|
|
|
+ let new_scope_id = ScopeId(self.scope_counter.get());
|
|
|
+ self.scope_counter.set(self.scope_counter.get() + 1);
|
|
|
+
|
|
|
+ //
|
|
|
+ //
|
|
|
+ if let Some(old_scope) = self.free_scopes.borrow_mut().pop() {
|
|
|
+ let scope = unsafe { &mut *old_scope };
|
|
|
+ log::debug!(
|
|
|
+ "reusing scope {:?} as {:?}",
|
|
|
+ scope.our_arena_idx,
|
|
|
+ new_scope_id
|
|
|
+ );
|
|
|
+
|
|
|
+ scope.caller = caller;
|
|
|
+ scope.parent_scope = parent_scope;
|
|
|
+ scope.height = height;
|
|
|
+ scope.subtree = Cell::new(subtree);
|
|
|
+ scope.our_arena_idx = new_scope_id;
|
|
|
+
|
|
|
+ scope.frames[0].nodes.get_mut().push({
|
|
|
+ let vnode = scope.frames[0]
|
|
|
+ .bump
|
|
|
+ .alloc(VNode::Text(scope.frames[0].bump.alloc(VText {
|
|
|
+ dom_id: Default::default(),
|
|
|
+ is_static: false,
|
|
|
+ text: "",
|
|
|
+ })));
|
|
|
+ unsafe { std::mem::transmute(vnode as *mut VNode) }
|
|
|
+ });
|
|
|
+
|
|
|
+ scope.frames[1].nodes.get_mut().push({
|
|
|
+ let vnode = scope.frames[1]
|
|
|
+ .bump
|
|
|
+ .alloc(VNode::Text(scope.frames[1].bump.alloc(VText {
|
|
|
+ dom_id: Default::default(),
|
|
|
+ is_static: false,
|
|
|
+ text: "",
|
|
|
+ })));
|
|
|
+ unsafe { std::mem::transmute(vnode as *mut VNode) }
|
|
|
+ });
|
|
|
+
|
|
|
+ let r = self.scopes.borrow_mut().insert(new_scope_id, scope);
|
|
|
+
|
|
|
+ assert!(r.is_none());
|
|
|
+
|
|
|
+ new_scope_id
|
|
|
} else {
|
|
|
- let scope = self.bump.alloc(new_scope);
|
|
|
- self.scopes.borrow_mut().push(scope);
|
|
|
- scope_id
|
|
|
+ let (node_capacity, hook_capacity) = {
|
|
|
+ let heuristics = self.heuristics.borrow();
|
|
|
+ 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<()> {
|
|
|
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
|
|
|
scope.hooks.clear();
|
|
@@ -143,6 +193,12 @@ impl ScopeArena {
|
|
|
scope.is_subtree_root.set(false);
|
|
|
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 {
|
|
|
borrowed_props,
|
|
|
listeners,
|
|
@@ -157,7 +213,8 @@ impl ScopeArena {
|
|
|
suspended_nodes.clear();
|
|
|
tasks.clear();
|
|
|
|
|
|
- self.free_scopes.borrow_mut().push(*id);
|
|
|
+ self.free_scopes.borrow_mut().push(scope);
|
|
|
+
|
|
|
Some(())
|
|
|
}
|
|
|
|
|
@@ -218,11 +275,9 @@ impl ScopeArena {
|
|
|
}
|
|
|
|
|
|
pub(crate) fn run_scope(&self, id: &ScopeId) -> bool {
|
|
|
- let scope = unsafe {
|
|
|
- &mut *self
|
|
|
- .get_scope_mut(id)
|
|
|
- .expect("The base scope should never be moved")
|
|
|
- };
|
|
|
+ let scope = unsafe { &mut *self.get_scope_mut(id).expect("could not find scope") };
|
|
|
+
|
|
|
+ log::debug!("found scope, about to run: {:?}", id);
|
|
|
|
|
|
// Cycle to the next frame and then reset 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
|
|
|
pub fn wip_head(&self, id: &ScopeId) -> &VNode {
|
|
|
let scope = self.get_scope(id).unwrap();
|
|
|
- let wip_frame = scope.wip_frame();
|
|
|
- let nodes = wip_frame.nodes.borrow();
|
|
|
+ let frame = scope.wip_frame();
|
|
|
+ let nodes = frame.nodes.borrow();
|
|
|
let node: &VNode = unsafe { &**nodes.get(0).unwrap() };
|
|
|
unsafe { std::mem::transmute::<&VNode, &VNode>(node) }
|
|
|
}
|
|
@@ -294,8 +349,8 @@ impl ScopeArena {
|
|
|
// 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 wip_frame = scope.fin_frame();
|
|
|
- let nodes = wip_frame.nodes.borrow();
|
|
|
+ let frame = scope.fin_frame();
|
|
|
+ let nodes = frame.nodes.borrow();
|
|
|
let node: &VNode = unsafe { &**nodes.get(0).unwrap() };
|
|
|
unsafe { std::mem::transmute::<&VNode, &VNode>(node) }
|
|
|
}
|