improvement: more auth chain caching

This commit is contained in:
Timo Kösters 2021-08-04 18:30:56 +02:00
parent ab7835dedb
commit b813c34642
No known key found for this signature in database
GPG key ID: 24DA7517711A2BA4
2 changed files with 13 additions and 8 deletions

View file

@ -87,7 +87,7 @@ pub struct Rooms {
pub(super) referencedevents: Arc<dyn Tree>,
pub(super) pdu_cache: Mutex<LruCache<EventId, Arc<PduEvent>>>,
pub(super) auth_chain_cache: Mutex<LruCache<EventId, HashSet<EventId>>>,
pub(super) auth_chain_cache: Mutex<LruCache<Vec<EventId>, HashSet<EventId>>>,
}
impl Rooms {
@ -2618,7 +2618,7 @@ impl Rooms {
#[tracing::instrument(skip(self))]
pub fn auth_chain_cache(
&self,
) -> std::sync::MutexGuard<'_, LruCache<EventId, HashSet<EventId>>> {
) -> std::sync::MutexGuard<'_, LruCache<Vec<EventId>, HashSet<EventId>>> {
self.auth_chain_cache.lock().unwrap()
}
}

View file

@ -1727,9 +1727,13 @@ fn get_auth_chain(starting_events: Vec<EventId>, db: &Database) -> Result<HashSe
let mut full_auth_chain = HashSet::new();
let mut cache = db.rooms.auth_chain_cache();
for event_id in starting_events {
let auth_chain = if let Some(cached) = cache.get_mut(&event_id) {
cached.clone()
if let Some(cached) = cache.get_mut(&starting_events) {
return Ok(cached.clone());
}
for event_id in &starting_events {
if let Some(cached) = cache.get_mut(&[event_id.clone()][..]) {
full_auth_chain.extend(cached.iter().cloned());
} else {
drop(cache);
let start = Instant::now();
@ -1741,14 +1745,15 @@ fn get_auth_chain(starting_events: Vec<EventId>, db: &Database) -> Result<HashSe
cache = db.rooms.auth_chain_cache();
cache.insert(event_id, auth_chain.clone());
cache.insert(vec![event_id.clone()], auth_chain.clone());
auth_chain
full_auth_chain.extend(auth_chain);
};
full_auth_chain.extend(auth_chain);
}
cache.insert(starting_events, full_auth_chain.clone());
Ok(full_auth_chain)
}