Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove double-boxing of Computation #31

Merged
merged 1 commit into from
Mar 9, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 27 additions & 21 deletions maple-core/src/reactive.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
//! Reactive primitives.

use std::cell::RefCell;
use std::collections::HashSet;
use std::hash::{Hash, Hasher};
use std::ops::Deref;
use std::ptr;
use std::rc::Rc;

/// Returned by functions that provide a handle to access state.
Expand Down Expand Up @@ -122,30 +125,19 @@ impl<T: 'static> Clone for Signal<T> {

struct SignalInner<T> {
inner: Rc<T>,
observers: Vec<Rc<Computation>>,
observers: HashSet<Computation>,
}

impl<T> SignalInner<T> {
fn new(value: T) -> Self {
Self {
inner: Rc::new(value),
observers: Vec::new(),
observers: HashSet::new(),
}
}

fn observe(&mut self, handler: Rc<Computation>) {
// make sure handler is not already in self.observers
if self
.observers
.iter()
.find(|observer| {
observer.as_ref() as *const Computation == handler.as_ref() as *const Computation
/* do reference equality */
})
.is_none()
{
self.observers.push(handler);
}
fn observe(&mut self, handler: Computation) {
self.observers.insert(handler);
}

fn update(&mut self, new_value: T) {
Expand All @@ -160,9 +152,23 @@ impl<T> SignalInner<T> {
}

/// A derived computation from a signal.
struct Computation(Box<dyn Fn()>);
#[derive(Clone)]
struct Computation(Rc<dyn Fn()>);

impl Hash for Computation {
fn hash<H: Hasher>(&self, state: &mut H) {
Rc::as_ptr(&self.0).hash(state);
}
}

impl PartialEq for Computation {
fn eq(&self, other: &Self) -> bool {
ptr::eq::<()>(Rc::as_ptr(&self.0).cast(), Rc::as_ptr(&other.0).cast())
}
}
impl Eq for Computation {}

type Dependency = Box<dyn Fn(&Rc<Computation>)>;
type Dependency = Box<dyn Fn(&Computation)>;

thread_local! {
/// To add the dependencies, iterate through functions and execute them.
Expand All @@ -173,7 +179,7 @@ thread_local! {
///
/// Unlike [`create_effect`], this will allow the closure to run different code upon first
/// execution, so it can return a value.
fn create_effect_initial<R>(initial: impl FnOnce() -> (Rc<Computation>, R)) -> R {
fn create_effect_initial<R>(initial: impl FnOnce() -> (Computation, R)) -> R {
DEPENDENCIES.with(|dependencies| {
if dependencies.borrow().is_some() {
unimplemented!("nested dependencies are not supported")
Expand Down Expand Up @@ -203,7 +209,7 @@ where
{
create_effect_initial(move || {
effect();
(Rc::new(Computation(Box::new(effect))), ())
(Computation(Rc::new(effect)), ())
})
}

Expand Down Expand Up @@ -246,15 +252,15 @@ where
create_effect_initial(|| {
let memo = Signal::new(derived());

let effect = Rc::new(Computation(Box::new({
let effect = Computation(Rc::new({
let memo = memo.clone();
move || {
let new_value = derived();
if !comparator(&memo.get_untracked(), &new_value) {
memo.set(new_value);
}
}
})));
}));

(effect, memo.into_handle())
})
Expand Down