scopearena.rs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. use bumpalo::Bump;
  2. use futures_channel::mpsc::UnboundedSender;
  3. use fxhash::FxHashMap;
  4. use slab::Slab;
  5. use std::cell::{Cell, RefCell};
  6. use crate::innerlude::*;
  7. pub type FcSlot = *const ();
  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. pub heuristics: RefCell<FxHashMap<FcSlot, Heuristic>>,
  20. free_scopes: RefCell<Vec<ScopeId>>,
  21. nodes: RefCell<Slab<*const VNode<'static>>>,
  22. pub(crate) sender: UnboundedSender<SchedulerMsg>,
  23. }
  24. impl ScopeArena {
  25. pub fn new(sender: UnboundedSender<SchedulerMsg>) -> Self {
  26. Self {
  27. bump: Bump::new(),
  28. scopes: RefCell::new(Vec::new()),
  29. heuristics: RefCell::new(FxHashMap::default()),
  30. free_scopes: RefCell::new(Vec::new()),
  31. nodes: RefCell::new(Slab::new()),
  32. sender,
  33. }
  34. }
  35. pub fn get_scope(&self, id: &ScopeId) -> Option<&Scope> {
  36. unsafe { self.scopes.borrow().get(id.0).map(|f| &**f) }
  37. }
  38. // this is unsafe
  39. pub unsafe fn get_scope_raw(&self, id: &ScopeId) -> Option<*mut Scope> {
  40. self.scopes.borrow().get(id.0).map(|f| *f)
  41. }
  42. // this is unsafe
  43. pub unsafe fn get_scope_mut(&self, id: &ScopeId) -> Option<&mut Scope> {
  44. self.scopes.borrow().get(id.0).map(|s| &mut **s)
  45. }
  46. pub fn new_with_key(
  47. &self,
  48. fc_ptr: *const (),
  49. caller: *const dyn Fn(&Scope) -> Element,
  50. parent_scope: Option<*mut Scope>,
  51. height: u32,
  52. subtree: u32,
  53. ) -> ScopeId {
  54. let scope_id = ScopeId(self.scopes.borrow().len());
  55. let (node_capacity, hook_capacity) = {
  56. let heuristics = self.heuristics.borrow();
  57. if let Some(heuristic) = heuristics.get(&fc_ptr) {
  58. (heuristic.node_arena_size, heuristic.hook_arena_size)
  59. } else {
  60. (0, 0)
  61. }
  62. };
  63. let mut frames = [BumpFrame::new(node_capacity), BumpFrame::new(node_capacity)];
  64. frames[0].nodes.get_mut().push({
  65. let vnode = frames[0]
  66. .bump
  67. .alloc(VNode::Text(frames[0].bump.alloc(VText {
  68. dom_id: Default::default(),
  69. is_static: false,
  70. text: "",
  71. })));
  72. unsafe { std::mem::transmute(vnode as *mut VNode) }
  73. });
  74. frames[1].nodes.get_mut().push({
  75. let vnode = frames[1]
  76. .bump
  77. .alloc(VNode::Text(frames[1].bump.alloc(VText {
  78. dom_id: Default::default(),
  79. is_static: false,
  80. text: "",
  81. })));
  82. unsafe { std::mem::transmute(vnode as *mut VNode) }
  83. });
  84. let mut new_scope = Scope {
  85. sender: self.sender.clone(),
  86. our_arena_idx: scope_id,
  87. parent_scope,
  88. height,
  89. frames,
  90. subtree: Cell::new(subtree),
  91. is_subtree_root: Cell::new(false),
  92. caller,
  93. generation: 0.into(),
  94. hooks: HookList::new(hook_capacity),
  95. shared_contexts: Default::default(),
  96. items: RefCell::new(SelfReferentialItems {
  97. listeners: Default::default(),
  98. borrowed_props: Default::default(),
  99. suspended_nodes: Default::default(),
  100. tasks: Default::default(),
  101. pending_effects: Default::default(),
  102. }),
  103. };
  104. if let Some(id) = self.free_scopes.borrow_mut().pop() {
  105. let scope = unsafe { self.get_scope_mut(&id) }.unwrap();
  106. std::mem::swap(&mut new_scope, scope);
  107. id
  108. } else {
  109. let scope = self.bump.alloc(new_scope);
  110. self.scopes.borrow_mut().push(scope);
  111. scope_id
  112. }
  113. }
  114. pub fn try_remove(&self, id: &ScopeId) -> Option<()> {
  115. self.ensure_drop_safety(id);
  116. let mut scope = unsafe { &mut *self.get_scope_raw(id)? };
  117. // we're just reusing scopes so we need to clear it out
  118. scope.hooks.clear();
  119. scope.shared_contexts.get_mut().clear();
  120. scope.parent_scope = None;
  121. scope.generation.set(0);
  122. scope.is_subtree_root.set(false);
  123. scope.subtree.set(0);
  124. let SelfReferentialItems {
  125. borrowed_props,
  126. listeners,
  127. pending_effects,
  128. suspended_nodes,
  129. tasks,
  130. } = scope.items.get_mut();
  131. borrowed_props.clear();
  132. listeners.clear();
  133. pending_effects.clear();
  134. suspended_nodes.clear();
  135. tasks.clear();
  136. self.free_scopes.borrow_mut().push(*id);
  137. Some(())
  138. }
  139. pub fn reserve_node(&self, node: &VNode) -> ElementId {
  140. let mut els = self.nodes.borrow_mut();
  141. let entry = els.vacant_entry();
  142. let key = entry.key();
  143. let id = ElementId(key);
  144. let node: *const VNode = node as *const _;
  145. let node = unsafe { std::mem::transmute::<*const VNode, *const VNode>(node) };
  146. entry.insert(node);
  147. id
  148. }
  149. pub fn collect_garbage(&self, id: ElementId) {
  150. self.nodes.borrow_mut().remove(id.0);
  151. }
  152. // These methods would normally exist on `scope` but they need access to *all* of the scopes
  153. /// This method cleans up any references to data held within our hook list. This prevents mutable aliasing from
  154. /// causing UB in our tree.
  155. ///
  156. /// This works by cleaning up our references from the bottom of the tree to the top. The directed graph of components
  157. /// essentially forms a dependency tree that we can traverse from the bottom to the top. As we traverse, we remove
  158. /// any possible references to the data in the hook list.
  159. ///
  160. /// References to hook data can only be stored in listeners and component props. During diffing, we make sure to log
  161. /// all listeners and borrowed props so we can clear them here.
  162. ///
  163. /// This also makes sure that drop order is consistent and predictable. All resources that rely on being dropped will
  164. /// be dropped.
  165. pub(crate) fn ensure_drop_safety(&self, scope_id: &ScopeId) {
  166. let scope = self.get_scope(scope_id).unwrap();
  167. let mut items = scope.items.borrow_mut();
  168. // make sure we drop all borrowed props manually to guarantee that their drop implementation is called before we
  169. // run the hooks (which hold an &mut Reference)
  170. // recursively call ensure_drop_safety on all children
  171. items.borrowed_props.drain(..).for_each(|comp| {
  172. let scope_id = comp
  173. .associated_scope
  174. .get()
  175. .expect("VComponents should be associated with a valid Scope");
  176. self.ensure_drop_safety(&scope_id);
  177. let mut drop_props = comp.drop_props.borrow_mut().take().unwrap();
  178. drop_props();
  179. });
  180. // Now that all the references are gone, we can safely drop our own references in our listeners.
  181. items
  182. .listeners
  183. .drain(..)
  184. .for_each(|listener| drop(listener.callback.borrow_mut().take()));
  185. }
  186. pub(crate) fn run_scope(&self, id: &ScopeId) -> bool {
  187. let scope = unsafe {
  188. &mut *self
  189. .get_scope_mut(id)
  190. .expect("The base scope should never be moved")
  191. };
  192. // Cycle to the next frame and then reset it
  193. // This breaks any latent references, invalidating every pointer referencing into it.
  194. // Remove all the outdated listeners
  195. self.ensure_drop_safety(id);
  196. // Safety:
  197. // - We dropped the listeners, so no more &mut T can be used while these are held
  198. // - All children nodes that rely on &mut T are replaced with a new reference
  199. unsafe { scope.hooks.reset() };
  200. // Safety:
  201. // - We've dropped all references to the wip bump frame with "ensure_drop_safety"
  202. unsafe { scope.reset_wip_frame() };
  203. {
  204. let mut items = scope.items.borrow_mut();
  205. // just forget about our suspended nodes while we're at it
  206. items.suspended_nodes.clear();
  207. items.tasks.clear();
  208. items.pending_effects.clear();
  209. // guarantee that we haven't screwed up - there should be no latent references anywhere
  210. debug_assert!(items.listeners.is_empty());
  211. debug_assert!(items.borrowed_props.is_empty());
  212. debug_assert!(items.suspended_nodes.is_empty());
  213. debug_assert!(items.tasks.is_empty());
  214. debug_assert!(items.pending_effects.is_empty());
  215. // Todo: see if we can add stronger guarantees around internal bookkeeping and failed component renders.
  216. scope.wip_frame().nodes.borrow_mut().clear();
  217. }
  218. let render: &dyn Fn(&Scope) -> Element = unsafe { &*scope.caller };
  219. if let Some(link) = render(scope) {
  220. // right now, it's a panic to render a nodelink from another scope
  221. // todo: enable this. it should (reasonably) work even if it doesnt make much sense
  222. assert_eq!(link.scope_id.get(), Some(*id));
  223. // nodelinks are not assigned when called and must be done so through the create/diff phase
  224. // however, we need to link this one up since it will never be used in diffing
  225. scope.wip_frame().assign_nodelink(&link);
  226. debug_assert_eq!(scope.wip_frame().nodes.borrow().len(), 1);
  227. if !scope.items.borrow().tasks.is_empty() {
  228. // self.
  229. }
  230. // make the "wip frame" contents the "finished frame"
  231. // any future dipping into completed nodes after "render" will go through "fin head"
  232. scope.cycle_frame();
  233. true
  234. } else {
  235. false
  236. }
  237. }
  238. // The head of the bumpframe is the first linked NodeLink
  239. pub fn wip_head(&self, id: &ScopeId) -> &VNode {
  240. let scope = self.get_scope(id).unwrap();
  241. let wip_frame = scope.wip_frame();
  242. let nodes = wip_frame.nodes.borrow();
  243. let node: &VNode = unsafe { &**nodes.get(0).unwrap() };
  244. unsafe { std::mem::transmute::<&VNode, &VNode>(node) }
  245. }
  246. // The head of the bumpframe is the first linked NodeLink
  247. pub fn fin_head(&self, id: &ScopeId) -> &VNode {
  248. let scope = self.get_scope(id).unwrap();
  249. let wip_frame = scope.fin_frame();
  250. let nodes = wip_frame.nodes.borrow();
  251. let node: &VNode = unsafe { &**nodes.get(0).unwrap() };
  252. unsafe { std::mem::transmute::<&VNode, &VNode>(node) }
  253. }
  254. pub fn root_node(&self, id: &ScopeId) -> &VNode {
  255. self.fin_head(id)
  256. }
  257. }