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 {
db: &'static dyn Data,
#[allow(clippy::type_complexity)]
auth_chain_cache:
Mutex<LruCache<Vec<ShortEventId>, Arc<HashSet<ShortEventId>>>>,
Option<Mutex<LruCache<Vec<ShortEventId>, Arc<HashSet<ShortEventId>>>>>,
}
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,11 +44,12 @@ impl Service {
) -> Result<Option<Arc<HashSet<ShortEventId>>>> {
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);
return Ok(Some(Arc::clone(result)));
}
}
let Some(chain) = self.db.get_cached_eventid_authchain(key)? else {
METRICS.record_lookup(lookup, FoundIn::Nothing);
@ -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<HashSet<ShortEventId>>,
) -> 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(())
}

View file

@ -37,12 +37,12 @@ pub(crate) use data::Data;
pub(crate) struct Service {
db: &'static dyn Data,
shorteventid_cache: Mutex<LruCache<ShortEventId, Arc<EventId>>>,
eventidshort_cache: Mutex<LruCache<OwnedEventId, ShortEventId>>,
shorteventid_cache: Option<Mutex<LruCache<ShortEventId, Arc<EventId>>>>,
eventidshort_cache: Option<Mutex<LruCache<OwnedEventId, ShortEventId>>>,
statekeyshort_cache:
Mutex<LruCache<(StateEventType, String), ShortStateKey>>,
Option<Mutex<LruCache<(StateEventType, String), ShortStateKey>>>,
shortstatekey_cache:
Mutex<LruCache<ShortStateKey, (StateEventType, String)>>,
Option<Mutex<LruCache<ShortStateKey, (StateEventType, String)>>>,
}
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,12 +72,12 @@ impl Service {
) -> Result<ShortEventId> {
let lookup = Lookup::CreateEventIdToShort;
if let Some(short) =
self.eventidshort_cache.lock().unwrap().get_mut(event_id)
{
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,8 +101,8 @@ impl Service {
) -> Result<Option<ShortStateKey>> {
let lookup = Lookup::StateKeyToShort;
if let Some(short) = self
.statekeyshort_cache
if let Some(cache) = &self.statekeyshort_cache {
if let Some(short) = cache
.lock()
.unwrap()
.get_mut(&(event_type.clone(), state_key.to_owned()))
@ -115,16 +110,19 @@ impl Service {
METRICS.record_lookup(lookup, FoundIn::Cache);
return Ok(Some(*short));
}
}
let short = self.db.get_shortstatekey(event_type, state_key)?;
if let Some(short) = short {
METRICS.record_lookup(lookup, FoundIn::Database);
self.statekeyshort_cache
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,8 +137,8 @@ impl Service {
) -> Result<ShortStateKey> {
let lookup = Lookup::CreateStateKeyToShort;
if let Some(short) = self
.statekeyshort_cache
if let Some(cache) = &self.statekeyshort_cache {
if let Some(short) = cache
.lock()
.unwrap()
.get_mut(&(event_type.clone(), state_key.to_owned()))
@ -148,6 +146,7 @@ impl Service {
METRICS.record_lookup(lookup, FoundIn::Cache);
return Ok(*short);
}
}
let (short, created) =
self.db.get_or_create_shortstatekey(event_type, state_key)?;
@ -158,10 +157,12 @@ impl Service {
METRICS.record_lookup(lookup, FoundIn::Database);
}
self.statekeyshort_cache
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<Arc<EventId>> {
let lookup = Lookup::ShortToEventId;
if let Some(id) =
self.shorteventid_cache.lock().unwrap().get_mut(&shorteventid)
{
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)
{
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)
}

View file

@ -46,15 +46,15 @@ pub(crate) struct CachedSpaceChunk {
pub(crate) struct Service {
roomid_spacechunk_cache:
Mutex<LruCache<OwnedRoomId, Option<CachedSpaceChunk>>>,
Option<Mutex<LruCache<OwnedRoomId, Option<CachedSpaceChunk>>>>,
}
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,12 +90,9 @@ impl Service {
break;
}
if let Some(cached) = self
.roomid_spacechunk_cache
.lock()
.await
.get_mut(&current_room.clone())
.as_ref()
if let Some(cache) = &self.roomid_spacechunk_cache {
if let Some(cached) =
cache.lock().await.get_mut(&current_room.clone()).as_ref()
{
if let Some(cached) = cached {
let allowed = match &cached.join_rule {
@ -118,7 +115,7 @@ impl Service {
}
continue;
}
}
if let Some(current_shortstatehash) =
services().rooms.state.get_room_shortstatehash(&current_room)?
{
@ -201,7 +198,8 @@ impl Service {
.transpose()?
.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(),
Some(CachedSpaceChunk {
chunk,
@ -210,6 +208,7 @@ impl Service {
}),
);
}
}
if rooms_in_path.len() < max_depth {
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(),
Some(CachedSpaceChunk {
chunk,
@ -315,11 +315,9 @@ impl Service {
join_rule: CachedJoinRule::Full(join_rule),
}),
);
} else {
self.roomid_spacechunk_cache
.lock()
.await
.insert(current_room.clone(), None);
}
} 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<SpaceRoomJoinRule> {

View file

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

View file

@ -32,7 +32,7 @@ pub(crate) struct Service {
#[allow(clippy::type_complexity)]
pub(crate) stateinfo_cache:
Mutex<LruCache<ShortStateHash, Vec<CompressedStateLayer>>>,
Option<Mutex<LruCache<ShortStateHash, Vec<CompressedStateLayer>>>>,
}
#[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,12 +93,12 @@ impl Service {
) -> Result<Vec<CompressedStateLayer>> {
let lookup = Lookup::StateInfo;
if let Some(r) =
self.stateinfo_cache.lock().unwrap().get_mut(&shortstatehash)
{
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 {
parent,
@ -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)
}