Skip to content

Commit

Permalink
perf: batch modifies notify
Browse files Browse the repository at this point in the history
  • Loading branch information
M-Adoo committed Sep 7, 2023
1 parent 5f510bf commit c22a244
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions core/src/state/stateful.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ pub(crate) struct StateData<W> {
/// the unsubscribe handle to this list let you can unsubscribe when this
/// `data` drop.
slot_link: UnsafeCell<Option<SlotNode>>,
/// The batched modifies of the `State` which will be notified.
share_scope: Rc<Cell<Option<ModifyScope>>>,
}

macro_rules! debug_borrow_location {
Expand Down Expand Up @@ -228,6 +230,7 @@ impl<W> StateData<W> {
#[cfg(debug_assertions)]
borrowed_at: Cell::new(None),
slot_link: <_>::default(),
share_scope: <_>::default(),
}
}

Expand Down Expand Up @@ -394,10 +397,19 @@ impl<'a, W> Drop for RefState<'a, W> {
let scope = self.forget_modifies();

if !scope.is_empty() {
if let Some(m) = self.modifier.as_mut() {
let mut subject = m.raw_modifies();
// todo: should batch all the modifies and notify once.`
AppCtx::spawn_local(async move { subject.next(scope) }).unwrap();
if let Some(s) = self.value.share_scope.get() {
self.value.share_scope.set(Some(s | scope));
} else {
self.value.share_scope.set(Some(scope));
if let Some(m) = self.modifier.as_mut() {
let mut subject = m.raw_modifies();
let share_scope = self.value.share_scope.clone();
AppCtx::spawn_local(async move {
let scope = share_scope.replace(None);
subject.next(unsafe { scope.unwrap_unchecked() });
})
.unwrap();
}
}
}
}
Expand Down

0 comments on commit c22a244

Please sign in to comment.