diff --git a/src/service/rooms/auth_chain.rs b/src/service/rooms/auth_chain.rs index 97c95f11..3c8e4b38 100644 --- a/src/service/rooms/auth_chain.rs +++ b/src/service/rooms/auth_chain.rs @@ -21,8 +21,9 @@ pub(crate) use data::Data; pub(crate) struct Service { db: &'static dyn Data, + #[allow(clippy::type_complexity)] auth_chain_cache: - Mutex, Arc>>>, + Option, Arc>>>>, } impl Service { @@ -32,7 +33,8 @@ impl Service { ) -> Self { Self { db, - auth_chain_cache: Mutex::new(LruCache::new(auth_chain_cache_size)), + auth_chain_cache: (auth_chain_cache_size > 0) + .then(|| Mutex::new(LruCache::new(auth_chain_cache_size))), } } @@ -42,10 +44,11 @@ impl Service { ) -> Result>>> { let lookup = Lookup::AuthChain; - if let Some(result) = self.auth_chain_cache.lock().unwrap().get_mut(key) - { - METRICS.record_lookup(lookup, FoundIn::Cache); - return Ok(Some(Arc::clone(result))); + if let Some(cache) = &self.auth_chain_cache { + if let Some(result) = cache.lock().unwrap().get_mut(key) { + METRICS.record_lookup(lookup, FoundIn::Cache); + return Ok(Some(Arc::clone(result))); + } } let Some(chain) = self.db.get_cached_eventid_authchain(key)? else { @@ -56,10 +59,9 @@ impl Service { METRICS.record_lookup(lookup, FoundIn::Database); let chain = Arc::new(chain); - self.auth_chain_cache - .lock() - .unwrap() - .insert(vec![key[0]], Arc::clone(&chain)); + if let Some(cache) = &self.auth_chain_cache { + cache.lock().unwrap().insert(vec![key[0]], Arc::clone(&chain)); + } Ok(Some(chain)) } @@ -71,7 +73,9 @@ impl Service { auth_chain: Arc>, ) -> Result<()> { self.db.cache_auth_chain(&key, &auth_chain)?; - self.auth_chain_cache.lock().unwrap().insert(key, auth_chain); + if let Some(cache) = &self.auth_chain_cache { + cache.lock().unwrap().insert(key, auth_chain); + } Ok(()) } diff --git a/src/service/rooms/short.rs b/src/service/rooms/short.rs index 917e5a85..ce4d207c 100644 --- a/src/service/rooms/short.rs +++ b/src/service/rooms/short.rs @@ -37,12 +37,12 @@ pub(crate) use data::Data; pub(crate) struct Service { db: &'static dyn Data, - shorteventid_cache: Mutex>>, - eventidshort_cache: Mutex>, + shorteventid_cache: Option>>>, + eventidshort_cache: Option>>, statekeyshort_cache: - Mutex>, + Option>>, shortstatekey_cache: - Mutex>, + Option>>, } impl Service { @@ -55,18 +55,14 @@ impl Service { ) -> Self { Self { db, - shorteventid_cache: Mutex::new(LruCache::new( - shorteventid_cache_size, - )), - eventidshort_cache: Mutex::new(LruCache::new( - eventidshort_cache_size, - )), - statekeyshort_cache: Mutex::new(LruCache::new( - statekeyshort_cache_size, - )), - shortstatekey_cache: Mutex::new(LruCache::new( - shortstatekey_cache_size, - )), + shorteventid_cache: (shorteventid_cache_size > 0) + .then(|| Mutex::new(LruCache::new(shorteventid_cache_size))), + eventidshort_cache: (eventidshort_cache_size > 0) + .then(|| Mutex::new(LruCache::new(eventidshort_cache_size))), + statekeyshort_cache: (statekeyshort_cache_size > 0) + .then(|| Mutex::new(LruCache::new(statekeyshort_cache_size))), + shortstatekey_cache: (shortstatekey_cache_size > 0) + .then(|| Mutex::new(LruCache::new(shortstatekey_cache_size))), } } @@ -76,11 +72,11 @@ impl Service { ) -> Result { let lookup = Lookup::CreateEventIdToShort; - if let Some(short) = - self.eventidshort_cache.lock().unwrap().get_mut(event_id) - { - METRICS.record_lookup(lookup, FoundIn::Cache); - return Ok(*short); + if let Some(cache) = &self.eventidshort_cache { + if let Some(short) = cache.lock().unwrap().get_mut(event_id) { + METRICS.record_lookup(lookup, FoundIn::Cache); + return Ok(*short); + } } let (short, created) = self.db.get_or_create_shorteventid(event_id)?; @@ -91,10 +87,9 @@ impl Service { METRICS.record_lookup(lookup, FoundIn::Database); } - self.eventidshort_cache - .lock() - .unwrap() - .insert(event_id.to_owned(), short); + if let Some(cache) = &self.eventidshort_cache { + cache.lock().unwrap().insert(event_id.to_owned(), short); + } Ok(short) } @@ -106,14 +101,15 @@ impl Service { ) -> Result> { let lookup = Lookup::StateKeyToShort; - if let Some(short) = self - .statekeyshort_cache - .lock() - .unwrap() - .get_mut(&(event_type.clone(), state_key.to_owned())) - { - METRICS.record_lookup(lookup, FoundIn::Cache); - return Ok(Some(*short)); + if let Some(cache) = &self.statekeyshort_cache { + if let Some(short) = cache + .lock() + .unwrap() + .get_mut(&(event_type.clone(), state_key.to_owned())) + { + METRICS.record_lookup(lookup, FoundIn::Cache); + return Ok(Some(*short)); + } } let short = self.db.get_shortstatekey(event_type, state_key)?; @@ -121,10 +117,12 @@ impl Service { if let Some(short) = short { METRICS.record_lookup(lookup, FoundIn::Database); - self.statekeyshort_cache - .lock() - .unwrap() - .insert((event_type.clone(), state_key.to_owned()), short); + if let Some(cache) = &self.statekeyshort_cache { + cache + .lock() + .unwrap() + .insert((event_type.clone(), state_key.to_owned()), short); + } } else { METRICS.record_lookup(lookup, FoundIn::Nothing); } @@ -139,14 +137,15 @@ impl Service { ) -> Result { let lookup = Lookup::CreateStateKeyToShort; - if let Some(short) = self - .statekeyshort_cache - .lock() - .unwrap() - .get_mut(&(event_type.clone(), state_key.to_owned())) - { - METRICS.record_lookup(lookup, FoundIn::Cache); - return Ok(*short); + if let Some(cache) = &self.statekeyshort_cache { + if let Some(short) = cache + .lock() + .unwrap() + .get_mut(&(event_type.clone(), state_key.to_owned())) + { + METRICS.record_lookup(lookup, FoundIn::Cache); + return Ok(*short); + } } let (short, created) = @@ -158,10 +157,12 @@ impl Service { METRICS.record_lookup(lookup, FoundIn::Database); } - self.statekeyshort_cache - .lock() - .unwrap() - .insert((event_type.clone(), state_key.to_owned()), short); + if let Some(cache) = &self.statekeyshort_cache { + cache + .lock() + .unwrap() + .insert((event_type.clone(), state_key.to_owned()), short); + } Ok(short) } @@ -172,21 +173,19 @@ impl Service { ) -> Result> { let lookup = Lookup::ShortToEventId; - if let Some(id) = - self.shorteventid_cache.lock().unwrap().get_mut(&shorteventid) - { - METRICS.record_lookup(lookup, FoundIn::Cache); - return Ok(Arc::clone(id)); + if let Some(cache) = &self.shorteventid_cache { + if let Some(id) = cache.lock().unwrap().get_mut(&shorteventid) { + METRICS.record_lookup(lookup, FoundIn::Cache); + return Ok(Arc::clone(id)); + } } - let event_id = self.db.get_eventid_from_short(shorteventid)?; METRICS.record_lookup(lookup, FoundIn::Database); - self.shorteventid_cache - .lock() - .unwrap() - .insert(shorteventid, Arc::clone(&event_id)); + if let Some(cache) = &self.shorteventid_cache { + cache.lock().unwrap().insert(shorteventid, Arc::clone(&event_id)); + } Ok(event_id) } @@ -197,21 +196,19 @@ impl Service { ) -> Result<(StateEventType, String)> { let lookup = Lookup::ShortToStateKey; - if let Some(id) = - self.shortstatekey_cache.lock().unwrap().get_mut(&shortstatekey) - { - METRICS.record_lookup(lookup, FoundIn::Cache); - return Ok(id.clone()); + if let Some(cache) = &self.shortstatekey_cache { + if let Some(id) = cache.lock().unwrap().get_mut(&shortstatekey) { + METRICS.record_lookup(lookup, FoundIn::Cache); + return Ok(id.clone()); + } } - let x = self.db.get_statekey_from_short(shortstatekey)?; METRICS.record_lookup(lookup, FoundIn::Database); - self.shortstatekey_cache - .lock() - .unwrap() - .insert(shortstatekey, x.clone()); + if let Some(cache) = &self.shortstatekey_cache { + cache.lock().unwrap().insert(shortstatekey, x.clone()); + } Ok(x) } diff --git a/src/service/rooms/spaces.rs b/src/service/rooms/spaces.rs index ad5f4da0..2011d5b5 100644 --- a/src/service/rooms/spaces.rs +++ b/src/service/rooms/spaces.rs @@ -46,15 +46,15 @@ pub(crate) struct CachedSpaceChunk { pub(crate) struct Service { roomid_spacechunk_cache: - Mutex>>, + Option>>>, } impl Service { pub(crate) fn new(roomid_spacechunk_cache_size: usize) -> Self { Self { - roomid_spacechunk_cache: Mutex::new(LruCache::new( - roomid_spacechunk_cache_size, - )), + roomid_spacechunk_cache: (roomid_spacechunk_cache_size > 0).then( + || Mutex::new(LruCache::new(roomid_spacechunk_cache_size)), + ), } } @@ -90,35 +90,32 @@ impl Service { break; } - if let Some(cached) = self - .roomid_spacechunk_cache - .lock() - .await - .get_mut(¤t_room.clone()) - .as_ref() - { - if let Some(cached) = cached { - let allowed = match &cached.join_rule { - CachedJoinRule::Full(f) => self.handle_join_rule( - f, - sender_user, - ¤t_room, - )?, - }; - if allowed { - if left_to_skip > 0 { - left_to_skip -= 1; - } else { - results.push(cached.chunk.clone()); - } - if rooms_in_path.len() < max_depth { - stack.push(cached.children.clone()); + if let Some(cache) = &self.roomid_spacechunk_cache { + if let Some(cached) = + cache.lock().await.get_mut(¤t_room.clone()).as_ref() + { + if let Some(cached) = cached { + let allowed = match &cached.join_rule { + CachedJoinRule::Full(f) => self.handle_join_rule( + f, + sender_user, + ¤t_room, + )?, + }; + if allowed { + if left_to_skip > 0 { + left_to_skip -= 1; + } else { + results.push(cached.chunk.clone()); + } + if rooms_in_path.len() < max_depth { + stack.push(cached.children.clone()); + } } } + continue; } - continue; } - if let Some(current_shortstatehash) = services().rooms.state.get_room_shortstatehash(¤t_room)? { @@ -201,14 +198,16 @@ impl Service { .transpose()? .unwrap_or(JoinRule::Invite); - self.roomid_spacechunk_cache.lock().await.insert( - current_room.clone(), - Some(CachedSpaceChunk { - chunk, - children: children_ids.clone(), - join_rule: CachedJoinRule::Full(join_rule), - }), - ); + if let Some(cache) = &self.roomid_spacechunk_cache { + cache.lock().await.insert( + current_room.clone(), + Some(CachedSpaceChunk { + chunk, + children: children_ids.clone(), + join_rule: CachedJoinRule::Full(join_rule), + }), + ); + } } if rooms_in_path.len() < max_depth { @@ -307,19 +306,18 @@ impl Service { } } - self.roomid_spacechunk_cache.lock().await.insert( - current_room.clone(), - Some(CachedSpaceChunk { - chunk, - children, - join_rule: CachedJoinRule::Full(join_rule), - }), - ); - } else { - self.roomid_spacechunk_cache - .lock() - .await - .insert(current_room.clone(), None); + if let Some(cache) = &self.roomid_spacechunk_cache { + cache.lock().await.insert( + current_room.clone(), + Some(CachedSpaceChunk { + chunk, + children, + join_rule: CachedJoinRule::Full(join_rule), + }), + ); + } + } else if let Some(cache) = &self.roomid_spacechunk_cache { + cache.lock().await.insert(current_room.clone(), None); } } } @@ -527,7 +525,9 @@ impl Service { } pub(crate) async fn invalidate_cache(&self, room_id: &RoomId) { - self.roomid_spacechunk_cache.lock().await.remove(room_id); + if let Some(cache) = &self.roomid_spacechunk_cache { + cache.lock().await.remove(room_id); + } } fn translate_joinrule(join_rule: &JoinRule) -> Result { diff --git a/src/service/rooms/state_accessor.rs b/src/service/rooms/state_accessor.rs index a3922249..108e510d 100644 --- a/src/service/rooms/state_accessor.rs +++ b/src/service/rooms/state_accessor.rs @@ -40,8 +40,9 @@ pub(crate) use data::Data; pub(crate) struct Service { db: &'static dyn Data, server_visibility_cache: - Mutex>, - user_visibility_cache: Mutex>, + Option>>, + user_visibility_cache: + Option>>, } impl Service { @@ -52,12 +53,11 @@ impl Service { ) -> Self { Self { db, - server_visibility_cache: Mutex::new(LruCache::new( - server_visibility_cache_size, - )), - user_visibility_cache: Mutex::new(LruCache::new( - user_visibility_cache_size, - )), + server_visibility_cache: (server_visibility_cache_size > 0).then( + || Mutex::new(LruCache::new(server_visibility_cache_size)), + ), + user_visibility_cache: (user_visibility_cache_size > 0) + .then(|| Mutex::new(LruCache::new(user_visibility_cache_size))), } } @@ -165,14 +165,15 @@ impl Service { return Ok(true); }; - if let Some(visibility) = self - .server_visibility_cache - .lock() - .unwrap() - .get_mut(&(origin.to_owned(), shortstatehash)) - { - METRICS.record_lookup(lookup, FoundIn::Cache); - return Ok(*visibility); + if let Some(cache) = &self.server_visibility_cache { + if let Some(visibility) = cache + .lock() + .unwrap() + .get_mut(&(origin.to_owned(), shortstatehash)) + { + METRICS.record_lookup(lookup, FoundIn::Cache); + return Ok(*visibility); + } } let history_visibility = self @@ -223,10 +224,13 @@ impl Service { }; METRICS.record_lookup(lookup, FoundIn::Database); - self.server_visibility_cache - .lock() - .unwrap() - .insert((origin.to_owned(), shortstatehash), visibility); + + if let Some(cache) = &self.server_visibility_cache { + cache + .lock() + .unwrap() + .insert((origin.to_owned(), shortstatehash), visibility); + } Ok(visibility) } @@ -246,14 +250,15 @@ impl Service { return Ok(true); }; - if let Some(visibility) = self - .user_visibility_cache - .lock() - .unwrap() - .get_mut(&(user_id.to_owned(), shortstatehash)) - { - METRICS.record_lookup(lookup, FoundIn::Cache); - return Ok(*visibility); + if let Some(cache) = &self.user_visibility_cache { + if let Some(visibility) = cache + .lock() + .unwrap() + .get_mut(&(user_id.to_owned(), shortstatehash)) + { + METRICS.record_lookup(lookup, FoundIn::Cache); + return Ok(*visibility); + } } let currently_member = @@ -296,10 +301,13 @@ impl Service { }; METRICS.record_lookup(lookup, FoundIn::Database); - self.user_visibility_cache - .lock() - .unwrap() - .insert((user_id.to_owned(), shortstatehash), visibility); + + if let Some(cache) = &self.user_visibility_cache { + cache + .lock() + .unwrap() + .insert((user_id.to_owned(), shortstatehash), visibility); + } Ok(visibility) } diff --git a/src/service/rooms/state_compressor.rs b/src/service/rooms/state_compressor.rs index cac73fe0..60fbb9c5 100644 --- a/src/service/rooms/state_compressor.rs +++ b/src/service/rooms/state_compressor.rs @@ -32,7 +32,7 @@ pub(crate) struct Service { #[allow(clippy::type_complexity)] pub(crate) stateinfo_cache: - Mutex>>, + Option>>>, } #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] @@ -78,7 +78,8 @@ impl Service { ) -> Self { Self { db, - stateinfo_cache: Mutex::new(LruCache::new(stateinfo_cache_size)), + stateinfo_cache: (stateinfo_cache_size > 0) + .then(|| Mutex::new(LruCache::new(stateinfo_cache_size))), } } @@ -92,11 +93,11 @@ impl Service { ) -> Result> { let lookup = Lookup::StateInfo; - if let Some(r) = - self.stateinfo_cache.lock().unwrap().get_mut(&shortstatehash) - { - METRICS.record_lookup(lookup, FoundIn::Cache); - return Ok(r.clone()); + if let Some(cache) = &self.stateinfo_cache { + if let Some(r) = cache.lock().unwrap().get_mut(&shortstatehash) { + METRICS.record_lookup(lookup, FoundIn::Cache); + return Ok(r.clone()); + } } let StateDiff { @@ -131,10 +132,10 @@ impl Service { }; METRICS.record_lookup(lookup, FoundIn::Database); - self.stateinfo_cache - .lock() - .unwrap() - .insert(shortstatehash, response.clone()); + + if let Some(cache) = &self.stateinfo_cache { + cache.lock().unwrap().insert(shortstatehash, response.clone()); + } Ok(response) }