Skip to content

Commit

Permalink
Refactoring in the get methods
Browse files Browse the repository at this point in the history
  • Loading branch information
tatsuya6502 committed Feb 27, 2021
1 parent 3a38b70 commit f4376ba
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 50 deletions.
29 changes: 8 additions & 21 deletions src/sync/base_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,31 +120,29 @@ where
Arc<K>: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
let record = |entry, ts| {
self.record_read_op(hash, entry, ts)
.expect("Failed to record a get op");
let record = |op| {
self.record_read_op(op).expect("Failed to record a get op");
};

match self.inner.get(key) {
None => {
record(None, None);
record(ReadOp::Miss(hash));
None
}
Some(entry) => {
let ttl = &self.inner.time_to_live();
let tti = &self.inner.time_to_idle();
let va = self.inner.valid_after();
let i = &self.inner;
let (ttl, tti, va) = (&i.time_to_live(), &i.time_to_idle(), i.valid_after());
let now = self.inner.current_time_from_expiration_clock();
if is_expired_entry_wo(ttl, va, &entry, now)
|| is_expired_entry_ao(tti, va, &entry, now)
{
// Expired entry. Record this access as a cache miss rather than a hit.
record(None, None);
record(ReadOp::Miss(hash));
None
} else {
// Valid entry.
let v = entry.value.clone();
record(Some(entry), Some(now));
record(ReadOp::Hit(hash, entry, now));
Some(v)
}
}
Expand Down Expand Up @@ -202,20 +200,9 @@ where
S: BuildHasher + Clone,
{
#[inline]
fn record_read_op(
&self,
hash: u64,
entry: Option<Arc<ValueEntry<K, V>>>,
timestamp: Option<Instant>,
) -> Result<(), TrySendError<ReadOp<K, V>>> {
use ReadOp::*;
fn record_read_op(&self, op: ReadOp<K, V>) -> Result<(), TrySendError<ReadOp<K, V>>> {
self.apply_reads_if_needed();
let ch = &self.read_op_ch;
let op = if let Some(entry) = entry {
Hit(hash, entry, timestamp.unwrap())
} else {
Miss(hash)
};
match ch.try_send(op) {
// Discard the ReadOp when the channel is full.
Ok(()) | Err(TrySendError::Full(_)) => Ok(()),
Expand Down
39 changes: 10 additions & 29 deletions src/unsync/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ where
Rc<K>: Borrow<Q>,
Q: Hash + Eq + ?Sized,
{
let hash = self.hash(key);
self.frequency_sketch.increment(self.hash(key));

let has_expiry = self.has_expiry();
let timestamp = if has_expiry {
Some(self.current_time_from_expiration_clock())
Expand All @@ -90,34 +91,24 @@ where
self.evict(ts);
}

let (entry, sketch, deqs) = (
self.cache.get_mut(key),
&mut self.frequency_sketch,
&mut self.deques,
);
let (entry, deqs) = (self.cache.get_mut(key), &mut self.deques);

match (entry, has_expiry) {
// Value not found.
(None, _) => {
Self::record_read(sketch, deqs, hash, None, None);
None
}
(None, _) => None,
// Value found, no expiry.
(Some(entry), false) => {
Self::record_read(sketch, deqs, hash, Some(entry), None);
Self::record_hit(deqs, entry, None);
Some(&entry.value)
}
// Value found, need to check if expired.
(Some(entry), true) => {
if Self::is_expired_entry_wo(&self.time_to_live, entry, timestamp.unwrap())
|| Self::is_expired_entry_ao(&self.time_to_idle, entry, timestamp.unwrap())
{
// Expired entry. Record this access as a cache miss rather than a hit.
Self::record_read(sketch, deqs, hash, None, None);
None
} else {
// Valid entry.
Self::record_read(sketch, deqs, hash, Some(entry), timestamp);
Self::record_hit(deqs, entry, timestamp);
Some(&entry.value)
}
}
Expand All @@ -141,7 +132,6 @@ where
if let Some(old_entry) = self.cache.insert(Rc::clone(&key), entry) {
self.handle_update(key, timestamp, old_entry);
} else {
// Insert
let hash = self.hash(&key);
self.handle_insert(key, hash, timestamp);
}
Expand Down Expand Up @@ -240,20 +230,11 @@ where
false
}

fn record_read(
frequency_sketch: &mut FrequencySketch,
deques: &mut Deques<K>,
hash: u64,
entry: Option<&mut ValueEntry<K, V>>,
timestamp: Option<Instant>,
) {
frequency_sketch.increment(hash);
if let Some(entry) = entry {
if let Some(ts) = timestamp {
entry.set_last_accessed(ts);
}
deques.move_to_back_ao(entry)
fn record_hit(deques: &mut Deques<K>, entry: &mut ValueEntry<K, V>, ts: Option<Instant>) {
if let Some(ts) = ts {
entry.set_last_accessed(ts);
}
deques.move_to_back_ao(entry)
}

#[inline]
Expand Down

0 comments on commit f4376ba

Please sign in to comment.