Massively improves the boot speed by: (#41)
1. ignoring duplicated events coming to the client 2. avoiding checking for trust in disk for every event (uses a simple cache)
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
use std::{
|
use std::{
|
||||||
cmp::Reverse,
|
cmp::Reverse,
|
||||||
collections::{BTreeMap, HashMap},
|
collections::{BTreeMap, HashMap, HashSet}
|
||||||
};
|
};
|
||||||
|
|
||||||
use account::Account;
|
use account::Account;
|
||||||
@@ -181,6 +181,7 @@ impl ChatRegistry {
|
|||||||
let events = send_events.merge(recv_events);
|
let events = send_events.merge(recv_events);
|
||||||
|
|
||||||
let mut room_map: HashMap<u64, (Event, usize, bool)> = HashMap::new();
|
let mut room_map: HashMap<u64, (Event, usize, bool)> = HashMap::new();
|
||||||
|
let mut trusted_keys: HashSet<PublicKey> = HashSet::new();
|
||||||
|
|
||||||
// Process each event and group by room hash
|
// Process each event and group by room hash
|
||||||
for event in events
|
for event in events
|
||||||
@@ -189,10 +190,18 @@ impl ChatRegistry {
|
|||||||
{
|
{
|
||||||
let hash = room_hash(&event);
|
let hash = room_hash(&event);
|
||||||
|
|
||||||
|
let mut is_trust = trusted_keys.contains(&event.pubkey);
|
||||||
|
|
||||||
|
if is_trust == false {
|
||||||
// Check if room's author is seen in any contact list
|
// Check if room's author is seen in any contact list
|
||||||
let filter = Filter::new().kind(Kind::ContactList).pubkey(event.pubkey);
|
let filter = Filter::new().kind(Kind::ContactList).pubkey(event.pubkey);
|
||||||
// If room's author is seen at least once, mark as trusted
|
// If room's author is seen at least once, mark as trusted
|
||||||
let is_trust = client.database().count(filter).await? >= 1;
|
is_trust = client.database().count(filter).await? >= 1;
|
||||||
|
|
||||||
|
if is_trust {
|
||||||
|
trusted_keys.insert(event.pubkey);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
room_map
|
room_map
|
||||||
.entry(hash)
|
.entry(hash)
|
||||||
|
|||||||
@@ -147,6 +147,8 @@ fn main() {
|
|||||||
let new_id = SubscriptionId::new(NEW_MESSAGE_SUB_ID);
|
let new_id = SubscriptionId::new(NEW_MESSAGE_SUB_ID);
|
||||||
let mut notifications = client.notifications();
|
let mut notifications = client.notifications();
|
||||||
|
|
||||||
|
let mut processed_events: HashSet<EventId> = HashSet::new();
|
||||||
|
|
||||||
while let Ok(notification) = notifications.recv().await {
|
while let Ok(notification) = notifications.recv().await {
|
||||||
if let RelayPoolNotification::Message { message, .. } = notification {
|
if let RelayPoolNotification::Message { message, .. } = notification {
|
||||||
match message {
|
match message {
|
||||||
@@ -154,6 +156,9 @@ fn main() {
|
|||||||
event,
|
event,
|
||||||
subscription_id,
|
subscription_id,
|
||||||
} => {
|
} => {
|
||||||
|
if processed_events.contains(&event.id) { continue }
|
||||||
|
processed_events.insert(event.id);
|
||||||
|
|
||||||
match event.kind {
|
match event.kind {
|
||||||
Kind::GiftWrap => {
|
Kind::GiftWrap => {
|
||||||
let event = match get_unwrapped(event.id).await {
|
let event = match get_unwrapped(event.id).await {
|
||||||
|
|||||||
Reference in New Issue
Block a user