This reduces the size of cursor by a usize when the summary does not
require a context making Cursor usages and constructions slightly more
efficient.
This change is a bit annoying though, as Rust has no means of
specializing, so this uses a `ContextlessSummary` trait with a blanket
impl while turning the `Context` into a GAT `Context<'a>`. This means
`Summary` implies are a bit more verbose now while contextless ones are
slimmer. It does come with the downside that the lifetime in the GAT is
always considered invariant, so some lifetime splitting occurred due to
that.
```
push/4096 time: [352.65 µs 360.87 µs 367.80 µs]
thrpt: [10.621 MiB/s 10.825 MiB/s 11.077 MiB/s]
change:
time: [-2.6633% -1.3640% -0.0561%] (p = 0.05 < 0.05)
thrpt: [+0.0561% +1.3828% +2.7361%]
Change within noise threshold.
Found 16 outliers among 100 measurements (16.00%)
7 (7.00%) low severe
3 (3.00%) low mild
2 (2.00%) high mild
4 (4.00%) high severe
push/65536 time: [1.2917 ms 1.2949 ms 1.2979 ms]
thrpt: [48.156 MiB/s 48.267 MiB/s 48.387 MiB/s]
change:
time: [+1.4428% +1.9844% +2.5299%] (p = 0.00 < 0.05)
thrpt: [-2.4675% -1.9458% -1.4223%]
Performance has regressed.
Found 3 outliers among 100 measurements (3.00%)
1 (1.00%) low severe
1 (1.00%) low mild
1 (1.00%) high severe
append/4096 time: [677.87 ns 678.87 ns 679.83 ns]
thrpt: [5.6112 GiB/s 5.6192 GiB/s 5.6274 GiB/s]
change:
time: [-0.8924% -0.5017% -0.1705%] (p = 0.00 < 0.05)
thrpt: [+0.1708% +0.5043% +0.9004%]
Change within noise threshold.
Found 2 outliers among 100 measurements (2.00%)
1 (1.00%) low mild
1 (1.00%) high mild
append/65536 time: [9.3275 µs 9.3406 µs 9.3536 µs]
thrpt: [6.5253 GiB/s 6.5344 GiB/s 6.5435 GiB/s]
change:
time: [+0.5409% +0.7215% +0.9054%] (p = 0.00 < 0.05)
thrpt: [-0.8973% -0.7163% -0.5380%]
Change within noise threshold.
slice/4096 time: [27.673 µs 27.791 µs 27.907 µs]
thrpt: [139.97 MiB/s 140.56 MiB/s 141.16 MiB/s]
change:
time: [-1.1065% -0.6725% -0.2429%] (p = 0.00 < 0.05)
thrpt: [+0.2435% +0.6770% +1.1189%]
Change within noise threshold.
Found 5 outliers among 100 measurements (5.00%)
4 (4.00%) low mild
1 (1.00%) high mild
slice/65536 time: [507.55 µs 517.40 µs 535.60 µs]
thrpt: [116.69 MiB/s 120.80 MiB/s 123.14 MiB/s]
change:
time: [-1.3489% +0.0599% +2.2591%] (p = 0.96 > 0.05)
thrpt: [-2.2092% -0.0598% +1.3674%]
No change in performance detected.
Found 8 outliers among 100 measurements (8.00%)
5 (5.00%) low mild
2 (2.00%) high mild
1 (1.00%) high severe
bytes_in_range/4096 time: [3.3917 µs 3.4108 µs 3.4313 µs]
thrpt: [1.1117 GiB/s 1.1184 GiB/s 1.1247 GiB/s]
change:
time: [-5.3466% -4.7193% -4.1262%] (p = 0.00 < 0.05)
thrpt: [+4.3038% +4.9531% +5.6487%]
Performance has improved.
Found 6 outliers among 100 measurements (6.00%)
1 (1.00%) low mild
5 (5.00%) high mild
bytes_in_range/65536 time: [88.175 µs 88.613 µs 89.111 µs]
thrpt: [701.37 MiB/s 705.31 MiB/s 708.82 MiB/s]
change:
time: [-0.6935% +0.3769% +1.4655%] (p = 0.50 > 0.05)
thrpt: [-1.4443% -0.3755% +0.6984%]
No change in performance detected.
Found 2 outliers among 100 measurements (2.00%)
2 (2.00%) high mild
chars/4096 time: [678.70 ns 680.38 ns 682.08 ns]
thrpt: [5.5927 GiB/s 5.6067 GiB/s 5.6206 GiB/s]
change:
time: [-0.6969% -0.2755% +0.1485%] (p = 0.20 > 0.05)
thrpt: [-0.1483% +0.2763% +0.7018%]
No change in performance detected.
Found 9 outliers among 100 measurements (9.00%)
5 (5.00%) low mild
4 (4.00%) high mild
chars/65536 time: [12.720 µs 12.775 µs 12.830 µs]
thrpt: [4.7573 GiB/s 4.7778 GiB/s 4.7983 GiB/s]
change:
time: [-0.6172% -0.1110% +0.4179%] (p = 0.68 > 0.05)
thrpt: [-0.4162% +0.1112% +0.6211%]
No change in performance detected.
Found 2 outliers among 100 measurements (2.00%)
1 (1.00%) low mild
1 (1.00%) high mild
clip_point/4096 time: [33.240 µs 33.310 µs 33.394 µs]
thrpt: [116.98 MiB/s 117.27 MiB/s 117.52 MiB/s]
change:
time: [-2.8892% -2.6305% -2.3438%] (p = 0.00 < 0.05)
thrpt: [+2.4000% +2.7015% +2.9751%]
Performance has improved.
Found 12 outliers among 100 measurements (12.00%)
1 (1.00%) low mild
4 (4.00%) high mild
7 (7.00%) high severe
clip_point/65536 time: [1.6531 ms 1.6586 ms 1.6640 ms]
thrpt: [37.560 MiB/s 37.683 MiB/s 37.808 MiB/s]
change:
time: [-6.6381% -5.9395% -5.2680%] (p = 0.00 < 0.05)
thrpt: [+5.5610% +6.3146% +7.1100%]
Performance has improved.
Found 7 outliers among 100 measurements (7.00%)
1 (1.00%) low mild
2 (2.00%) high mild
4 (4.00%) high severe
point_to_offset/4096 time: [11.586 µs 11.603 µs 11.621 µs]
thrpt: [336.15 MiB/s 336.67 MiB/s 337.16 MiB/s]
change:
time: [-14.289% -14.111% -13.939%] (p = 0.00 < 0.05)
thrpt: [+16.197% +16.429% +16.672%]
Performance has improved.
Found 12 outliers among 100 measurements (12.00%)
3 (3.00%) low severe
5 (5.00%) low mild
4 (4.00%) high mild
point_to_offset/65536 time: [527.74 µs 532.08 µs 536.51 µs]
thrpt: [116.49 MiB/s 117.46 MiB/s 118.43 MiB/s]
change:
time: [-6.7825% -4.6235% -2.3533%] (p = 0.00 < 0.05)
thrpt: [+2.4100% +4.8477% +7.2760%]
Performance has improved.
Found 8 outliers among 100 measurements (8.00%)
4 (4.00%) high mild
4 (4.00%) high severe
cursor/4096 time: [16.154 µs 16.192 µs 16.232 µs]
thrpt: [240.66 MiB/s 241.24 MiB/s 241.81 MiB/s]
change:
time: [-3.2536% -2.9145% -2.5526%] (p = 0.00 < 0.05)
thrpt: [+2.6194% +3.0019% +3.3630%]
Performance has improved.
Found 5 outliers among 100 measurements (5.00%)
1 (1.00%) low mild
2 (2.00%) high mild
2 (2.00%) high severe
cursor/65536 time: [509.60 µs 511.24 µs 512.93 µs]
thrpt: [121.85 MiB/s 122.25 MiB/s 122.65 MiB/s]
change:
time: [-7.3677% -6.6017% -5.7840%] (p = 0.00 < 0.05)
thrpt: [+6.1391% +7.0683% +7.9537%]
Performance has improved.
Found 6 outliers among 100 measurements (6.00%)
3 (3.00%) high mild
3 (3.00%) high severe
```
Release Notes:
- N/A *or* Added/Fixed/Improved ...
431 lines
13 KiB
Rust
431 lines
13 KiB
Rust
use anyhow::{Context as _, Result};
|
|
use channel::ChannelStore;
|
|
use client::{ChannelId, Client, UserStore};
|
|
use db::smol::stream::StreamExt;
|
|
use gpui::{App, AppContext as _, AsyncApp, Context, Entity, EventEmitter, Global, Task};
|
|
use rpc::{Notification, TypedEnvelope, proto};
|
|
use std::{ops::Range, sync::Arc};
|
|
use sum_tree::{Bias, Dimensions, SumTree};
|
|
use time::OffsetDateTime;
|
|
use util::ResultExt;
|
|
|
|
pub fn init(client: Arc<Client>, user_store: Entity<UserStore>, cx: &mut App) {
|
|
let notification_store = cx.new(|cx| NotificationStore::new(client, user_store, cx));
|
|
cx.set_global(GlobalNotificationStore(notification_store));
|
|
}
|
|
|
|
struct GlobalNotificationStore(Entity<NotificationStore>);
|
|
|
|
impl Global for GlobalNotificationStore {}
|
|
|
|
pub struct NotificationStore {
|
|
client: Arc<Client>,
|
|
user_store: Entity<UserStore>,
|
|
channel_store: Entity<ChannelStore>,
|
|
notifications: SumTree<NotificationEntry>,
|
|
loaded_all_notifications: bool,
|
|
_watch_connection_status: Task<Option<()>>,
|
|
_subscriptions: Vec<client::Subscription>,
|
|
}
|
|
|
|
#[derive(Clone, PartialEq, Eq, Debug)]
|
|
pub enum NotificationEvent {
|
|
NotificationsUpdated {
|
|
old_range: Range<usize>,
|
|
new_count: usize,
|
|
},
|
|
NewNotification {
|
|
entry: NotificationEntry,
|
|
},
|
|
NotificationRemoved {
|
|
entry: NotificationEntry,
|
|
},
|
|
NotificationRead {
|
|
entry: NotificationEntry,
|
|
},
|
|
}
|
|
|
|
#[derive(Debug, PartialEq, Eq, Clone)]
|
|
pub struct NotificationEntry {
|
|
pub id: u64,
|
|
pub notification: Notification,
|
|
pub timestamp: OffsetDateTime,
|
|
pub is_read: bool,
|
|
pub response: Option<bool>,
|
|
}
|
|
|
|
#[derive(Clone, Debug, Default)]
|
|
pub struct NotificationSummary {
|
|
max_id: u64,
|
|
count: usize,
|
|
unread_count: usize,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
|
|
struct Count(usize);
|
|
|
|
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, PartialOrd, Ord)]
|
|
struct NotificationId(u64);
|
|
|
|
impl NotificationStore {
|
|
pub fn global(cx: &App) -> Entity<Self> {
|
|
cx.global::<GlobalNotificationStore>().0.clone()
|
|
}
|
|
|
|
pub fn new(client: Arc<Client>, user_store: Entity<UserStore>, cx: &mut Context<Self>) -> Self {
|
|
let mut connection_status = client.status();
|
|
let watch_connection_status = cx.spawn(async move |this, cx| {
|
|
while let Some(status) = connection_status.next().await {
|
|
let this = this.upgrade()?;
|
|
match status {
|
|
client::Status::Connected { .. } => {
|
|
if let Some(task) = this
|
|
.update(cx, |this, cx| this.handle_connect(cx))
|
|
.log_err()?
|
|
{
|
|
task.await.log_err()?;
|
|
}
|
|
}
|
|
_ => this
|
|
.update(cx, |this, cx| this.handle_disconnect(cx))
|
|
.log_err()?,
|
|
}
|
|
}
|
|
Some(())
|
|
});
|
|
|
|
Self {
|
|
channel_store: ChannelStore::global(cx),
|
|
notifications: Default::default(),
|
|
loaded_all_notifications: false,
|
|
_watch_connection_status: watch_connection_status,
|
|
_subscriptions: vec![
|
|
client.add_message_handler(cx.weak_entity(), Self::handle_new_notification),
|
|
client.add_message_handler(cx.weak_entity(), Self::handle_delete_notification),
|
|
],
|
|
user_store,
|
|
client,
|
|
}
|
|
}
|
|
|
|
pub fn notification_count(&self) -> usize {
|
|
self.notifications.summary().count
|
|
}
|
|
|
|
pub fn unread_notification_count(&self) -> usize {
|
|
self.notifications.summary().unread_count
|
|
}
|
|
|
|
// Get the nth newest notification.
|
|
pub fn notification_at(&self, ix: usize) -> Option<&NotificationEntry> {
|
|
let count = self.notifications.summary().count;
|
|
if ix >= count {
|
|
return None;
|
|
}
|
|
let ix = count - 1 - ix;
|
|
let mut cursor = self.notifications.cursor::<Count>(());
|
|
cursor.seek(&Count(ix), Bias::Right);
|
|
cursor.item()
|
|
}
|
|
pub fn notification_for_id(&self, id: u64) -> Option<&NotificationEntry> {
|
|
let mut cursor = self.notifications.cursor::<NotificationId>(());
|
|
cursor.seek(&NotificationId(id), Bias::Left);
|
|
if let Some(item) = cursor.item()
|
|
&& item.id == id
|
|
{
|
|
return Some(item);
|
|
}
|
|
None
|
|
}
|
|
|
|
pub fn load_more_notifications(
|
|
&self,
|
|
clear_old: bool,
|
|
cx: &mut Context<Self>,
|
|
) -> Option<Task<Result<()>>> {
|
|
if self.loaded_all_notifications && !clear_old {
|
|
return None;
|
|
}
|
|
|
|
let before_id = if clear_old {
|
|
None
|
|
} else {
|
|
self.notifications.first().map(|entry| entry.id)
|
|
};
|
|
let request = self.client.request(proto::GetNotifications { before_id });
|
|
Some(cx.spawn(async move |this, cx| {
|
|
let this = this
|
|
.upgrade()
|
|
.context("Notification store was dropped while loading notifications")?;
|
|
|
|
let response = request.await?;
|
|
this.update(cx, |this, _| this.loaded_all_notifications = response.done)?;
|
|
Self::add_notifications(
|
|
this,
|
|
response.notifications,
|
|
AddNotificationsOptions {
|
|
is_new: false,
|
|
clear_old,
|
|
includes_first: response.done,
|
|
},
|
|
cx,
|
|
)
|
|
.await?;
|
|
Ok(())
|
|
}))
|
|
}
|
|
|
|
fn handle_connect(&mut self, cx: &mut Context<Self>) -> Option<Task<Result<()>>> {
|
|
self.notifications = Default::default();
|
|
cx.notify();
|
|
self.load_more_notifications(true, cx)
|
|
}
|
|
|
|
fn handle_disconnect(&mut self, cx: &mut Context<Self>) {
|
|
cx.notify()
|
|
}
|
|
|
|
async fn handle_new_notification(
|
|
this: Entity<Self>,
|
|
envelope: TypedEnvelope<proto::AddNotification>,
|
|
mut cx: AsyncApp,
|
|
) -> Result<()> {
|
|
Self::add_notifications(
|
|
this,
|
|
envelope.payload.notification.into_iter().collect(),
|
|
AddNotificationsOptions {
|
|
is_new: true,
|
|
clear_old: false,
|
|
includes_first: false,
|
|
},
|
|
&mut cx,
|
|
)
|
|
.await
|
|
}
|
|
|
|
async fn handle_delete_notification(
|
|
this: Entity<Self>,
|
|
envelope: TypedEnvelope<proto::DeleteNotification>,
|
|
mut cx: AsyncApp,
|
|
) -> Result<()> {
|
|
this.update(&mut cx, |this, cx| {
|
|
this.splice_notifications([(envelope.payload.notification_id, None)], false, cx);
|
|
Ok(())
|
|
})?
|
|
}
|
|
|
|
async fn add_notifications(
|
|
this: Entity<Self>,
|
|
notifications: Vec<proto::Notification>,
|
|
options: AddNotificationsOptions,
|
|
cx: &mut AsyncApp,
|
|
) -> Result<()> {
|
|
let mut user_ids = Vec::new();
|
|
|
|
let notifications = notifications
|
|
.into_iter()
|
|
.filter_map(|message| {
|
|
Some(NotificationEntry {
|
|
id: message.id,
|
|
is_read: message.is_read,
|
|
timestamp: OffsetDateTime::from_unix_timestamp(message.timestamp as i64)
|
|
.ok()?,
|
|
notification: Notification::from_proto(&message)?,
|
|
response: message.response,
|
|
})
|
|
})
|
|
.collect::<Vec<_>>();
|
|
if notifications.is_empty() {
|
|
return Ok(());
|
|
}
|
|
|
|
for entry in ¬ifications {
|
|
match entry.notification {
|
|
Notification::ChannelInvitation { inviter_id, .. } => {
|
|
user_ids.push(inviter_id);
|
|
}
|
|
Notification::ContactRequest {
|
|
sender_id: requester_id,
|
|
} => {
|
|
user_ids.push(requester_id);
|
|
}
|
|
Notification::ContactRequestAccepted {
|
|
responder_id: contact_id,
|
|
} => {
|
|
user_ids.push(contact_id);
|
|
}
|
|
}
|
|
}
|
|
|
|
let user_store = this.read_with(cx, |this, _| this.user_store.clone())?;
|
|
|
|
user_store
|
|
.update(cx, |store, cx| store.get_users(user_ids, cx))?
|
|
.await?;
|
|
this.update(cx, |this, cx| {
|
|
if options.clear_old {
|
|
cx.emit(NotificationEvent::NotificationsUpdated {
|
|
old_range: 0..this.notifications.summary().count,
|
|
new_count: 0,
|
|
});
|
|
this.notifications = SumTree::default();
|
|
this.loaded_all_notifications = false;
|
|
}
|
|
|
|
if options.includes_first {
|
|
this.loaded_all_notifications = true;
|
|
}
|
|
|
|
this.splice_notifications(
|
|
notifications
|
|
.into_iter()
|
|
.map(|notification| (notification.id, Some(notification))),
|
|
options.is_new,
|
|
cx,
|
|
);
|
|
})
|
|
.log_err();
|
|
|
|
Ok(())
|
|
}
|
|
|
|
fn splice_notifications(
|
|
&mut self,
|
|
notifications: impl IntoIterator<Item = (u64, Option<NotificationEntry>)>,
|
|
is_new: bool,
|
|
cx: &mut Context<NotificationStore>,
|
|
) {
|
|
let mut cursor = self
|
|
.notifications
|
|
.cursor::<Dimensions<NotificationId, Count>>(());
|
|
let mut new_notifications = SumTree::default();
|
|
let mut old_range = 0..0;
|
|
|
|
for (i, (id, new_notification)) in notifications.into_iter().enumerate() {
|
|
new_notifications.append(cursor.slice(&NotificationId(id), Bias::Left), ());
|
|
|
|
if i == 0 {
|
|
old_range.start = cursor.start().1.0;
|
|
}
|
|
|
|
let old_notification = cursor.item();
|
|
if let Some(old_notification) = old_notification {
|
|
if old_notification.id == id {
|
|
cursor.next();
|
|
|
|
if let Some(new_notification) = &new_notification {
|
|
if new_notification.is_read {
|
|
cx.emit(NotificationEvent::NotificationRead {
|
|
entry: new_notification.clone(),
|
|
});
|
|
}
|
|
} else {
|
|
cx.emit(NotificationEvent::NotificationRemoved {
|
|
entry: old_notification.clone(),
|
|
});
|
|
}
|
|
}
|
|
} else if let Some(new_notification) = &new_notification
|
|
&& is_new
|
|
{
|
|
cx.emit(NotificationEvent::NewNotification {
|
|
entry: new_notification.clone(),
|
|
});
|
|
}
|
|
|
|
if let Some(notification) = new_notification {
|
|
new_notifications.push(notification, ());
|
|
}
|
|
}
|
|
|
|
old_range.end = cursor.start().1.0;
|
|
let new_count = new_notifications.summary().count - old_range.start;
|
|
new_notifications.append(cursor.suffix(), ());
|
|
drop(cursor);
|
|
|
|
self.notifications = new_notifications;
|
|
cx.emit(NotificationEvent::NotificationsUpdated {
|
|
old_range,
|
|
new_count,
|
|
});
|
|
}
|
|
|
|
pub fn respond_to_notification(
|
|
&mut self,
|
|
notification: Notification,
|
|
response: bool,
|
|
cx: &mut Context<Self>,
|
|
) {
|
|
match notification {
|
|
Notification::ContactRequest { sender_id } => {
|
|
self.user_store
|
|
.update(cx, |store, cx| {
|
|
store.respond_to_contact_request(sender_id, response, cx)
|
|
})
|
|
.detach();
|
|
}
|
|
Notification::ChannelInvitation { channel_id, .. } => {
|
|
self.channel_store
|
|
.update(cx, |store, cx| {
|
|
store.respond_to_channel_invite(ChannelId(channel_id), response, cx)
|
|
})
|
|
.detach();
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
}
|
|
|
|
impl EventEmitter<NotificationEvent> for NotificationStore {}
|
|
|
|
impl sum_tree::Item for NotificationEntry {
|
|
type Summary = NotificationSummary;
|
|
|
|
fn summary(&self, _cx: ()) -> Self::Summary {
|
|
NotificationSummary {
|
|
max_id: self.id,
|
|
count: 1,
|
|
unread_count: if self.is_read { 0 } else { 1 },
|
|
}
|
|
}
|
|
}
|
|
|
|
impl sum_tree::ContextLessSummary for NotificationSummary {
|
|
fn zero() -> Self {
|
|
Default::default()
|
|
}
|
|
|
|
fn add_summary(&mut self, summary: &Self) {
|
|
self.max_id = self.max_id.max(summary.max_id);
|
|
self.count += summary.count;
|
|
self.unread_count += summary.unread_count;
|
|
}
|
|
}
|
|
|
|
impl sum_tree::Dimension<'_, NotificationSummary> for NotificationId {
|
|
fn zero(_cx: ()) -> Self {
|
|
Default::default()
|
|
}
|
|
|
|
fn add_summary(&mut self, summary: &NotificationSummary, _: ()) {
|
|
debug_assert!(summary.max_id > self.0);
|
|
self.0 = summary.max_id;
|
|
}
|
|
}
|
|
|
|
impl sum_tree::Dimension<'_, NotificationSummary> for Count {
|
|
fn zero(_cx: ()) -> Self {
|
|
Default::default()
|
|
}
|
|
|
|
fn add_summary(&mut self, summary: &NotificationSummary, _: ()) {
|
|
self.0 += summary.count;
|
|
}
|
|
}
|
|
|
|
struct AddNotificationsOptions {
|
|
is_new: bool,
|
|
clear_old: bool,
|
|
includes_first: bool,
|
|
}
|