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

@ -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)
}