scopearena.rs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. use bumpalo::Bump;
  2. use futures_channel::mpsc::UnboundedSender;
  3. use fxhash::{FxHashMap, FxHashSet};
  4. use slab::Slab;
  5. use std::{
  6. borrow::Borrow,
  7. cell::{Cell, RefCell},
  8. };
  9. use crate::innerlude::*;
  10. pub type FcSlot = *const ();
  11. pub struct Heuristic {
  12. hook_arena_size: usize,
  13. node_arena_size: usize,
  14. }
  15. // a slab-like arena with stable references even when new scopes are allocated
  16. // uses a bump arena as a backing
  17. //
  18. // has an internal heuristics engine to pre-allocate arenas to the right size
  19. pub(crate) struct ScopeArena {
  20. bump: Bump,
  21. pub pending_futures: RefCell<FxHashSet<ScopeId>>,
  22. scope_counter: Cell<usize>,
  23. pub scopes: RefCell<FxHashMap<ScopeId, *mut Scope>>,
  24. pub heuristics: RefCell<FxHashMap<FcSlot, Heuristic>>,
  25. free_scopes: RefCell<Vec<*mut Scope>>,
  26. nodes: RefCell<Slab<*const VNode<'static>>>,
  27. pub(crate) sender: UnboundedSender<SchedulerMsg>,
  28. }
  29. impl ScopeArena {
  30. pub(crate) fn new(sender: UnboundedSender<SchedulerMsg>) -> Self {
  31. let bump = Bump::new();
  32. // allocate a container for the root element
  33. // this will *never* show up in the diffing process
  34. let el = bump.alloc(VElement {
  35. tag_name: "root",
  36. namespace: None,
  37. key: None,
  38. dom_id: Cell::new(Some(ElementId(0))),
  39. parent_id: Default::default(),
  40. listeners: &[],
  41. attributes: &[],
  42. children: &[],
  43. });
  44. let node = bump.alloc(VNode::Element(el));
  45. let mut nodes = Slab::new();
  46. let root_id = nodes.insert(unsafe { std::mem::transmute(node as *const _) });
  47. debug_assert_eq!(root_id, 0);
  48. Self {
  49. scope_counter: Cell::new(0),
  50. bump,
  51. pending_futures: RefCell::new(FxHashSet::default()),
  52. scopes: RefCell::new(FxHashMap::default()),
  53. heuristics: RefCell::new(FxHashMap::default()),
  54. free_scopes: RefCell::new(Vec::new()),
  55. nodes: RefCell::new(nodes),
  56. sender,
  57. }
  58. }
  59. /// Safety:
  60. /// - Obtaining a mutable refernece to any Scope is unsafe
  61. /// - Scopes use interior mutability when sharing data into components
  62. pub(crate) fn get_scope(&self, id: &ScopeId) -> Option<&Scope> {
  63. unsafe { self.scopes.borrow().get(id).map(|f| &**f) }
  64. }
  65. pub(crate) unsafe fn get_scope_raw(&self, id: &ScopeId) -> Option<*mut Scope> {
  66. self.scopes.borrow().get(id).copied()
  67. }
  68. pub(crate) unsafe fn get_scope_mut(&self, id: &ScopeId) -> Option<&mut Scope> {
  69. self.scopes.borrow().get(id).map(|s| &mut **s)
  70. }
  71. pub(crate) fn new_with_key(
  72. &self,
  73. fc_ptr: *const (),
  74. caller: *const dyn Fn(&Scope) -> Element,
  75. parent_scope: Option<*mut Scope>,
  76. container: ElementId,
  77. height: u32,
  78. subtree: u32,
  79. ) -> ScopeId {
  80. let new_scope_id = ScopeId(self.scope_counter.get());
  81. self.scope_counter.set(self.scope_counter.get() + 1);
  82. // log::debug!("new scope {:?} with parent {:?}", new_scope_id, container);
  83. if let Some(old_scope) = self.free_scopes.borrow_mut().pop() {
  84. let scope = unsafe { &mut *old_scope };
  85. // log::debug!(
  86. // "reusing scope {:?} as {:?}",
  87. // scope.our_arena_idx,
  88. // new_scope_id
  89. // );
  90. scope.caller = caller;
  91. scope.parent_scope = parent_scope;
  92. scope.height = height;
  93. scope.subtree = Cell::new(subtree);
  94. scope.our_arena_idx = new_scope_id;
  95. scope.container = container;
  96. scope.frames[0].nodes.get_mut().push({
  97. let vnode = scope.frames[0]
  98. .bump
  99. .alloc(VNode::Text(scope.frames[0].bump.alloc(VText {
  100. dom_id: Default::default(),
  101. is_static: false,
  102. text: "",
  103. })));
  104. unsafe { std::mem::transmute(vnode as *mut VNode) }
  105. });
  106. scope.frames[1].nodes.get_mut().push({
  107. let vnode = scope.frames[1]
  108. .bump
  109. .alloc(VNode::Text(scope.frames[1].bump.alloc(VText {
  110. dom_id: Default::default(),
  111. is_static: false,
  112. text: "",
  113. })));
  114. unsafe { std::mem::transmute(vnode as *mut VNode) }
  115. });
  116. let any_item = self.scopes.borrow_mut().insert(new_scope_id, scope);
  117. debug_assert!(any_item.is_none());
  118. } else {
  119. let (node_capacity, hook_capacity) = {
  120. let heuristics = self.heuristics.borrow();
  121. if let Some(heuristic) = heuristics.get(&fc_ptr) {
  122. (heuristic.node_arena_size, heuristic.hook_arena_size)
  123. } else {
  124. (0, 0)
  125. }
  126. };
  127. let mut frames = [BumpFrame::new(node_capacity), BumpFrame::new(node_capacity)];
  128. frames[0].nodes.get_mut().push({
  129. let vnode = frames[0]
  130. .bump
  131. .alloc(VNode::Text(frames[0].bump.alloc(VText {
  132. dom_id: Default::default(),
  133. is_static: false,
  134. text: "",
  135. })));
  136. unsafe { std::mem::transmute(vnode as *mut VNode) }
  137. });
  138. frames[1].nodes.get_mut().push({
  139. let vnode = frames[1]
  140. .bump
  141. .alloc(VNode::Text(frames[1].bump.alloc(VText {
  142. dom_id: Default::default(),
  143. is_static: false,
  144. text: "",
  145. })));
  146. unsafe { std::mem::transmute(vnode as *mut VNode) }
  147. });
  148. let scope = self.bump.alloc(Scope {
  149. sender: self.sender.clone(),
  150. container,
  151. our_arena_idx: new_scope_id,
  152. parent_scope,
  153. height,
  154. frames,
  155. subtree: Cell::new(subtree),
  156. is_subtree_root: Cell::new(false),
  157. caller,
  158. generation: 0.into(),
  159. hooks: HookList::new(hook_capacity),
  160. shared_contexts: Default::default(),
  161. items: RefCell::new(SelfReferentialItems {
  162. listeners: Default::default(),
  163. borrowed_props: Default::default(),
  164. suspended_nodes: Default::default(),
  165. tasks: Default::default(),
  166. pending_effects: Default::default(),
  167. }),
  168. });
  169. let any_item = self.scopes.borrow_mut().insert(new_scope_id, scope);
  170. debug_assert!(any_item.is_none());
  171. }
  172. new_scope_id
  173. }
  174. pub fn try_remove(&self, id: &ScopeId) -> Option<()> {
  175. self.ensure_drop_safety(id);
  176. // log::debug!("removing scope {:?}", id);
  177. // Safety:
  178. // - ensure_drop_safety ensures that no references to this scope are in use
  179. // - this raw pointer is removed from the map
  180. let scope = unsafe { &mut *self.scopes.borrow_mut().remove(id).unwrap() };
  181. // we're just reusing scopes so we need to clear it out
  182. scope.hooks.clear();
  183. scope.shared_contexts.get_mut().clear();
  184. scope.parent_scope = None;
  185. scope.generation.set(0);
  186. scope.is_subtree_root.set(false);
  187. scope.subtree.set(0);
  188. scope.frames[0].nodes.get_mut().clear();
  189. scope.frames[1].nodes.get_mut().clear();
  190. scope.frames[0].bump.reset();
  191. scope.frames[1].bump.reset();
  192. let SelfReferentialItems {
  193. borrowed_props,
  194. listeners,
  195. pending_effects,
  196. suspended_nodes,
  197. tasks,
  198. } = scope.items.get_mut();
  199. borrowed_props.clear();
  200. listeners.clear();
  201. pending_effects.clear();
  202. suspended_nodes.clear();
  203. tasks.clear();
  204. self.free_scopes.borrow_mut().push(scope);
  205. Some(())
  206. }
  207. pub fn reserve_node(&self, node: &VNode) -> ElementId {
  208. let mut els = self.nodes.borrow_mut();
  209. let entry = els.vacant_entry();
  210. let key = entry.key();
  211. let id = ElementId(key);
  212. let node: *const VNode = node as *const _;
  213. let node = unsafe { std::mem::transmute::<*const VNode, *const VNode>(node) };
  214. entry.insert(node);
  215. id
  216. }
  217. pub fn update_node(&self, node: &VNode, id: ElementId) {
  218. let node = unsafe { std::mem::transmute::<*const VNode, *const VNode>(node) };
  219. *self.nodes.borrow_mut().get_mut(id.0).unwrap() = node;
  220. }
  221. pub fn collect_garbage(&self, id: ElementId) {
  222. self.nodes.borrow_mut().remove(id.0);
  223. }
  224. // These methods would normally exist on `scope` but they need access to *all* of the scopes
  225. /// This method cleans up any references to data held within our hook list. This prevents mutable aliasing from
  226. /// causing UB in our tree.
  227. ///
  228. /// This works by cleaning up our references from the bottom of the tree to the top. The directed graph of components
  229. /// essentially forms a dependency tree that we can traverse from the bottom to the top. As we traverse, we remove
  230. /// any possible references to the data in the hook list.
  231. ///
  232. /// References to hook data can only be stored in listeners and component props. During diffing, we make sure to log
  233. /// all listeners and borrowed props so we can clear them here.
  234. ///
  235. /// This also makes sure that drop order is consistent and predictable. All resources that rely on being dropped will
  236. /// be dropped.
  237. pub(crate) fn ensure_drop_safety(&self, scope_id: &ScopeId) {
  238. let scope = self.get_scope(scope_id).unwrap();
  239. let mut items = scope.items.borrow_mut();
  240. // make sure we drop all borrowed props manually to guarantee that their drop implementation is called before we
  241. // run the hooks (which hold an &mut Reference)
  242. // recursively call ensure_drop_safety on all children
  243. items.borrowed_props.drain(..).for_each(|comp| {
  244. let scope_id = comp
  245. .associated_scope
  246. .get()
  247. .expect("VComponents should be associated with a valid Scope");
  248. self.ensure_drop_safety(&scope_id);
  249. let mut drop_props = comp.drop_props.borrow_mut().take().unwrap();
  250. drop_props();
  251. });
  252. // Now that all the references are gone, we can safely drop our own references in our listeners.
  253. items
  254. .listeners
  255. .drain(..)
  256. .for_each(|listener| drop(listener.callback.borrow_mut().take()));
  257. }
  258. pub(crate) fn run_scope(&self, id: &ScopeId) -> bool {
  259. // Cycle to the next frame and then reset it
  260. // This breaks any latent references, invalidating every pointer referencing into it.
  261. // Remove all the outdated listeners
  262. self.ensure_drop_safety(id);
  263. let scope = unsafe { &mut *self.get_scope_mut(id).expect("could not find scope") };
  264. // log::debug!("found scope, about to run: {:?}", id);
  265. // Safety:
  266. // - We dropped the listeners, so no more &mut T can be used while these are held
  267. // - All children nodes that rely on &mut T are replaced with a new reference
  268. unsafe { scope.hooks.reset() };
  269. // Safety:
  270. // - We've dropped all references to the wip bump frame with "ensure_drop_safety"
  271. unsafe { scope.reset_wip_frame() };
  272. {
  273. let mut items = scope.items.borrow_mut();
  274. // just forget about our suspended nodes while we're at it
  275. items.suspended_nodes.clear();
  276. items.tasks.clear();
  277. items.pending_effects.clear();
  278. // guarantee that we haven't screwed up - there should be no latent references anywhere
  279. debug_assert!(items.listeners.is_empty());
  280. debug_assert!(items.borrowed_props.is_empty());
  281. debug_assert!(items.suspended_nodes.is_empty());
  282. debug_assert!(items.tasks.is_empty());
  283. debug_assert!(items.pending_effects.is_empty());
  284. // Todo: see if we can add stronger guarantees around internal bookkeeping and failed component renders.
  285. scope.wip_frame().nodes.borrow_mut().clear();
  286. }
  287. let render: &dyn Fn(&Scope) -> Element = unsafe { &*scope.caller };
  288. if let Some(link) = render(scope) {
  289. // right now, it's a panic to render a nodelink from another scope
  290. // todo: enable this. it should (reasonably) work even if it doesnt make much sense
  291. assert_eq!(link.scope_id.get(), Some(*id));
  292. // nodelinks are not assigned when called and must be done so through the create/diff phase
  293. // however, we need to link this one up since it will never be used in diffing
  294. scope.wip_frame().assign_nodelink(&link);
  295. debug_assert_eq!(scope.wip_frame().nodes.borrow().len(), 1);
  296. if !scope.items.borrow().tasks.is_empty() {
  297. self.pending_futures.borrow_mut().insert(*id);
  298. }
  299. // make the "wip frame" contents the "finished frame"
  300. // any future dipping into completed nodes after "render" will go through "fin head"
  301. scope.cycle_frame();
  302. true
  303. } else {
  304. false
  305. }
  306. }
  307. pub fn call_listener_with_bubbling(&self, event: UserEvent, element: ElementId) {
  308. let nodes = self.nodes.borrow();
  309. let mut cur_el = Some(element);
  310. while let Some(id) = cur_el.take() {
  311. if let Some(el) = nodes.get(id.0) {
  312. let real_el = unsafe { &**el };
  313. if let VNode::Element(real_el) = real_el {
  314. //
  315. for listener in real_el.listeners.borrow().iter() {
  316. if listener.event == event.name {
  317. let mut cb = listener.callback.borrow_mut();
  318. if let Some(cb) = cb.as_mut() {
  319. (cb)(event.data.clone());
  320. }
  321. }
  322. }
  323. cur_el = real_el.parent_id.get();
  324. }
  325. }
  326. }
  327. }
  328. // The head of the bumpframe is the first linked NodeLink
  329. pub fn wip_head(&self, id: &ScopeId) -> &VNode {
  330. let scope = self.get_scope(id).unwrap();
  331. let frame = scope.wip_frame();
  332. let nodes = frame.nodes.borrow();
  333. let node: &VNode = unsafe { &**nodes.get(0).unwrap() };
  334. unsafe { std::mem::transmute::<&VNode, &VNode>(node) }
  335. }
  336. // The head of the bumpframe is the first linked NodeLink
  337. pub fn fin_head(&self, id: &ScopeId) -> &VNode {
  338. let scope = self.get_scope(id).unwrap();
  339. let frame = scope.fin_frame();
  340. let nodes = frame.nodes.borrow();
  341. let node: &VNode = unsafe { &**nodes.get(0).unwrap() };
  342. unsafe { std::mem::transmute::<&VNode, &VNode>(node) }
  343. }
  344. pub fn root_node(&self, id: &ScopeId) -> &VNode {
  345. self.fin_head(id)
  346. }
  347. }