Skip to content

Commit c935301

Browse files
lutterclaude
andcommitted
graph: Replace std::sync::RwLock with parking_lot::RwLock in TimedCache
Replace std::sync::RwLock with parking_lot::RwLock in TimedCache for faster lock acquisition on cache gets and sets. parking_lot's RwLock uses efficient spinning before parking, reducing contention. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent ed3fd40 commit c935301

1 file changed

Lines changed: 6 additions & 6 deletions

File tree

graph/src/util/timed_cache.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ use std::{
33
cmp::Eq,
44
collections::HashMap,
55
hash::Hash,
6-
sync::{Arc, RwLock},
6+
sync::Arc,
77
time::{Duration, Instant},
88
};
99

10+
use crate::parking_lot::RwLock;
11+
1012
/// Caching of values for a specified amount of time
1113
#[derive(Debug)]
1214
struct CacheEntry<V> {
@@ -49,7 +51,7 @@ impl<K, V> TimedCache<K, V> {
4951
K: Borrow<Q> + Eq + Hash,
5052
Q: Hash + Eq + ?Sized,
5153
{
52-
match self.entries.read().unwrap().get(key) {
54+
match self.entries.read().get(key) {
5355
Some(CacheEntry { value, expires }) if expires >= &now => Some(value.clone()),
5456
_ => None,
5557
}
@@ -72,11 +74,11 @@ impl<K, V> TimedCache<K, V> {
7274
value,
7375
expires: now + self.ttl,
7476
};
75-
self.entries.write().unwrap().insert(key, entry);
77+
self.entries.write().insert(key, entry);
7678
}
7779

7880
pub fn clear(&self) {
79-
self.entries.write().unwrap().clear();
81+
self.entries.write().clear();
8082
}
8183

8284
pub fn find<F>(&self, pred: F) -> Option<Arc<V>>
@@ -85,7 +87,6 @@ impl<K, V> TimedCache<K, V> {
8587
{
8688
self.entries
8789
.read()
88-
.unwrap()
8990
.values()
9091
.find(move |entry| pred(entry.value.as_ref()))
9192
.map(|entry| entry.value.clone())
@@ -101,7 +102,6 @@ impl<K, V> TimedCache<K, V> {
101102
{
102103
self.entries
103104
.write()
104-
.unwrap()
105105
.remove(key)
106106
.map(|CacheEntry { value, expires }| (value, expires >= Instant::now()))
107107
}

0 commit comments

Comments
 (0)