Skip to content

Commit

Permalink
Optimize view scopes
Browse files Browse the repository at this point in the history
  • Loading branch information
matthunz committed Jan 26, 2024
1 parent eec522f commit 0a01b18
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 10 deletions.
1 change: 1 addition & 0 deletions crates/concoct/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ full = ["tracing"]

[dependencies]
futures = "0.3.30"
im = "15.1.0"
rustc-hash = "1.1.0"
slotmap = "1.0.7"
tracing = { version = "0.1.40", optional = true }
Expand Down
13 changes: 10 additions & 3 deletions crates/concoct/src/hook/use_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,18 @@ pub fn use_ref<T, A, R: 'static>(cx: &Scope<T, A>, make_initial: impl FnOnce() -
let idx = node.hook_idx;
node.hook_idx += 1;

let cell = if let Some(cell) = node.hooks.get(idx) {
let hooks = if let Some(hooks) = node.hooks.as_mut() {
hooks
} else {
node.hooks = Some(Vec::new());
node.hooks.as_mut().unwrap()
};

let cell = if let Some(cell) = hooks.get(idx) {
cell
} else {
node.hooks.push(UnsafeCell::new(Box::new(make_initial())));
node.hooks.last().unwrap()
hooks.push(UnsafeCell::new(Box::new(make_initial())));
hooks.last().unwrap()
};

// Safety: All references to scope are dropped before `hook_idx` is reset.
Expand Down
8 changes: 4 additions & 4 deletions crates/concoct/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
//! ```ignore
//! use concoct::{Scope, View};
//! use concoct_web::html;
//!
//!
//! #[derive(Default)]
//! struct Counter {
//! count: i32,
//! }
//!
//!
//! impl View<Self> for Counter {
//! fn body(&mut self, _cx: &Scope<Self>) -> impl View<Self> {
//! (
Expand All @@ -22,12 +22,12 @@
//! )
//! }
//! }
//!
//!
//! fn main() {
//! concoct_web::launch(Counter::default())
//! }
//! ```
//!
//!

#![deny(missing_docs)]

Expand Down
6 changes: 4 additions & 2 deletions crates/concoct/src/scope.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
use crate::{vdom::Node, Handle, View};
use rustc_hash::FxHashMap;
use im::HashMap;
use rustc_hash::FxHasher;
use slotmap::{DefaultKey, SlotMap};
use std::{
any::{Any, TypeId},
cell::{Cell, RefCell},
hash::BuildHasherDefault,
mem,
rc::Rc,
};
Expand All @@ -16,7 +18,7 @@ pub struct Scope<T, A = ()> {
pub(crate) update: Rc<dyn Fn(Rc<dyn Fn(&Handle<T, A>, &mut T) -> Option<A>>)>,
pub(crate) is_empty: Cell<bool>,
pub(crate) nodes: Rc<RefCell<SlotMap<DefaultKey, Node>>>,
pub(crate) contexts: RefCell<FxHashMap<TypeId, Rc<dyn Any>>>,
pub(crate) contexts: RefCell<HashMap<TypeId, Rc<dyn Any>, BuildHasherDefault<FxHasher>>>,
}

impl<T, A> Scope<T, A> {
Expand Down
2 changes: 1 addition & 1 deletion crates/concoct/src/vdom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::{

#[derive(Default)]
pub(crate) struct NodeInner {
pub(crate) hooks: Vec<UnsafeCell<Box<dyn Any>>>,
pub(crate) hooks: Option<Vec<UnsafeCell<Box<dyn Any>>>>,
pub(crate) hook_idx: usize,
pub(crate) children: Vec<DefaultKey>,
}
Expand Down

0 comments on commit 0a01b18

Please sign in to comment.