Skip to content

Commit

Permalink
Add is_changed parameter to to fix memo
Browse files Browse the repository at this point in the history
  • Loading branch information
matthunz committed Jan 21, 2024
1 parent 94db702 commit 535eaa8
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 42 deletions.
14 changes: 7 additions & 7 deletions crates/concoct/src/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub use node::Node;
pub trait Tree: 'static {
unsafe fn build(&mut self);

unsafe fn rebuild(&mut self, last: &mut dyn Any);
unsafe fn rebuild(&mut self, last: &mut dyn Any, is_changed: bool);

unsafe fn remove(&mut self);
}
Expand All @@ -21,10 +21,10 @@ impl<T: Tree> Tree for Option<T> {
}
}

unsafe fn rebuild(&mut self, last: &mut dyn Any) {
unsafe fn rebuild(&mut self, last: &mut dyn Any, _is_changed: bool) {
if let Some(tree) = self {
if let Some(last_tree) = last.downcast_mut::<Self>().unwrap() {
tree.rebuild(last_tree)
tree.rebuild(last_tree, true)
} else {
tree.build();
}
Expand All @@ -51,13 +51,13 @@ where
}
}

unsafe fn rebuild(&mut self, last: &mut dyn Any) {
unsafe fn rebuild(&mut self, last: &mut dyn Any, _is_changed: bool) {
let mut visited = HashSet::new();
let last = last.downcast_mut::<Self>().unwrap();

for (key, body) in self.iter_mut() {
if let Some((_, last_body)) = last.iter_mut().find(|(last_key, _)| last_key == key) {
body.rebuild(last_body);
body.rebuild(last_body, true);
visited.insert(key);
} else {
body.build();
Expand Down Expand Up @@ -87,10 +87,10 @@ macro_rules! impl_tree_for_tuple {
)*
}

unsafe fn rebuild(&mut self, last: &mut dyn Any) {
unsafe fn rebuild(&mut self, last: &mut dyn Any, is_changed: bool) {
if let Some(last) = last.downcast_mut::<Self>() {
$(
self.$idx.rebuild(&mut last.$idx);
self.$idx.rebuild(&mut last.$idx, is_changed);
)*
}
}
Expand Down
57 changes: 31 additions & 26 deletions crates/concoct/src/tree/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ where
};

let mut last_body = mem::replace(&mut self.body, Some(body)).unwrap();
self.body.as_mut().unwrap().rebuild(&mut last_body);
self.body.as_mut().unwrap().rebuild(&mut last_body, true);

let mut cx_ref = cx.inner.borrow_mut();
cx_ref.contexts = parent_contexts;
Expand Down Expand Up @@ -78,7 +78,7 @@ where
}
}

unsafe fn rebuild(&mut self, last: &mut dyn Any) {
unsafe fn rebuild(&mut self, last: &mut dyn Any, is_changed: bool) {
let last = (*last).downcast_mut::<Self>().unwrap();
let cx = Runtime::current();
let mut cx_ref = cx.inner.borrow_mut();
Expand All @@ -87,36 +87,41 @@ where
self.key = Some(key);
self.scope = last.scope.clone();

let mut scope = self.scope.as_ref().unwrap().inner.borrow_mut();
for (name, value) in cx_ref.contexts.iter() {
if !scope.contexts.contains_key(name) {
scope.contexts.insert(*name, value.clone());
if is_changed {
let mut scope = self.scope.as_ref().unwrap().inner.borrow_mut();
for (name, value) in cx_ref.contexts.iter() {
if !scope.contexts.contains_key(name) {
scope.contexts.insert(*name, value.clone());
}
}
}
drop(scope);
drop(scope);

cx_ref.node = Some(key);
cx_ref.scope = Some(self.scope.clone().unwrap());
drop(cx_ref);
cx_ref.node = Some(key);
cx_ref.scope = Some(self.scope.clone().unwrap());
drop(cx_ref);

let view = unsafe { mem::transmute(&self.view) };
let body = (self.builder)(view);
let view = unsafe { mem::transmute(&self.view) };
let body = (self.builder)(view);

let parent_contexts = {
let mut cx_ref = cx.inner.borrow_mut();
let mut scope = self.scope.as_ref().unwrap().inner.borrow_mut();
scope.hook_idx = 0;
mem::replace(&mut cx_ref.contexts, scope.contexts.clone())
};
let parent_contexts = {
let mut cx_ref = cx.inner.borrow_mut();
let mut scope = self.scope.as_ref().unwrap().inner.borrow_mut();
scope.hook_idx = 0;
mem::replace(&mut cx_ref.contexts, scope.contexts.clone())
};

self.body = Some(body);
self.body
.as_mut()
.unwrap()
.rebuild(last.body.as_mut().unwrap());
self.body = Some(body);

let mut cx_ref = cx.inner.borrow_mut();
cx_ref.contexts = parent_contexts;
self.body
.as_mut()
.unwrap()
.rebuild(last.body.as_mut().unwrap(), is_changed);

let mut cx_ref = cx.inner.borrow_mut();
cx_ref.contexts = parent_contexts;
} else {
self.body = last.body.take();
}
}

unsafe fn remove(&mut self) {
Expand Down
4 changes: 2 additions & 2 deletions crates/concoct/src/view/cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@ impl<V: View> Tree for ViewCell<V> {
self.tree.borrow_mut().as_tree().build()
}

unsafe fn rebuild(&mut self, last: &mut dyn Any) {
unsafe fn rebuild(&mut self, last: &mut dyn Any,is_changed: bool) {
let last = last.downcast_mut::<Self>().unwrap();
self.tree
.borrow_mut()
.as_tree()
.rebuild(&mut *last.tree.borrow_mut().as_any())
.rebuild(&mut *last.tree.borrow_mut().as_any(), is_changed)
}

unsafe fn remove(&mut self) {
Expand Down
2 changes: 1 addition & 1 deletion crates/concoct/src/view/empty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl View for Empty {
impl Tree for Empty {
unsafe fn build(&mut self) {}

unsafe fn rebuild(&mut self, _last: &mut dyn Any) {}
unsafe fn rebuild(&mut self, _last: &mut dyn Any, _is_changed: bool) {}

unsafe fn remove(&mut self) {}
}
7 changes: 3 additions & 4 deletions crates/concoct/src/view/memo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,10 @@ impl<T: Tree> Tree for Memo<T> {
self.body.build()
}

unsafe fn rebuild(&mut self, last: &mut dyn Any) {
unsafe fn rebuild(&mut self, last: &mut dyn Any, is_changed: bool) {
let last = last.downcast_mut::<Self>().unwrap();
if self.hash != last.hash {
self.body.rebuild(&mut last.body)
}
self.body
.rebuild(&mut last.body, is_changed && self.hash != last.hash)
}

unsafe fn remove(&mut self) {
Expand Down
4 changes: 2 additions & 2 deletions crates/concoct/src/view/one_of.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ macro_rules! one_of {
}
}

unsafe fn rebuild(&mut self, last: &mut dyn Any) {
unsafe fn rebuild(&mut self, last: &mut dyn Any, is_changed: bool) {
let last = last.downcast_mut::<Self>().unwrap();
match (self, last) {
$(
($name::$t(tree), $name::$t(last_tree)) => {
tree.rebuild(last_tree)
tree.rebuild(last_tree, is_changed)
}
),*
(me, last) => {
Expand Down

0 comments on commit 535eaa8

Please sign in to comment.