From 47513e8b9e7ec1b1478b34c9df4f9fffd1bea90c Mon Sep 17 00:00:00 2001 From: Jonathan Kelley Date: Thu, 6 Jul 2023 12:43:04 -0700 Subject: [PATCH] Use boxes instead of bumpslab for hooks --- packages/core/src/arena.rs | 5 +---- packages/core/src/scope_arena.rs | 3 +-- packages/core/src/scopes.rs | 17 ++++++++--------- 3 files changed, 10 insertions(+), 15 deletions(-) diff --git a/packages/core/src/arena.rs b/packages/core/src/arena.rs index ec9cf1684..04ffb50d1 100644 --- a/packages/core/src/arena.rs +++ b/packages/core/src/arena.rs @@ -4,7 +4,6 @@ use crate::{ innerlude::DirtyScope, nodes::RenderReturn, nodes::VNode, virtual_dom::VirtualDom, AttributeValue, DynamicNode, ScopeId, }; -use bumpalo::boxed::Box as BumpBox; /// An Element's unique identifier. /// @@ -110,9 +109,7 @@ impl VirtualDom { // Drop all the hooks once the children are dropped // this means we'll drop hooks bottom-up - for hook in scope.hook_list.get_mut().drain(..) { - drop(unsafe { BumpBox::from_raw(hook) }); - } + scope.hooks.get_mut().clear(); // Drop all the futures once the hooks are dropped for task_id in scope.spawned_tasks.borrow_mut().drain() { diff --git a/packages/core/src/scope_arena.rs b/packages/core/src/scope_arena.rs index 548329354..a8a7319aa 100644 --- a/packages/core/src/scope_arena.rs +++ b/packages/core/src/scope_arena.rs @@ -38,8 +38,7 @@ impl VirtualDom { node_arena_2: BumpFrame::new(0), spawned_tasks: Default::default(), render_cnt: Default::default(), - hook_arena: Default::default(), - hook_list: Default::default(), + hooks: Default::default(), hook_idx: Default::default(), shared_contexts: Default::default(), borrowed_props: Default::default(), diff --git a/packages/core/src/scopes.rs b/packages/core/src/scopes.rs index 5601d57fe..2e4bf3651 100644 --- a/packages/core/src/scopes.rs +++ b/packages/core/src/scopes.rs @@ -15,7 +15,7 @@ use rustc_hash::{FxHashMap, FxHashSet}; use slab::{Slab, VacantEntry}; use std::{ any::{Any, TypeId}, - cell::{Cell, RefCell}, + cell::{Cell, RefCell, UnsafeCell}, fmt::{Arguments, Debug}, future::Future, ops::{Index, IndexMut}, @@ -170,8 +170,7 @@ pub struct ScopeState { pub(crate) height: u32, - pub(crate) hook_arena: Bump, - pub(crate) hook_list: RefCell>, + pub(crate) hooks: RefCell>>>, pub(crate) hook_idx: Cell, pub(crate) shared_contexts: RefCell>>, @@ -641,18 +640,18 @@ impl<'src> ScopeState { #[allow(clippy::mut_from_ref)] pub fn use_hook(&self, initializer: impl FnOnce() -> State) -> &mut State { let cur_hook = self.hook_idx.get(); - let mut hook_list = self.hook_list.try_borrow_mut().expect("The hook list is already borrowed: This error is likely caused by trying to use a hook inside a hook which violates the rules of hooks."); + let mut hooks = self.hooks.try_borrow_mut().expect("The hook list is already borrowed: This error is likely caused by trying to use a hook inside a hook which violates the rules of hooks."); - if cur_hook >= hook_list.len() { - hook_list.push(self.hook_arena.alloc(initializer())); + if cur_hook >= hooks.len() { + hooks.push(Box::new(UnsafeCell::new(initializer()))); } - hook_list + hooks .get(cur_hook) .and_then(|inn| { self.hook_idx.set(cur_hook + 1); - let raw_box = unsafe { &mut **inn }; - raw_box.downcast_mut::() + let raw_ref = unsafe { &mut *inn.get() }; + raw_ref.downcast_mut::() }) .expect( r###"