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

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