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,10 +44,11 @@ impl Service {
) -> Result<Option<Arc<HashSet<ShortEventId>>>> {
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<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,11 +72,11 @@ impl Service {
) -> Result<ShortEventId> {
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<Option<ShortStateKey>> {
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<ShortStateKey> {
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<Arc<EventId>> {
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)
}

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,35 +90,32 @@ impl Service {
break;
}
if let Some(cached) = self
.roomid_spacechunk_cache
.lock()
.await
.get_mut(&current_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,
&current_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(&current_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,
&current_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(&current_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<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,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)
}

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,11 +93,11 @@ impl Service {
) -> Result<Vec<CompressedStateLayer>> {
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)
}