fix: total ordering for Priority
This commit is contained in:
parent
8cc0d28402
commit
476a8fb445
3 changed files with 42 additions and 39 deletions
|
|
@ -15,6 +15,10 @@ const LONGTERM_CAP: usize = 100;
|
|||
pub struct RecentUseStorage {
|
||||
long_term: HashMap<u64, usize>,
|
||||
short_term: HashMap<u64, usize>,
|
||||
/// used for normalizing individual scores
|
||||
max_long_term: usize,
|
||||
/// used for normalizing individual scores
|
||||
max_short_term: usize,
|
||||
}
|
||||
|
||||
fn hash_key<K: Hash>(key: K) -> u64 {
|
||||
|
|
@ -26,8 +30,11 @@ fn hash_key<K: Hash>(key: K) -> u64 {
|
|||
impl RecentUseStorage {
|
||||
pub fn add<K: Hash>(&mut self, exec: &K) {
|
||||
let key = hash_key(exec);
|
||||
*self.long_term.entry(key).or_insert(0) += 1;
|
||||
let entry = self.long_term.entry(key).or_insert(0);
|
||||
*entry += 1;
|
||||
self.max_long_term = self.max_long_term.max(*entry);
|
||||
let short_term_idx = self.short_term.values().max().unwrap_or(&0) + 1;
|
||||
self.max_short_term = self.max_short_term.max(short_term_idx);
|
||||
self.short_term.insert(key, short_term_idx);
|
||||
self.trim();
|
||||
}
|
||||
|
|
@ -39,6 +46,8 @@ impl RecentUseStorage {
|
|||
}
|
||||
|
||||
while self.long_term.values().sum::<usize>() > LONGTERM_CAP {
|
||||
self.max_long_term /= 2;
|
||||
|
||||
let mut delete_keys = Vec::new();
|
||||
for (k, v) in &mut self.long_term {
|
||||
*v /= 2;
|
||||
|
|
@ -52,12 +61,14 @@ impl RecentUseStorage {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn get_recent<K: Hash>(&self, exec: &K) -> usize {
|
||||
self.short_term.get(&hash_key(exec)).copied().unwrap_or(0)
|
||||
pub fn get_recent<K: Hash>(&self, exec: &K) -> f64 {
|
||||
self.short_term.get(&hash_key(exec)).copied().unwrap_or(0) as f64
|
||||
/ (self.max_short_term.max(1) as f64)
|
||||
}
|
||||
|
||||
pub fn get_freq<K: Hash>(&self, exec: &K) -> usize {
|
||||
self.long_term.get(&hash_key(exec)).copied().unwrap_or(0)
|
||||
pub fn get_freq<K: Hash>(&self, exec: &K) -> f64 {
|
||||
self.long_term.get(&hash_key(exec)).copied().unwrap_or(0) as f64
|
||||
/ (self.max_long_term.max(1) as f64)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -80,9 +91,13 @@ impl<'de> Deserialize<'de> for RecentUseStorage {
|
|||
type SerType = (HashMap<u64, usize>, Vec<u64>);
|
||||
let (long_term, stv) = SerType::deserialize(deserializer)?;
|
||||
let short_term: HashMap<_, _> = stv.into_iter().enumerate().map(|(v, k)| (k, v)).collect();
|
||||
let max_long_term = long_term.values().max().copied().unwrap_or(1);
|
||||
let max_short_term = short_term.values().max().copied().unwrap_or(1);
|
||||
Ok(RecentUseStorage {
|
||||
long_term,
|
||||
short_term,
|
||||
max_long_term,
|
||||
max_short_term,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue