Skip to content

Commit 11f17d3

Browse files
committed
Refactor conditional checks for improved readability in cache data structures
- Simplified conditional checks in `clock_ring.rs`, `frequency_buckets.rs`, and `ghost_list.rs` to enhance code clarity and maintainability. - Updated the `pop_lfu_internal` method in `heap_lfu.rs` to improve the structure of the return condition, ensuring better readability while maintaining functionality.
1 parent 21ae531 commit 11f17d3

4 files changed

Lines changed: 40 additions & 36 deletions

File tree

src/ds/clock_ring.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -393,10 +393,10 @@ where
393393
let cap = self.capacity();
394394
for offset in 0..cap {
395395
let idx = (self.hand + offset) % cap;
396-
if let Some(entry) = self.slots.get(idx).and_then(|slot| slot.as_ref())
397-
&& !entry.referenced
398-
{
399-
return Some((&entry.key, &entry.value));
396+
if let Some(entry) = self.slots.get(idx).and_then(|slot| slot.as_ref()) {
397+
if !entry.referenced {
398+
return Some((&entry.key, &entry.value));
399+
}
400400
}
401401
}
402402
None

src/ds/frequency_buckets.rs

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -710,28 +710,28 @@ where
710710
};
711711
self.buckets.insert(freq, bucket);
712712

713-
if let Some(prev) = prev
714-
&& let Some(prev_bucket) = self.buckets.get_mut(&prev)
715-
{
716-
prev_bucket.next = Some(freq);
713+
if let Some(prev) = prev {
714+
if let Some(prev_bucket) = self.buckets.get_mut(&prev) {
715+
prev_bucket.next = Some(freq);
716+
}
717717
}
718-
if let Some(next) = next
719-
&& let Some(next_bucket) = self.buckets.get_mut(&next)
720-
{
721-
next_bucket.prev = Some(freq);
718+
if let Some(next) = next {
719+
if let Some(next_bucket) = self.buckets.get_mut(&next) {
720+
next_bucket.prev = Some(freq);
721+
}
722722
}
723723
}
724724

725725
fn remove_bucket(&mut self, freq: u64, prev: Option<u64>, next: Option<u64>) {
726-
if let Some(prev) = prev
727-
&& let Some(prev_bucket) = self.buckets.get_mut(&prev)
728-
{
729-
prev_bucket.next = next;
726+
if let Some(prev) = prev {
727+
if let Some(prev_bucket) = self.buckets.get_mut(&prev) {
728+
prev_bucket.next = next;
729+
}
730730
}
731-
if let Some(next) = next
732-
&& let Some(next_bucket) = self.buckets.get_mut(&next)
733-
{
734-
next_bucket.prev = prev;
731+
if let Some(next) = next {
732+
if let Some(next_bucket) = self.buckets.get_mut(&next) {
733+
next_bucket.prev = prev;
734+
}
735735
}
736736
self.buckets.remove(&freq);
737737
}
@@ -1023,9 +1023,11 @@ where
10231023
let mut best: Option<(usize, u64)> = None;
10241024
for (idx, shard) in self.shards.iter().enumerate() {
10251025
let buckets = shard.read();
1026-
if let Some(freq) = buckets.min_freq()
1027-
&& best.is_none_or(|(_, best_freq)| freq < best_freq)
1028-
{
1026+
let Some(freq) = buckets.min_freq() else {
1027+
continue;
1028+
};
1029+
let is_better = best.is_none_or(|(_, best_freq)| freq < best_freq);
1030+
if is_better {
10291031
best = Some((idx, freq));
10301032
}
10311033
}
@@ -1039,9 +1041,11 @@ where
10391041
let mut best: Option<(usize, u64)> = None;
10401042
for (idx, shard) in self.shards.iter().enumerate() {
10411043
let buckets = shard.read();
1042-
if let Some(freq) = buckets.min_freq()
1043-
&& best.is_none_or(|(_, best_freq)| freq < best_freq)
1044-
{
1044+
let Some(freq) = buckets.min_freq() else {
1045+
continue;
1046+
};
1047+
let is_better = best.is_none_or(|(_, best_freq)| freq < best_freq);
1048+
if is_better {
10451049
best = Some((idx, freq));
10461050
}
10471051
}

src/ds/ghost_list.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ where
8080
return;
8181
}
8282

83-
if self.list.len() >= self.capacity
84-
&& let Some(old_key) = self.list.pop_back()
85-
{
86-
self.index.remove(&old_key);
83+
if self.list.len() >= self.capacity {
84+
if let Some(old_key) = self.list.pop_back() {
85+
self.index.remove(&old_key);
86+
}
8787
}
8888

8989
let id = self.list.push_front(key.clone());

src/policy/heap_lfu.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -366,12 +366,12 @@ where
366366
fn pop_lfu_internal(&mut self) -> Option<(K, u64)> {
367367
let mut stale_pops = 0usize;
368368
while let Some(Reverse((heap_freq, key))) = self.freq_heap.peek() {
369-
if let Some(&current_freq) = self.frequencies.get(key)
370-
&& *heap_freq == current_freq
371-
{
372-
// This is a valid (non-stale) entry
373-
let Reverse((freq, key)) = self.freq_heap.pop().unwrap();
374-
return Some((key, freq));
369+
if let Some(&current_freq) = self.frequencies.get(key) {
370+
if *heap_freq == current_freq {
371+
// This is a valid (non-stale) entry
372+
let Reverse((freq, key)) = self.freq_heap.pop().unwrap();
373+
return Some((key, freq));
374+
}
375375
}
376376

377377
// This entry is stale (key doesn't exist or frequency changed)

0 commit comments

Comments
 (0)