scopearena.rs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. use slab::Slab;
  2. use std::cell::{Cell, RefCell};
  3. use bumpalo::{boxed::Box as BumpBox, Bump};
  4. use futures_channel::mpsc::UnboundedSender;
  5. use crate::innerlude::*;
  6. pub type FcSlot = *const ();
  7. // pub heuristics: FxHashMap<FcSlot, Heuristic>,
  8. pub struct Heuristic {
  9. hook_arena_size: usize,
  10. node_arena_size: usize,
  11. }
  12. // a slab-like arena with stable references even when new scopes are allocated
  13. // uses a bump arena as a backing
  14. //
  15. // has an internal heuristics engine to pre-allocate arenas to the right size
  16. pub(crate) struct ScopeArena {
  17. bump: Bump,
  18. scopes: RefCell<Vec<*mut Scope>>,
  19. free_scopes: RefCell<Vec<ScopeId>>,
  20. nodes: RefCell<Slab<*const VNode<'static>>>,
  21. pub(crate) sender: UnboundedSender<SchedulerMsg>,
  22. }
  23. impl ScopeArena {
  24. pub fn new(sender: UnboundedSender<SchedulerMsg>) -> Self {
  25. Self {
  26. bump: Bump::new(),
  27. scopes: RefCell::new(Vec::new()),
  28. free_scopes: RefCell::new(Vec::new()),
  29. nodes: RefCell::new(Slab::new()),
  30. sender,
  31. }
  32. }
  33. pub fn get_scope(&self, id: &ScopeId) -> Option<&Scope> {
  34. unsafe { Some(&*self.scopes.borrow()[id.0]) }
  35. }
  36. // this is unsafe
  37. pub unsafe fn get_scope_mut(&self, id: &ScopeId) -> Option<*mut Scope> {
  38. Some(self.scopes.borrow()[id.0])
  39. }
  40. pub fn new_with_key(
  41. &self,
  42. fc_ptr: *const (),
  43. caller: *const dyn Fn(&Scope) -> Element,
  44. parent_scope: Option<*mut Scope>,
  45. height: u32,
  46. subtree: u32,
  47. ) -> ScopeId {
  48. if let Some(id) = self.free_scopes.borrow_mut().pop() {
  49. // have already called drop on it - the slot is still chillin tho
  50. // let scope = unsafe { &mut *self.scopes.borrow()[id.0 as usize] };
  51. todo!("override the scope contents");
  52. id
  53. } else {
  54. let scope_id = ScopeId(self.scopes.borrow().len());
  55. let old_root = NodeLink {
  56. link_idx: 0,
  57. gen_id: 0,
  58. scope_id,
  59. };
  60. let new_root = NodeLink {
  61. link_idx: 0,
  62. gen_id: 0,
  63. scope_id,
  64. };
  65. let new_scope = Scope {
  66. sender: self.sender.clone(),
  67. parent_scope,
  68. our_arena_idx: scope_id,
  69. height,
  70. subtree: Cell::new(subtree),
  71. is_subtree_root: Cell::new(false),
  72. frames: [BumpFrame::new(), BumpFrame::new()],
  73. hooks: Default::default(),
  74. shared_contexts: Default::default(),
  75. caller,
  76. generation: 0.into(),
  77. old_root: RefCell::new(Some(old_root)),
  78. new_root: RefCell::new(Some(new_root)),
  79. items: RefCell::new(SelfReferentialItems {
  80. listeners: Default::default(),
  81. borrowed_props: Default::default(),
  82. suspended_nodes: Default::default(),
  83. tasks: Default::default(),
  84. pending_effects: Default::default(),
  85. }),
  86. };
  87. let stable = self.bump.alloc(new_scope);
  88. self.scopes.borrow_mut().push(stable);
  89. scope_id
  90. }
  91. }
  92. pub fn try_remove(&self, id: &ScopeId) -> Option<Scope> {
  93. todo!()
  94. }
  95. pub fn reserve_node(&self, node: &VNode) -> ElementId {
  96. let mut els = self.nodes.borrow_mut();
  97. let entry = els.vacant_entry();
  98. let key = entry.key();
  99. let id = ElementId(key);
  100. let node: *const VNode = node as *const _;
  101. let node = unsafe { std::mem::transmute::<*const VNode, *const VNode>(node) };
  102. entry.insert(node);
  103. id
  104. // let nodes = self.nodes.borrow_mut();
  105. // let id = nodes.insert(());
  106. // let node_id = ElementId(id);
  107. // node = Some(node_id);
  108. // node_id
  109. }
  110. pub fn collect_garbage(&self, id: ElementId) {
  111. todo!()
  112. }
  113. // These methods would normally exist on `scope` but they need access to *all* of the scopes
  114. /// This method cleans up any references to data held within our hook list. This prevents mutable aliasing from
  115. /// causing UB in our tree.
  116. ///
  117. /// This works by cleaning up our references from the bottom of the tree to the top. The directed graph of components
  118. /// essentially forms a dependency tree that we can traverse from the bottom to the top. As we traverse, we remove
  119. /// any possible references to the data in the hook list.
  120. ///
  121. /// References to hook data can only be stored in listeners and component props. During diffing, we make sure to log
  122. /// all listeners and borrowed props so we can clear them here.
  123. ///
  124. /// This also makes sure that drop order is consistent and predictable. All resources that rely on being dropped will
  125. /// be dropped.
  126. pub(crate) fn ensure_drop_safety(&self, scope_id: &ScopeId) {
  127. let scope = self.get_scope(scope_id).unwrap();
  128. // make sure we drop all borrowed props manually to guarantee that their drop implementation is called before we
  129. // run the hooks (which hold an &mut Reference)
  130. // right now, we don't drop
  131. scope
  132. .items
  133. .borrow_mut()
  134. .borrowed_props
  135. .drain(..)
  136. .for_each(|comp| {
  137. // First drop the component's undropped references
  138. let scope_id = comp
  139. .associated_scope
  140. .get()
  141. .expect("VComponents should be associated with a valid Scope");
  142. todo!("move this onto virtualdom");
  143. // let scope = unsafe { &mut *scope_id };
  144. // scope.ensure_drop_safety();
  145. todo!("drop the component's props");
  146. // let mut drop_props = comp.drop_props.borrow_mut().take().unwrap();
  147. // drop_props();
  148. });
  149. // Now that all the references are gone, we can safely drop our own references in our listeners.
  150. scope
  151. .items
  152. .borrow_mut()
  153. .listeners
  154. .drain(..)
  155. .for_each(|listener| drop(listener.callback.borrow_mut().take()));
  156. }
  157. pub(crate) fn run_scope(&self, id: &ScopeId) -> bool {
  158. let scope = self
  159. .get_scope(id)
  160. .expect("The base scope should never be moved");
  161. // Cycle to the next frame and then reset it
  162. // This breaks any latent references, invalidating every pointer referencing into it.
  163. // Remove all the outdated listeners
  164. self.ensure_drop_safety(id);
  165. // Safety:
  166. // - We dropped the listeners, so no more &mut T can be used while these are held
  167. // - All children nodes that rely on &mut T are replaced with a new reference
  168. unsafe { scope.hooks.reset() };
  169. // Safety:
  170. // - We've dropped all references to the wip bump frame with "ensure_drop_safety"
  171. unsafe { scope.reset_wip_frame() };
  172. {
  173. let mut items = scope.items.borrow_mut();
  174. // just forget about our suspended nodes while we're at it
  175. items.suspended_nodes.clear();
  176. // guarantee that we haven't screwed up - there should be no latent references anywhere
  177. debug_assert!(items.listeners.is_empty());
  178. debug_assert!(items.suspended_nodes.is_empty());
  179. debug_assert!(items.borrowed_props.is_empty());
  180. log::debug!("Borrowed stuff is successfully cleared");
  181. // temporarily cast the vcomponent to the right lifetime
  182. // let vcomp = scope.load_vcomp();
  183. }
  184. let render: &dyn Fn(&Scope) -> Element = unsafe { &*scope.caller };
  185. // Todo: see if we can add stronger guarantees around internal bookkeeping and failed component renders.
  186. scope.wip_frame().nodes.borrow_mut().clear();
  187. if let Some(key) = render(scope) {
  188. dbg!(key);
  189. dbg!(&scope.wip_frame().nodes.borrow_mut());
  190. // let mut old = scope.old_root.borrow_mut();
  191. // let mut new = scope.new_root.borrow_mut();
  192. // let new_old = new.clone();
  193. // *old = new_old;
  194. // *new = Some(key);
  195. // dbg!(&old);
  196. // dbg!(&new);
  197. // the user's component succeeded. We can safely cycle to the next frame
  198. // scope.frames.wip_frame_mut().head_node = unsafe { std::mem::transmute(new_head) };
  199. scope.cycle_frame();
  200. true
  201. } else {
  202. false
  203. }
  204. }
  205. pub fn wip_head(&self, id: &ScopeId) -> &VNode {
  206. let scope = self.get_scope(id).unwrap();
  207. let wip_frame = scope.wip_frame();
  208. let nodes = wip_frame.nodes.borrow();
  209. let node = nodes.get(0).unwrap();
  210. unsafe { std::mem::transmute::<&VNode, &VNode>(node) }
  211. }
  212. pub fn fin_head(&self, id: &ScopeId) -> &VNode {
  213. let scope = self.get_scope(id).unwrap();
  214. let wip_frame = scope.fin_frame();
  215. let nodes = wip_frame.nodes.borrow();
  216. let node: &VNode = nodes.get(0).unwrap();
  217. unsafe { std::mem::transmute::<&VNode, &VNode>(node) }
  218. }
  219. pub fn root_node(&self, id: &ScopeId) -> &VNode {
  220. self.wip_head(id)
  221. }
  222. }