avoid overhead when cache sizes are zero

Don't even try taking locks, inserting or removing anything, etc.
This commit is contained in:
Charles Hall 2024-10-08 22:28:52 -07:00
parent 1148c6004f
commit d42a5ec1f0
No known key found for this signature in database
GPG key ID: 7B8E0645816E07CF
5 changed files with 184 additions and 174 deletions

View file

@ -21,8 +21,9 @@ pub(crate) use data::Data;
pub(crate) struct Service { pub(crate) struct Service {
db: &'static dyn Data, db: &'static dyn Data,
#[allow(clippy::type_complexity)]
auth_chain_cache: auth_chain_cache:
Mutex<LruCache<Vec<ShortEventId>, Arc<HashSet<ShortEventId>>>>, Option<Mutex<LruCache<Vec<ShortEventId>, Arc<HashSet<ShortEventId>>>>>,
} }
impl Service { impl Service {
@ -32,7 +33,8 @@ impl Service {
) -> Self { ) -> Self {
Self { Self {
db, 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,11 +44,12 @@ impl Service {
) -> Result<Option<Arc<HashSet<ShortEventId>>>> { ) -> Result<Option<Arc<HashSet<ShortEventId>>>> {
let lookup = Lookup::AuthChain; let lookup = Lookup::AuthChain;
if let Some(result) = self.auth_chain_cache.lock().unwrap().get_mut(key) if let Some(cache) = &self.auth_chain_cache {
{ if let Some(result) = cache.lock().unwrap().get_mut(key) {
METRICS.record_lookup(lookup, FoundIn::Cache); METRICS.record_lookup(lookup, FoundIn::Cache);
return Ok(Some(Arc::clone(result))); return Ok(Some(Arc::clone(result)));
} }
}
let Some(chain) = self.db.get_cached_eventid_authchain(key)? else { let Some(chain) = self.db.get_cached_eventid_authchain(key)? else {
METRICS.record_lookup(lookup, FoundIn::Nothing); METRICS.record_lookup(lookup, FoundIn::Nothing);
@ -56,10 +59,9 @@ impl Service {
METRICS.record_lookup(lookup, FoundIn::Database); METRICS.record_lookup(lookup, FoundIn::Database);
let chain = Arc::new(chain); let chain = Arc::new(chain);
self.auth_chain_cache if let Some(cache) = &self.auth_chain_cache {
.lock() cache.lock().unwrap().insert(vec![key[0]], Arc::clone(&chain));
.unwrap() }
.insert(vec![key[0]], Arc::clone(&chain));
Ok(Some(chain)) Ok(Some(chain))
} }
@ -71,7 +73,9 @@ impl Service {
auth_chain: Arc<HashSet<ShortEventId>>, auth_chain: Arc<HashSet<ShortEventId>>,
) -> Result<()> { ) -> Result<()> {
self.db.cache_auth_chain(&key, &auth_chain)?; 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(()) Ok(())
} }

View file

@ -37,12 +37,12 @@ pub(crate) use data::Data;
pub(crate) struct Service { pub(crate) struct Service {
db: &'static dyn Data, db: &'static dyn Data,
shorteventid_cache: Mutex<LruCache<ShortEventId, Arc<EventId>>>, shorteventid_cache: Option<Mutex<LruCache<ShortEventId, Arc<EventId>>>>,
eventidshort_cache: Mutex<LruCache<OwnedEventId, ShortEventId>>, eventidshort_cache: Option<Mutex<LruCache<OwnedEventId, ShortEventId>>>,
statekeyshort_cache: statekeyshort_cache:
Mutex<LruCache<(StateEventType, String), ShortStateKey>>, Option<Mutex<LruCache<(StateEventType, String), ShortStateKey>>>,
shortstatekey_cache: shortstatekey_cache:
Mutex<LruCache<ShortStateKey, (StateEventType, String)>>, Option<Mutex<LruCache<ShortStateKey, (StateEventType, String)>>>,
} }
impl Service { impl Service {
@ -55,18 +55,14 @@ impl Service {
) -> Self { ) -> Self {
Self { Self {
db, db,
shorteventid_cache: Mutex::new(LruCache::new( shorteventid_cache: (shorteventid_cache_size > 0)
shorteventid_cache_size, .then(|| Mutex::new(LruCache::new(shorteventid_cache_size))),
)), eventidshort_cache: (eventidshort_cache_size > 0)
eventidshort_cache: Mutex::new(LruCache::new( .then(|| Mutex::new(LruCache::new(eventidshort_cache_size))),
eventidshort_cache_size, statekeyshort_cache: (statekeyshort_cache_size > 0)
)), .then(|| Mutex::new(LruCache::new(statekeyshort_cache_size))),
statekeyshort_cache: Mutex::new(LruCache::new( shortstatekey_cache: (shortstatekey_cache_size > 0)
statekeyshort_cache_size, .then(|| Mutex::new(LruCache::new(shortstatekey_cache_size))),
)),
shortstatekey_cache: Mutex::new(LruCache::new(
shortstatekey_cache_size,
)),
} }
} }
@ -76,12 +72,12 @@ impl Service {
) -> Result<ShortEventId> { ) -> Result<ShortEventId> {
let lookup = Lookup::CreateEventIdToShort; let lookup = Lookup::CreateEventIdToShort;
if let Some(short) = if let Some(cache) = &self.eventidshort_cache {
self.eventidshort_cache.lock().unwrap().get_mut(event_id) if let Some(short) = cache.lock().unwrap().get_mut(event_id) {
{
METRICS.record_lookup(lookup, FoundIn::Cache); METRICS.record_lookup(lookup, FoundIn::Cache);
return Ok(*short); return Ok(*short);
} }
}
let (short, created) = self.db.get_or_create_shorteventid(event_id)?; let (short, created) = self.db.get_or_create_shorteventid(event_id)?;
@ -91,10 +87,9 @@ impl Service {
METRICS.record_lookup(lookup, FoundIn::Database); METRICS.record_lookup(lookup, FoundIn::Database);
} }
self.eventidshort_cache if let Some(cache) = &self.eventidshort_cache {
.lock() cache.lock().unwrap().insert(event_id.to_owned(), short);
.unwrap() }
.insert(event_id.to_owned(), short);
Ok(short) Ok(short)
} }
@ -106,8 +101,8 @@ impl Service {
) -> Result<Option<ShortStateKey>> { ) -> Result<Option<ShortStateKey>> {
let lookup = Lookup::StateKeyToShort; let lookup = Lookup::StateKeyToShort;
if let Some(short) = self if let Some(cache) = &self.statekeyshort_cache {
.statekeyshort_cache if let Some(short) = cache
.lock() .lock()
.unwrap() .unwrap()
.get_mut(&(event_type.clone(), state_key.to_owned())) .get_mut(&(event_type.clone(), state_key.to_owned()))
@ -115,16 +110,19 @@ impl Service {
METRICS.record_lookup(lookup, FoundIn::Cache); METRICS.record_lookup(lookup, FoundIn::Cache);
return Ok(Some(*short)); return Ok(Some(*short));
} }
}
let short = self.db.get_shortstatekey(event_type, state_key)?; let short = self.db.get_shortstatekey(event_type, state_key)?;
if let Some(short) = short { if let Some(short) = short {
METRICS.record_lookup(lookup, FoundIn::Database); METRICS.record_lookup(lookup, FoundIn::Database);
self.statekeyshort_cache if let Some(cache) = &self.statekeyshort_cache {
cache
.lock() .lock()
.unwrap() .unwrap()
.insert((event_type.clone(), state_key.to_owned()), short); .insert((event_type.clone(), state_key.to_owned()), short);
}
} else { } else {
METRICS.record_lookup(lookup, FoundIn::Nothing); METRICS.record_lookup(lookup, FoundIn::Nothing);
} }
@ -139,8 +137,8 @@ impl Service {
) -> Result<ShortStateKey> { ) -> Result<ShortStateKey> {
let lookup = Lookup::CreateStateKeyToShort; let lookup = Lookup::CreateStateKeyToShort;
if let Some(short) = self if let Some(cache) = &self.statekeyshort_cache {
.statekeyshort_cache if let Some(short) = cache
.lock() .lock()
.unwrap() .unwrap()
.get_mut(&(event_type.clone(), state_key.to_owned())) .get_mut(&(event_type.clone(), state_key.to_owned()))
@ -148,6 +146,7 @@ impl Service {
METRICS.record_lookup(lookup, FoundIn::Cache); METRICS.record_lookup(lookup, FoundIn::Cache);
return Ok(*short); return Ok(*short);
} }
}
let (short, created) = let (short, created) =
self.db.get_or_create_shortstatekey(event_type, state_key)?; self.db.get_or_create_shortstatekey(event_type, state_key)?;
@ -158,10 +157,12 @@ impl Service {
METRICS.record_lookup(lookup, FoundIn::Database); METRICS.record_lookup(lookup, FoundIn::Database);
} }
self.statekeyshort_cache if let Some(cache) = &self.statekeyshort_cache {
cache
.lock() .lock()
.unwrap() .unwrap()
.insert((event_type.clone(), state_key.to_owned()), short); .insert((event_type.clone(), state_key.to_owned()), short);
}
Ok(short) Ok(short)
} }
@ -172,21 +173,19 @@ impl Service {
) -> Result<Arc<EventId>> { ) -> Result<Arc<EventId>> {
let lookup = Lookup::ShortToEventId; let lookup = Lookup::ShortToEventId;
if let Some(id) = if let Some(cache) = &self.shorteventid_cache {
self.shorteventid_cache.lock().unwrap().get_mut(&shorteventid) if let Some(id) = cache.lock().unwrap().get_mut(&shorteventid) {
{
METRICS.record_lookup(lookup, FoundIn::Cache); METRICS.record_lookup(lookup, FoundIn::Cache);
return Ok(Arc::clone(id)); return Ok(Arc::clone(id));
} }
}
let event_id = self.db.get_eventid_from_short(shorteventid)?; let event_id = self.db.get_eventid_from_short(shorteventid)?;
METRICS.record_lookup(lookup, FoundIn::Database); METRICS.record_lookup(lookup, FoundIn::Database);
self.shorteventid_cache if let Some(cache) = &self.shorteventid_cache {
.lock() cache.lock().unwrap().insert(shorteventid, Arc::clone(&event_id));
.unwrap() }
.insert(shorteventid, Arc::clone(&event_id));
Ok(event_id) Ok(event_id)
} }
@ -197,21 +196,19 @@ impl Service {
) -> Result<(StateEventType, String)> { ) -> Result<(StateEventType, String)> {
let lookup = Lookup::ShortToStateKey; let lookup = Lookup::ShortToStateKey;
if let Some(id) = if let Some(cache) = &self.shortstatekey_cache {
self.shortstatekey_cache.lock().unwrap().get_mut(&shortstatekey) if let Some(id) = cache.lock().unwrap().get_mut(&shortstatekey) {
{
METRICS.record_lookup(lookup, FoundIn::Cache); METRICS.record_lookup(lookup, FoundIn::Cache);
return Ok(id.clone()); return Ok(id.clone());
} }
}
let x = self.db.get_statekey_from_short(shortstatekey)?; let x = self.db.get_statekey_from_short(shortstatekey)?;
METRICS.record_lookup(lookup, FoundIn::Database); METRICS.record_lookup(lookup, FoundIn::Database);
self.shortstatekey_cache if let Some(cache) = &self.shortstatekey_cache {
.lock() cache.lock().unwrap().insert(shortstatekey, x.clone());
.unwrap() }
.insert(shortstatekey, x.clone());
Ok(x) Ok(x)
} }

View file

@ -46,15 +46,15 @@ pub(crate) struct CachedSpaceChunk {
pub(crate) struct Service { pub(crate) struct Service {
roomid_spacechunk_cache: roomid_spacechunk_cache:
Mutex<LruCache<OwnedRoomId, Option<CachedSpaceChunk>>>, Option<Mutex<LruCache<OwnedRoomId, Option<CachedSpaceChunk>>>>,
} }
impl Service { impl Service {
pub(crate) fn new(roomid_spacechunk_cache_size: usize) -> Self { pub(crate) fn new(roomid_spacechunk_cache_size: usize) -> Self {
Self { Self {
roomid_spacechunk_cache: Mutex::new(LruCache::new( roomid_spacechunk_cache: (roomid_spacechunk_cache_size > 0).then(
roomid_spacechunk_cache_size, || Mutex::new(LruCache::new(roomid_spacechunk_cache_size)),
)), ),
} }
} }
@ -90,12 +90,9 @@ impl Service {
break; break;
} }
if let Some(cached) = self if let Some(cache) = &self.roomid_spacechunk_cache {
.roomid_spacechunk_cache if let Some(cached) =
.lock() cache.lock().await.get_mut(&current_room.clone()).as_ref()
.await
.get_mut(&current_room.clone())
.as_ref()
{ {
if let Some(cached) = cached { if let Some(cached) = cached {
let allowed = match &cached.join_rule { let allowed = match &cached.join_rule {
@ -118,7 +115,7 @@ impl Service {
} }
continue; continue;
} }
}
if let Some(current_shortstatehash) = if let Some(current_shortstatehash) =
services().rooms.state.get_room_shortstatehash(&current_room)? services().rooms.state.get_room_shortstatehash(&current_room)?
{ {
@ -201,7 +198,8 @@ impl Service {
.transpose()? .transpose()?
.unwrap_or(JoinRule::Invite); .unwrap_or(JoinRule::Invite);
self.roomid_spacechunk_cache.lock().await.insert( if let Some(cache) = &self.roomid_spacechunk_cache {
cache.lock().await.insert(
current_room.clone(), current_room.clone(),
Some(CachedSpaceChunk { Some(CachedSpaceChunk {
chunk, chunk,
@ -210,6 +208,7 @@ impl Service {
}), }),
); );
} }
}
if rooms_in_path.len() < max_depth { if rooms_in_path.len() < max_depth {
stack.push(children_ids); stack.push(children_ids);
@ -307,7 +306,8 @@ impl Service {
} }
} }
self.roomid_spacechunk_cache.lock().await.insert( if let Some(cache) = &self.roomid_spacechunk_cache {
cache.lock().await.insert(
current_room.clone(), current_room.clone(),
Some(CachedSpaceChunk { Some(CachedSpaceChunk {
chunk, chunk,
@ -315,11 +315,9 @@ impl Service {
join_rule: CachedJoinRule::Full(join_rule), join_rule: CachedJoinRule::Full(join_rule),
}), }),
); );
} else { }
self.roomid_spacechunk_cache } else if let Some(cache) = &self.roomid_spacechunk_cache {
.lock() cache.lock().await.insert(current_room.clone(), None);
.await
.insert(current_room.clone(), None);
} }
} }
} }
@ -527,7 +525,9 @@ impl Service {
} }
pub(crate) async fn invalidate_cache(&self, room_id: &RoomId) { 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<SpaceRoomJoinRule> { fn translate_joinrule(join_rule: &JoinRule) -> Result<SpaceRoomJoinRule> {

View file

@ -40,8 +40,9 @@ pub(crate) use data::Data;
pub(crate) struct Service { pub(crate) struct Service {
db: &'static dyn Data, db: &'static dyn Data,
server_visibility_cache: server_visibility_cache:
Mutex<LruCache<(OwnedServerName, ShortStateHash), bool>>, Option<Mutex<LruCache<(OwnedServerName, ShortStateHash), bool>>>,
user_visibility_cache: Mutex<LruCache<(OwnedUserId, ShortStateHash), bool>>, user_visibility_cache:
Option<Mutex<LruCache<(OwnedUserId, ShortStateHash), bool>>>,
} }
impl Service { impl Service {
@ -52,12 +53,11 @@ impl Service {
) -> Self { ) -> Self {
Self { Self {
db, db,
server_visibility_cache: Mutex::new(LruCache::new( server_visibility_cache: (server_visibility_cache_size > 0).then(
server_visibility_cache_size, || Mutex::new(LruCache::new(server_visibility_cache_size)),
)), ),
user_visibility_cache: Mutex::new(LruCache::new( user_visibility_cache: (user_visibility_cache_size > 0)
user_visibility_cache_size, .then(|| Mutex::new(LruCache::new(user_visibility_cache_size))),
)),
} }
} }
@ -165,8 +165,8 @@ impl Service {
return Ok(true); return Ok(true);
}; };
if let Some(visibility) = self if let Some(cache) = &self.server_visibility_cache {
.server_visibility_cache if let Some(visibility) = cache
.lock() .lock()
.unwrap() .unwrap()
.get_mut(&(origin.to_owned(), shortstatehash)) .get_mut(&(origin.to_owned(), shortstatehash))
@ -174,6 +174,7 @@ impl Service {
METRICS.record_lookup(lookup, FoundIn::Cache); METRICS.record_lookup(lookup, FoundIn::Cache);
return Ok(*visibility); return Ok(*visibility);
} }
}
let history_visibility = self let history_visibility = self
.state_get( .state_get(
@ -223,10 +224,13 @@ impl Service {
}; };
METRICS.record_lookup(lookup, FoundIn::Database); METRICS.record_lookup(lookup, FoundIn::Database);
self.server_visibility_cache
if let Some(cache) = &self.server_visibility_cache {
cache
.lock() .lock()
.unwrap() .unwrap()
.insert((origin.to_owned(), shortstatehash), visibility); .insert((origin.to_owned(), shortstatehash), visibility);
}
Ok(visibility) Ok(visibility)
} }
@ -246,8 +250,8 @@ impl Service {
return Ok(true); return Ok(true);
}; };
if let Some(visibility) = self if let Some(cache) = &self.user_visibility_cache {
.user_visibility_cache if let Some(visibility) = cache
.lock() .lock()
.unwrap() .unwrap()
.get_mut(&(user_id.to_owned(), shortstatehash)) .get_mut(&(user_id.to_owned(), shortstatehash))
@ -255,6 +259,7 @@ impl Service {
METRICS.record_lookup(lookup, FoundIn::Cache); METRICS.record_lookup(lookup, FoundIn::Cache);
return Ok(*visibility); return Ok(*visibility);
} }
}
let currently_member = let currently_member =
services().rooms.state_cache.is_joined(user_id, room_id)?; services().rooms.state_cache.is_joined(user_id, room_id)?;
@ -296,10 +301,13 @@ impl Service {
}; };
METRICS.record_lookup(lookup, FoundIn::Database); METRICS.record_lookup(lookup, FoundIn::Database);
self.user_visibility_cache
if let Some(cache) = &self.user_visibility_cache {
cache
.lock() .lock()
.unwrap() .unwrap()
.insert((user_id.to_owned(), shortstatehash), visibility); .insert((user_id.to_owned(), shortstatehash), visibility);
}
Ok(visibility) Ok(visibility)
} }

View file

@ -32,7 +32,7 @@ pub(crate) struct Service {
#[allow(clippy::type_complexity)] #[allow(clippy::type_complexity)]
pub(crate) stateinfo_cache: pub(crate) stateinfo_cache:
Mutex<LruCache<ShortStateHash, Vec<CompressedStateLayer>>>, Option<Mutex<LruCache<ShortStateHash, Vec<CompressedStateLayer>>>>,
} }
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
@ -78,7 +78,8 @@ impl Service {
) -> Self { ) -> Self {
Self { Self {
db, 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,12 +93,12 @@ impl Service {
) -> Result<Vec<CompressedStateLayer>> { ) -> Result<Vec<CompressedStateLayer>> {
let lookup = Lookup::StateInfo; let lookup = Lookup::StateInfo;
if let Some(r) = if let Some(cache) = &self.stateinfo_cache {
self.stateinfo_cache.lock().unwrap().get_mut(&shortstatehash) if let Some(r) = cache.lock().unwrap().get_mut(&shortstatehash) {
{
METRICS.record_lookup(lookup, FoundIn::Cache); METRICS.record_lookup(lookup, FoundIn::Cache);
return Ok(r.clone()); return Ok(r.clone());
} }
}
let StateDiff { let StateDiff {
parent, parent,
@ -131,10 +132,10 @@ impl Service {
}; };
METRICS.record_lookup(lookup, FoundIn::Database); METRICS.record_lookup(lookup, FoundIn::Database);
self.stateinfo_cache
.lock() if let Some(cache) = &self.stateinfo_cache {
.unwrap() cache.lock().unwrap().insert(shortstatehash, response.clone());
.insert(shortstatehash, response.clone()); }
Ok(response) Ok(response)
} }